using ryCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VSoft.Config
{
    /// <summary>
    /// 配置类,保存在运行期间会被改变的配置信息(本系统基于乘黄V2架构)
    /// </summary>
    public static class Soft_Config
    {
        /// <summary>
        /// 是否能关闭窗体。
        /// </summary>
        public static bool IsCanCloseForm = true;
        /// <summary>
        /// 在点击主窗口关闭按钮时,隐藏窗体(只有开启了托盘图标,本功能才能生效,此时需要通过托盘图标进行关闭)。
        /// </summary>
        public static bool HideByCloseBtn = true;
        /// <summary>
        /// 显示主窗体的热键
        /// </summary>
        public static string ShowMainHotKey = "";
        /// <summary>
        /// 是否开启鼠标快捷键
        /// </summary>
        public static bool ShowMainMouseKeyOn = true;
        /// <summary>
        /// 鼠标快捷键
        /// </summary>
        public static int ShowMainMouseKey = 0;
        /// <summary>
        /// 单击打开软件
        /// </summary>
        public static bool OpenByClick = false;
        /// <summary>
        /// 启动软件后隐藏自身
        /// </summary>
        public static bool HideAfterRun = true;
        /// <summary>
        /// 当前软件加载的热键列表
        /// </summary>
        public static List<HotKeyItem> HotKeyList = new List<HotKeyItem>();
        /// <summary>
        /// 获取设置
        /// </summary>
        public static void LoadSetting()
        {
            ryCommon.Storage Stor = new Storage();
            Stor.LoadFromFile(Soft_Info.UserDataFolder + "\\Setting.xml");
            Stor.SelectNodeBySet();
            ShowMainHotKey = Stor.GetAttrValue("ShowMainHotKey", "1+88");
            OpenByClick = Stor.GetAttrValue("OpenByClick", false);
            HideAfterRun = Stor.GetAttrValue("HideAfterRun", true);
            ShowMainMouseKeyOn = Stor.GetAttrValue("ShowMainMouseKeyOn", true);
            ShowMainMouseKey = Stor.GetAttrValue("ShowMainMouseKey", 0);
            //low_count = Stor.GetAttrValue("LowCount", 10);
        }
        /// <summary>
        /// 设置热键到列表中
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="hotkey"></param>
        /// <returns>返回热键在列表中的位置</returns>
        public static int SetHotKeyList(string id,string name,string hotkey)
        {
           var index= HotKeyList.FindIndex(a => a.ID == id);
            if(index!=-1)
            {
                HotKeyList[index].Name = name;
                HotKeyList[index].HotKey = hotkey;
                return index;
            }
            else
            {
                HotKeyList.Add(new HotKeyItem() { ID=id, Name=name, HotKey=hotkey });
                return HotKeyList.Count - 1;
            }
        }
        /// <summary>
        /// 从列表中删除热键
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回热键在原来列表中的位置</returns>
        public static int RemoveHotKeyList(string id)
        {
            var index = HotKeyList.FindIndex(a => a.ID == id);
            if (index != -1)
            {
                HotKeyList.RemoveAt(index);

            }
            return index;
        }
        /// <summary>
        /// 在列表中查找热键是否已存在
        /// </summary>
        /// <param name="hotkey"></param>
        /// <returns>返回热键在原来列表中的位置</returns>
        public static int IsHotKeyExistInList(string hotkey)
        {
            var index = HotKeyList.FindIndex(a => a.HotKey == hotkey);
            return index;
        }

    }
    public class HotKeyItem
    {
        /// <summary>
        /// ID
        /// </summary>
        public string ID { get; set; } = "";
        /// <summary>
        /// 热键名称
        /// </summary>
        public string Name { get; set; } = "";
        /// <summary>
        /// 热键
        /// </summary>
        public string HotKey { get; set; } = "";

    }
}
