using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XKRS.Common.Model.Helper
{
    public static class SettingHelper
    {
        const string SETTINGFILE = "Setting.json";
        //声明一个SettingInfo类型的字段
        public static SettingInfo SettingInfo;
        public static string SettingPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
        /// <summary>
        /// 检查配置文件是否存在且有效
        /// </summary>
        /// <returns>如果文件存在且配置内容有效，返回true，否则返回false</returns>
        public static bool IsSettingFileValid()
        {
            if (!File.Exists(SettingPath))
            {
                return false;
            }
            return null != SettingInfo;
        }

        public static void InitWithContent(string content)
        {
            SettingInfo = JsonConvert.DeserializeObject<SettingInfo>(content);
            using(StreamWriter writer=new StreamWriter(SettingPath, false, System.Text.Encoding.UTF8))
            {
                writer.Write(content);
                writer.Flush();
            }
        }


        /// <summary>
        /// 声明一个静态方法，获取选择的布局
        /// </summary>
        /// <returns>返回当前布局</returns>
        public static string GetSelectdLayout()
        {
            return SettingInfo.CurrLayout;
        }
        /// <summary>
        /// 获取生产代码，
        /// </summary>
        /// <returns>字符串类型的集合</returns>
        public static List<string> GetProductionCodes()
        {
            if (SettingInfo.ProductionCodes.Count == 0)
            {
                SettingInfo.ProductionCodes.Add("Default");
            }
            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProduction))
            {
                if (SettingInfo.ProductionCodes.Contains(SettingInfo.DefaultProduction))
                {
                    SettingInfo.ProductionCodes.Remove(SettingInfo.DefaultProduction);
                    SettingInfo.ProductionCodes.Insert(0, SettingInfo.DefaultProduction);
                }
            }
            return SettingInfo.ProductionCodes;
           
        }

        /// <summary>
        /// 获取过程代码
        /// </summary>
        /// <returns></returns>
        public static List<string> GetProcessCodes()
        {
            if (!string.IsNullOrWhiteSpace(SettingInfo.DefaultProcess))
            {
                if (SettingInfo.ProcessCodes.Contains(SettingInfo.DefaultProcess))
                {
                    SettingInfo.ProcessCodes.Remove(SettingInfo.DefaultProcess);
                    SettingInfo.ProcessCodes.Insert(0, SettingInfo.DefaultProcess);
                }
            }
            return SettingInfo.ProcessCodes;
        }


        public static string GetProgramDescription()
        {
            if (string.IsNullOrWhiteSpace(SettingInfo.Description))
            {
                SettingInfo.Description = "";
            }
            return SettingInfo.Description;
        }

        public static string GetProgramIcon()
        {
            string iconPath = SettingInfo.IconPath;
            if (string.IsNullOrWhiteSpace(iconPath) || !File.Exists(iconPath))
            {
                iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
            }
            if (!File.Exists(iconPath))
            {
                iconPath = "";
            }
            return iconPath;
        }
    }

    #region 保存
    public static void SetCurrLayout(string name)
    {
        if(SettingInfo.CurrLayout!=name)
    }

    #endregion 



    /// <summary>
    /// 设置信息类，声明自动属性
    /// </summary>
    public class SettingInfo
    {
        public List<string> ProcessCodes { get; set; } = new List<string>();
        public List<string> ProductionCodes { get; set; } = new List<string>();

        public string DefaultProcess { get; set; }
        public string DefaultProduction { get; set; }
        public string IconPath { get; set; }
        public string Description { get; set; }
        public string CurrLayout { get; set; } = "";

        public bool IsMinimumWhenClose { get; set; } = false;


    }
}
