using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{
 public  class GlobalVariable
  {
    public GlobalVariable()
    {
      
    }
    public static readonly GlobalVariable Instance = new GlobalVariable();
    /// <summary>
    /// 主界面最小化时隐藏
    /// </summary>
    public  bool HideMainFormOnMinimizedTag = false;
    
    public  string MyToken{  get; set; }
    /// <summary>
    /// 当前用户id
    /// </summary>
    public  string CurrentUserId { get; set; }
    /// <summary>
    /// 当前用户Name
    /// </summary>
    public  string CurrentUserName { get; set; }
    public string successString = "suceess";
    /// <summary>
    /// 系统名称
    /// </summary>
    
    public string SystemName = "酒钢智能计量系统客户端";
    public string InitPassword = "init";
    public  string ServerIp = "10.1.213.213";

    /// <summary>
    /// 更新程序名称
    /// </summary>
    public string ApplicationStart = "CarMeterSystem.exe";
    public string ReleaseFileName = "ReleaseList.xml";
    public string ReleaseURL = "http://10.129.1.210/pikaServices/ZNZD";
    public string LocalPath = "FantasyUpdate";
    public string ApplicationUpdater = "FantasyCorrector.exe";
    public  List<string> Authorities { get; set; }

    
    public  void WriteLocalConfigFile()
    {
      XmlDocument doc = new XmlDocument();
      CheckPathExist();
      if (!File.Exists(LocalConfigFile)) CreateLocalConfigFile();
      doc.Load(LocalConfigFile);

      XmlNode nodeLoginUsers = doc.SelectSingleNode("/WlsConfig/HistoryInfo/LoginUsers");
      XmlNode x = FindXmlNodeInList(nodeLoginUsers, "User", GlobalVariable.Instance.CurrentUserId);
      if (x == null)
      {
        XmlElement xe = doc.CreateElement("Item");
        xe.SetAttribute("User", GlobalVariable.Instance.CurrentUserId);
        xe.SetAttribute("Name", GlobalVariable.Instance.CurrentUserName);
        nodeLoginUsers.AppendChild(xe);
      }

      XmlNode nodeLastLoginUser = doc.SelectSingleNode("/WlsConfig/HistoryInfo/LastLoginUser");
      nodeLastLoginUser.Attributes["User"].Value = GlobalVariable.Instance.CurrentUserId;
      nodeLastLoginUser.Attributes["Name"].Value = GlobalVariable.Instance.CurrentUserName;

      doc.Save(LocalConfigFile);
    }
    public  string GetLastLoginUser(string attName)
    {
      if (!File.Exists(LocalConfigFile)) return string.Empty;
      XmlDocument doc = new XmlDocument();
      doc.Load(LocalConfigFile);
      XmlNode nodeLastLoginUser = doc.SelectSingleNode("/WlsConfig/HistoryInfo/LastLoginUser");
      return nodeLastLoginUser.Attributes[attName].Value;
    }
    public  XmlNodeList GetLoginUsers()
    {
      if (!File.Exists(GlobalVariable.Instance.LocalConfigFile)) return null;
      XmlDocument doc = new XmlDocument();
      doc.Load(GlobalVariable.Instance.LocalConfigFile);
      XmlNode nodeLoginUsers = doc.SelectSingleNode("/WlsConfig/HistoryInfo/LoginUsers");
      return nodeLoginUsers.ChildNodes;
    }
    private  void CheckPathExist()
    {
      if (!Directory.Exists(LocalConfigFilePath))
        Directory.CreateDirectory(LocalConfigFilePath);
      DirectoryInfo dir = new DirectoryInfo(LocalConfigFilePath);
    }
    private  string LocalConfigFilePath
    {
      get
      {
        return string.Format("{0}\\{1}", SystemPath, LocalConfigFileShortPath);
      }
    }
    /// <summary>
    /// 本地配置文件
    /// </summary>
    private  string LocalConfigFileShortPath = "LocalConfigFilePath";


    /// <summary>
    /// 用户配置文件保存路径
    /// </summary>
    public  string SystemPath { get; set; }
    public string Path= System.Environment.CurrentDirectory;
    private  string LocalConfigFile
    {
      get
      {
        return string.Format("{0}\\{1}", LocalConfigFilePath, "LocalConfigFile.xml");
      }
    }
    public string TableConfigFile
    {
      get
      {
        return string.Format("{0}\\{1}", LocalConfigFilePath, "TableConfigFile.xml");
      }
    }
    private   XmlNode FindXmlNodeInList(XmlNode nodeList, string attributeName, string attributeValue)
    {
      if (nodeList == null || nodeList.ChildNodes.Count == 0) return null;
      foreach (XmlNode x in nodeList.ChildNodes)
      {
        if (x.Attributes[attributeName] != null && x.Attributes[attributeName].Value.ToUpper() == attributeValue.ToUpper())
          return x;
      }
      return null;
    }
    private  void CreateLocalConfigFile()
    {
      XmlTextWriter writer = new XmlTextWriter(LocalConfigFile, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartDocument();
      writer.WriteStartElement("WlsConfig");

      writer.WriteStartElement("HistoryInfo");
      writer.WriteStartElement("LoginUsers");
      writer.WriteEndElement();
      writer.WriteStartElement("LastLoginUser");
      writer.WriteAttributeString("User", GlobalVariable.Instance.CurrentUserId);
      writer.WriteAttributeString("Name", GlobalVariable.Instance.CurrentUserName);
      writer.WriteEndElement();
      writer.WriteEndElement();

      writer.WriteEndElement();
      writer.WriteEndDocument();
      writer.Flush();
      writer.Close();
    }
  }
}
