using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace HighWayIot.Winform.Business
{
    /// <summary>
    /// XML读写类
    /// </summary>
    public class XmlUtil
    {
        private static readonly Lazy<XmlUtil> lazy = new Lazy<XmlUtil>(() => new XmlUtil());

        public static XmlUtil Instance
        {
            get
            {
                return lazy.Value;
            }
        }

        /// <summary>
        /// XML读写实例
        /// </summary>
        XmlDocument xmlDocument = new XmlDocument();

        /// <summary>
        /// 运行时路径
        /// </summary>
        private string Path = System.Environment.CurrentDirectory;

        /// <summary>
        /// 配置读取
        /// </summary>
        /// <returns></returns>
        public List<RoleConfig> ConfigReader()
        {
            List<RoleConfig> list = new List<RoleConfig>();

            xmlDocument.Load($"{Path}\\Configuration.xml");
            XmlNode root = xmlDocument.DocumentElement;
            XmlNode node = root.SelectSingleNode("RoleConfig");
            foreach (XmlNode role in node)
            {
                XmlAttribute pageName = (XmlAttribute)role.Attributes.GetNamedItem("PageName");
                XmlAttribute roleIndex = (XmlAttribute)role.Attributes.GetNamedItem("RoleIndex");
                int.TryParse(roleIndex.Value, out int index);
                list.Add(new RoleConfig()
                {
                    PageName = pageName.Value,
                    RoleIndex = index,
                });
            }

            return list;
        }
    }

    public class RoleConfig
    {
        /// <summary>
        /// 页面名称
        /// </summary>
        public string PageName { get; set; }

        /// <summary>
        /// 规则编号
        /// </summary>
        public int RoleIndex { get; set; }
    }
}
