using Newtonsoft.Json.Linq;
using ryCommon;
using ryControls;
using RySkins;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace SkinPreview
{
    public partial class FrmTab1 : Form
    {
        public FrmTab1()
        {
            InitializeComponent();
            SkinSetting.LoadSkin(Application.StartupPath + "\\skin.json");
            LoadUIBySkins();
        }

        private void BtnMsg_Click(object sender, EventArgs e)
        {
            RySkins.Msg.ShowMsgUI("对话框展示测试。","提示", MessageBoxButtons.OK);
        }
        private bool IsProcUse = false;
        private void LoadUIBySkins()
        {
            IsProcUse = true;
            Color_CaptionBackColorTop.BackColor = SkinSetting.FormSkin.CaptionBackColorTop;
            Color_CaptionBackColorBottom.BackColor = SkinSetting.FormSkin.CaptionBackColorBottom;
            Color_CaptionTextColor.BackColor = SkinSetting.FormSkin.CaptionTextColor;
            NumIconLeftMargin.Value = SkinSetting.FormSkin.IconLeftMargin;
            NumIconSize.Value = SkinSetting.FormSkin.IconSize.Width;
            NumCaptionHeight.Value = SkinSetting.FormSkin.CaptionHeight;
            Color_minForeColor.BackColor = SkinSetting.minForeColor;
            Color_minBackColorHover.BackColor = SkinSetting.minBackColorHover;
            Color_minBackColorPressed.BackColor = SkinSetting.minBackColorPressed;
            Color_maxForeColor.BackColor = SkinSetting.maxForeColor;
            Color_maxBackColorHover.BackColor = SkinSetting.maxBackColorHover;
            Color_maxBackColorPressed.BackColor = SkinSetting.maxBackColorPressed;
            Color_closeForeColor.BackColor = SkinSetting.closeForeColor;
            Color_closeBackColorHover.BackColor = SkinSetting.closeBackColorHover;
            Color_closeBackColorPressed.BackColor = SkinSetting.closeBackColorPressed;
            NumBorderWidth.Value = SkinSetting.FormSkin.BorderWidth;
            Color_FormBackColor.BackColor = SkinSetting.FormSkin.FormBackColor;
            Color_FormBorderOutterColor.BackColor = SkinSetting.FormSkin.FormBorderOutterColor;
            Color_FormBorderInnerColor.BackColor = SkinSetting.FormSkin.FormBorderInnerColor;
            Color_FormBorderInmostColor.BackColor = SkinSetting.FormSkin.FormBorderInmostColor;
            ChkShowShadow.Checked = SkinSetting.FormSkin.ShowShadow;
            NumShadowWidth.Value = SkinSetting.FormSkin.ShadowWidth;
            Color_ShadowColor.BackColor = SkinSetting.FormSkin.ShadowColor;
            NumShadowAValueDark.Value = SkinSetting.FormSkin.ShadowAValueDark;
            NumShadowAValueLight.Value = SkinSetting.FormSkin.ShadowAValueLight;
            Color_TabActiveBackColorTop.BackColor = SkinSetting.FormSkin.Mdi_TabActiveBackColorTop;
            Color_TabActiveBackColorBottom.BackColor = SkinSetting.FormSkin.Mdi_TabActiveBackColorBottom;
            Color_TabDeactiveBackColorTop.BackColor = SkinSetting.FormSkin.Mdi_TabDeactiveBackColorTop;
            Color_TabDeactiveBackColorBottom.BackColor = SkinSetting.FormSkin.Mdi_TabDeactiveBackColorBottom;
            Color_TabMoveColorStart.BackColor = SkinSetting.TabMoveColorStart;
            Color_TabMoveColorEnd.BackColor = SkinSetting.TabMoveColorEnd;
            IsProcUse = false;
        }
        private void SaveSkins()
        {
            SaveSkins(Application.StartupPath + "\\skin.json");
        }
        private void SaveSkins(string path)
        {
            JObject jo = new JObject
            {
                { "CaptionBackColorTop", Color_CaptionBackColorTop.HtmlColor },
                { "CaptionBackColorBottom", Color_CaptionBackColorBottom.HtmlColor },
                { "CaptionTextColor", Color_CaptionTextColor.HtmlColor },
                { "IconLeftMargin", NumIconLeftMargin.Value.ToInt() },
                { "IconSize", NumIconSize.Value.ToInt() },
                { "CaptionHeight", NumCaptionHeight.Value.ToInt() },
                { "BorderWidth", NumBorderWidth.Value.ToInt() },
                { "minForeColor", Color_minForeColor.HtmlColor },
                { "minBackColorHover", Color_minBackColorHover.HtmlColor },
                { "minBackColorPressed", Color_minBackColorPressed.HtmlColor },
                { "maxForeColor", Color_maxForeColor.HtmlColor },
                { "maxBackColorHover", Color_maxBackColorHover.HtmlColor },
                { "maxBackColorPressed", Color_maxBackColorPressed.HtmlColor },
                { "closeForeColor", Color_closeForeColor.HtmlColor },
                { "closeBackColorHover", Color_closeBackColorHover.HtmlColor },
                { "closeBackColorPressed", Color_closeBackColorPressed.HtmlColor },
                { "FormBackColor",Color_FormBackColor.HtmlColor },
                { "FormBorderOutterColor",Color_FormBorderOutterColor.HtmlColor },
                { "FormBorderInnerColor",Color_FormBorderInnerColor.HtmlColor },
                { "FormBorderInmostColor",Color_FormBorderInmostColor.HtmlColor },
                { "ShowShadow",ChkShowShadow.Checked },
                { "ShadowWidth",NumShadowWidth.Value.ToInt() },
                { "ShadowColor",Color_ShadowColor.HtmlColor },
                { "ShadowAValueDark",NumShadowAValueDark.Value.ToInt() },
                { "ShadowAValueLight",NumShadowAValueLight.Value.ToInt() },
                { "TabActiveBackColorTop",Color_TabActiveBackColorTop.HtmlColor },
                { "TabActiveBackColorBottom",Color_TabActiveBackColorBottom.HtmlColor },
                { "TabDeactiveBackColorTop",Color_TabDeactiveBackColorTop.HtmlColor },
                { "TabDeactiveBackColorBottom",Color_TabDeactiveBackColorBottom.HtmlColor },
                { "TabMoveColorStart",Color_TabMoveColorStart.HtmlColor },
                { "TabMoveColorEnd",Color_TabMoveColorEnd.HtmlColor },
            };
            JObject jo_skin = new JObject
            {
                { "name",TxtSkinName.Text },
                { "date", DateTime.Now.ToInt64() },
                { "ver", TxtVer.Text },
                { "author", TxtAuthor.Text },
                { "skin", jo }
            };
            RyFiles.WriteAllText(path, jo_skin.ToString());
            Config.MainTabForm.Refresh();
            Config.MainTabForm.UpdateSkins();
        }
        private void Color_CaptionBackColorTop_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.CaptionBackColorTop = Color_CaptionBackColorTop.BackColor;
            SaveSkins();
        }

        private void Color_CaptionBackColorBottom_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.CaptionBackColorBottom = Color_CaptionBackColorBottom.BackColor;
            SaveSkins();
        }

        private void Color_CaptionTextColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.CaptionTextColor = Color_CaptionTextColor.BackColor;
            SaveSkins();
        }

        private void NumIconSize_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var icon_size = NumIconSize.Value.ToInt();
            SkinSetting.FormSkin.IconSize = new Size(icon_size, icon_size);
            SaveSkins();
        }

        private void NumIconLeftMargin_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var IconLeftMargin = NumIconLeftMargin.Value.ToInt();
            SkinSetting.FormSkin.IconLeftMargin = IconLeftMargin;
            SaveSkins();
        }

        private void NumCaptionHeight_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var CaptionHeight = NumCaptionHeight.Value.ToInt();
            SkinSetting.FormSkin.CaptionHeight = CaptionHeight;
            SaveSkins();
        }

        private void NumBorderWidth_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var BorderWidth = NumBorderWidth.Value.ToInt();
            SkinSetting.FormSkin.BorderWidth = BorderWidth;
            SaveSkins();
        }

        private void Color_FormBackColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.FormBackColor = Color_FormBackColor.BackColor;
            SaveSkins();
        }

        private void Color_FormBorderOutterColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.FormBorderOutterColor = Color_FormBorderOutterColor.BackColor;
            SaveSkins();
        }

        private void Color_FormBorderInnerColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.FormBorderInnerColor  = Color_FormBorderInnerColor.BackColor;
            SaveSkins();
        }

        private void Color_FormBorderInmostColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.FormBorderInmostColor  = Color_FormBorderInmostColor.BackColor;
            SaveSkins();
        }

        private void NumShadowWidth_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var ShadowWidth = NumShadowWidth.Value.ToInt();
            SkinSetting.FormSkin.ShadowWidth  = ShadowWidth;
            SaveSkins();
        }

        private void ChkShowShadow_CheckedChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.ShowShadow= ChkShowShadow.Checked;
            SaveSkins();
        }

        private void Color_ShadowColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.ShadowColor  = Color_ShadowColor.BackColor;
            SaveSkins();
        }

        private void NumShadowAValueDark_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var ShadowAValueDark = NumShadowAValueDark.Value.ToInt();
            SkinSetting.FormSkin.ShadowAValueDark= ShadowAValueDark;
            SaveSkins();
        }

        private void NumShadowAValueLight_ValueChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var ShadowAValueLight = NumShadowAValueLight.Value.ToInt();
            SkinSetting.FormSkin.ShadowAValueLight = ShadowAValueLight;
            SaveSkins();
        }

        private void Color_TabActiveBackColorTop_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.Mdi_TabActiveBackColorTop= Color_TabActiveBackColorTop.BackColor;
            SaveSkins();
        }

        private void Color_TabActiveBackColorBottom_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.Mdi_TabActiveBackColorBottom  = Color_TabActiveBackColorBottom.BackColor;
            SaveSkins();
        }

        private void Color_TabDeactiveBackColorTop_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.Mdi_TabDeactiveBackColorTop  = Color_TabDeactiveBackColorTop.BackColor;
            SaveSkins();
        }

        private void Color_TabDeactiveBackColorBottom_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.FormSkin.Mdi_TabDeactiveBackColorBottom  = Color_TabDeactiveBackColorBottom.BackColor;
            SaveSkins();
        }

        private void Color_TabMoveColorStart_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.TabMoveColorStart  = Color_TabMoveColorStart.BackColor;
            SaveSkins();
        }

        private void Color_TabMoveColorEnd_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            SkinSetting.TabMoveColorEnd = Color_TabMoveColorEnd.BackColor;
            SaveSkins();
        }

        private void BtnLoadSkin_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var skin_json = JObject.Parse(RyFiles.ReadAllText(openFileDialog1.FileName));
                TxtSkinName.Text = skin_json["name"]?.ToString();
                TxtVer.Text = skin_json["ver"]?.ToString();
                TxtAuthor.Text = skin_json["author"]?.ToString();
                SkinSetting.LoadSkin(openFileDialog1.FileName);
                LoadUIBySkins();
                Config.MainTabForm.Refresh();
                Config.MainTabForm.UpdateSkins();
            }
        }

        private void FrmTab1_Load(object sender, EventArgs e)
        {
            Config.MainTabForm.Refresh();
            Config.MainTabForm.UpdateSkins();
        }

        private void BtnSaveSkin_Click(object sender, EventArgs e)
        {
            if(saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                SaveSkins(saveFileDialog1.FileName);
            }
        }

        private void Color_minForeColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.minForeColor = color_item.BackColor;
            SkinSetting.SaveMinBtnSkin();
            SaveSkins();
        }

        private void Color_maxForeColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.maxForeColor = color_item.BackColor;
            SkinSetting.SaveMaxBtnSkin();
            SaveSkins();
        }

        private void Color_closeForeColor_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.closeForeColor = color_item.BackColor;
            SkinSetting.SaveCloseBtnSkin();
            SaveSkins();
        }

        private void Color_minBackColorHover_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.minBackColorHover = color_item.BackColor;
            SkinSetting.SaveMinBtnSkin();
            SaveSkins();
        }

        private void Color_maxBackColorHover_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.maxBackColorHover = color_item.BackColor;
            SkinSetting.SaveMaxBtnSkin();
            SaveSkins();
        }

        private void Color_closeBackColorHover_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.closeBackColorHover = color_item.BackColor;
            SkinSetting.SaveCloseBtnSkin();
            SaveSkins();
        }

        private void Color_minBackColorPressed_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.minBackColorPressed = color_item.BackColor;
            SkinSetting.SaveMinBtnSkin();
            SaveSkins();
        }

        private void Color_maxBackColorPressed_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.maxBackColorPressed = color_item.BackColor;
            SkinSetting.SaveMaxBtnSkin();
            SaveSkins();
        }

        private void Color_closeBackColorPressed_BackColorChanged(object sender, EventArgs e)
        {
            if (IsProcUse) { return; }
            var color_item = (SuperColor)sender;
            SkinSetting.closeBackColorPressed = color_item.BackColor;
            SkinSetting.SaveCloseBtnSkin();
            SaveSkins();
        }
    }
}
