using MGdu.WinFormUI;
using ryControls;
using SkinPreview;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace RySkins
{
    [ComVisible(true)]
    public partial class SKinTabForm : SKinForm
    {
        public new DateTime OpenTime { get; private set; } = DateTime.MinValue;
        /// <summary>
        /// 无参数启动
        /// </summary>
        public SKinTabForm() : base()
        {
            OpenTime = DateTime.Now;
            InitializeComponent();
            this.Resize += TabControl1_Resize;
            InitTab();
        }
        /// <summary>
        /// 无参数启动
        /// </summary>
        public SKinTabForm(string[] args) : base()
        {
            OpenTime = DateTime.Now;
            InitializeComponent();
            this.Resize += TabControl1_Resize;
            InitTab();
        }
        private void InitTab()
        {
            UpdateSkins();
            chromeTabControl1.SizeMode = TabSizeMode.Normal;
            chromeTabControl1.SelectedIndexChanged += TabControl1_SelectedIndexChanged;
        }
        /// <summary>
        /// 显示的窗口列表
        /// </summary>
        public List<Form> TabForms { get; set; }=new List<Form>();
        private int _SelectedIndex = 0;
        /// <summary>
        /// 设置选择的标签页
        /// </summary>
        public int SelectedIndex {
            get
            {
                return _SelectedIndex;
            }
            set
            {
                _SelectedIndex = value;
                if(chromeTabControl1.TabPages.Count> _SelectedIndex)
                {
                    chromeTabControl1.SelectedIndex= _SelectedIndex;  
                }
            }
        }
        public void UpdateSkins()
        {
            chromeTabControl1.TabOnColorStart = XTheme.Mdi_TabActiveBackColorTop;
            chromeTabControl1.TabOnColorEnd = XTheme.Mdi_TabActiveBackColorBottom;
            chromeTabControl1.TabOffColorStart = XTheme.Mdi_TabDeactiveBackColorTop;
            chromeTabControl1.TabOffColorEnd = XTheme.Mdi_TabDeactiveBackColorBottom;
            chromeTabControl1.TabMoveColorStart = SkinSetting.TabMoveColorStart;
            chromeTabControl1.TabMoveColorEnd = SkinSetting.TabMoveColorEnd;
        }
        private void SKinTabForm_Load(object sender, EventArgs e)
        {
            UpdateSkins();
            for (int i = 0; i < TabForms.Count; i++)
            {
                var item = TabForms[i];
                item.EnabledChanged += Item_EnabledChanged;
                TabPage tab = new TabPage
                {
                    Text = item.Text+"    ",
                    BackColor= SystemColors.Window
                };
                ryCommon.RyForm.SetParentWin(item, tab);
                chromeTabControl1.TabPages.Add(tab);
                if(i==_SelectedIndex)
                {
                    item.Show();
                }
            }
            ReizeUI();
        }

        private void Item_EnabledChanged(object sender, EventArgs e)
        {
            this.Enabled = ((Form)sender).Enabled;
        }

        private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TabForms[chromeTabControl1.SelectedIndex].Activate();
            TabForms[chromeTabControl1.SelectedIndex].Show();
        }

        private void TabControl1_Resize(object sender, EventArgs e)
        {
            ReizeUI();
        }

        private void ReizeUI()
        {
            if(chromeTabControl1.TabPages.Count!= TabForms.Count) { return; }
            for (int i = 0; i < TabForms.Count; i++)
            {
                var item = TabForms[i];
                ryCommon.RyForm.ResizeWin(item, chromeTabControl1.TabPages[i]);
            }
        }

        private void SKinTabForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            for (int i = 0; i < TabForms.Count; i++)
            {
                var item = TabForms[i];
                item.Close();
            }
        }

        private void SKinTabForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}
