using MGdu.WinFormUI;
using Newtonsoft.Json.Linq;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace RySkins
{
    public class SkinSetting
    {
        /// <summary>
        /// MGdu窗体的默认皮肤
        /// </summary>
        public static ThemeFormBase FormSkin { get; set; } = new ThemeForm();
        /// <summary>
        /// Tab标签鼠标移上时的颜色开始色
        /// </summary>
        public static Color TabMoveColorStart { get; set; }
        /// <summary>
        /// Tab标签鼠标移上时的颜色结束色
        /// </summary>
        public static Color TabMoveColorEnd { get; set; }
        /// <summary>
        /// 最小化按钮字体色
        /// </summary>
        public static Color minForeColor { get; set; } = Color.Gray;
        /// <summary>
        /// 最小化按钮激活背景色
        /// </summary>
        public static Color minBackColorHover { get; set; } = Color.FromArgb(67, 139, 221);
        /// <summary>
        /// 最小化按钮按下背景色
        /// </summary>
        public static Color minBackColorPressed { get; set; } = Color.FromArgb(50, 125, 210);
        /// <summary>
        /// 最大化按钮字体色
        /// </summary>
        public static Color maxForeColor { get; set; } = Color.Gray;
        /// <summary>
        /// 最大化按钮激活背景色
        /// </summary>
        public static Color maxBackColorHover { get; set; } = Color.FromArgb(67, 139, 221);
        /// <summary>
        /// 最大化按钮按下背景色
        /// </summary>
        public static Color maxBackColorPressed { get; set; } = Color.FromArgb(50, 125, 210);
        /// <summary>
        /// 关闭按钮字体色
        /// </summary>
        public static Color closeForeColor { get; set; } = Color.Gray;
        /// <summary>
        /// 关闭按钮激活背景色
        /// </summary>
        public static Color closeBackColorHover { get; set; } = Color.FromArgb(217, 71, 71);
        /// <summary>
        /// 关闭按钮按下背景色
        /// </summary>
        public static Color closeBackColorPressed { get; set; } = Color.FromArgb(188, 52, 52);
        public static void SaveCloseBtnSkin()
        {
            if (FormSkin.CloseBoxColor == null) { FormSkin.CloseBoxColor = new ButtonColorTable(); }
            FormSkin.CloseBoxColor.ForeColorNormal = FormSkin.CloseBoxColor.ForeColorHover
                = FormSkin.CloseBoxColor.ForeColorPressed = closeForeColor;
            FormSkin.CloseBoxColor.BackColorHover = closeBackColorHover;
            FormSkin.CloseBoxColor.BackColorPressed = closeBackColorPressed;
        }
        public static void SaveMinBtnSkin()
        {
            if (FormSkin.MinBoxColor == null) { FormSkin.MinBoxColor = new ButtonColorTable(); }
            //FormSkin.MinBoxColor = new ButtonColorTable();
            FormSkin.MinBoxColor.ForeColorNormal = FormSkin.MinBoxColor.ForeColorHover
                = FormSkin.MinBoxColor.ForeColorPressed = minForeColor;
            FormSkin.MinBoxColor.BackColorHover = minBackColorHover;
            FormSkin.MinBoxColor.BackColorPressed = minBackColorPressed;
        }
        public static void SaveMaxBtnSkin()
        {
            if (FormSkin.MaxBoxColor == null) { FormSkin.MaxBoxColor = new ButtonColorTable(); }
            //FormSkin.MaxBoxColor = new ButtonColorTable();
            FormSkin.MaxBoxColor.ForeColorNormal = FormSkin.MaxBoxColor.ForeColorHover
                = FormSkin.MaxBoxColor.ForeColorPressed = maxForeColor;
            FormSkin.MaxBoxColor.BackColorHover = maxBackColorHover;
            FormSkin.MaxBoxColor.BackColorPressed = maxBackColorPressed;
        }
        /// <summary>
        /// 加载皮肤
        /// </summary>
        /// <param name="path"></param>
        public static void LoadSkin(string path)
        {
            if (!System.IO.File.Exists(path)) {
                SaveMinBtnSkin();
                SaveMaxBtnSkin();
                SaveCloseBtnSkin();
                return;
            }
            var skin = FormSkin;
            var skin_json= JObject.Parse(RyFiles.ReadAllText(path));
            var skin_item = skin_json["skin"];
            if (skin_item == null) { return; }            
            JObject jo = JObject.Parse(skin_item.ToString());
            skin.CaptionBackColorTop = GetColor("CaptionBackColorTop", skin.CaptionBackColorTop);
            skin.CaptionBackColorBottom = GetColor("CaptionBackColorBottom", skin.CaptionBackColorBottom);
            skin.CaptionTextColor = GetColor("CaptionTextColor", skin.CaptionTextColor);
            skin.IconLeftMargin = GetInt("IconLeftMargin", skin.IconLeftMargin);
            var icon_size = GetInt("IconSize", skin.IconSize.Width);
            skin.IconSize = new Size(icon_size, icon_size);
            skin.CaptionHeight = GetInt("CaptionHeight", skin.CaptionHeight);
            minForeColor = GetColor("minForeColor", minForeColor);
            minBackColorHover = GetColor("minBackColorHover", minBackColorHover);
            minBackColorPressed = GetColor("minBackColorPressed", minBackColorPressed);
            maxForeColor = GetColor("maxForeColor", maxForeColor);
            maxBackColorHover = GetColor("maxBackColorHover", maxBackColorHover);
            maxBackColorPressed = GetColor("maxBackColorPressed", maxBackColorPressed);
            closeForeColor = GetColor("closeForeColor", closeForeColor);
            closeBackColorHover = GetColor("closeBackColorHover", closeBackColorHover);
            closeBackColorPressed = GetColor("closeBackColorPressed", closeBackColorPressed);
            SaveMinBtnSkin();
            SaveMaxBtnSkin();
            SaveCloseBtnSkin();
            skin.BorderWidth = GetInt("BorderWidth", skin.BorderWidth);
            skin.FormBackColor = GetColor("FormBackColor", skin.FormBackColor);
            skin.FormBorderOutterColor = GetColor("FormBorderOutterColor", skin.FormBorderOutterColor);
            skin.FormBorderInnerColor = GetColor("FormBorderInnerColor", skin.FormBorderInnerColor);
            skin.FormBorderInmostColor = GetColor("FormBorderInmostColor", skin.FormBorderInmostColor);
            skin.ShowShadow = GetBool("ShowShadow", skin.ShowShadow);
            skin.ShadowWidth = GetInt("ShadowWidth", skin.ShadowWidth);
            skin.ShadowColor = GetColor("ShadowColor", skin.ShadowColor);
            skin.ShadowAValueDark = GetInt("ShadowAValueDark", skin.ShadowAValueDark);
            skin.ShadowAValueLight = GetInt("ShadowAValueLight", skin.ShadowAValueLight);
            skin.Mdi_TabActiveBackColorTop = GetColor("TabActiveBackColorTop", skin.Mdi_TabActiveBackColorTop);
            skin.Mdi_TabActiveBackColorBottom = GetColor("TabActiveBackColorBottom", skin.Mdi_TabActiveBackColorBottom);
            skin.Mdi_TabDeactiveBackColorTop = GetColor("TabDeactiveBackColorTop", skin.Mdi_TabDeactiveBackColorTop);
            skin.Mdi_TabDeactiveBackColorBottom = GetColor("TabDeactiveBackColorBottom", skin.Mdi_TabDeactiveBackColorBottom);
            SkinSetting.TabMoveColorStart = GetColor("TabMoveColorStart", SkinSetting.TabMoveColorStart);
            SkinSetting.TabMoveColorEnd = GetColor("TabMoveColorEnd", SkinSetting.TabMoveColorEnd);
            string GetJItem(string id)
            {
                var item = jo[id];
                if (item == null) return "";
                return item.ToString();
            }
            Color GetColor(string id, Color defColor)
            {
                try
                {
                    var item = GetJItem(id);
                    if (item.Length == 0) { return defColor; }
                    return System.Drawing.ColorTranslator.FromHtml(item);
                }
                catch { return defColor; }
            }
            int GetInt(string id, int defValue)
            {
                try
                {
                    var item = GetJItem(id);
                    if (item.Length == 0) { return defValue; }
                    return item.ToInt();
                }
                catch { return defValue; }
            }
            bool GetBool(string id, bool defValue)
            {
                try
                {
                    var item = GetJItem(id);
                    if (item.Length == 0) { return defValue; }
                    return item.ToBool();
                }
                catch { return defValue; }
            }
        }
    }
}
