 //ライセンス情報はAgeOnフォルダのLISENCEフォルダを参照してください
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;


using Dotnetrix.Controls;

namespace nispi.controlclass
{
    
    class TabControlParts : ControlBase
    {
        public TabControlEX control;

        public TabControlParts()
        {
            control = new TabControlEX();

            this.control.AllowDrop = false;
            this.control.ItemSize = new Size(90, 25);
            //this.control.TabStyles = KRBTabControl.TabStyle.KRBStyle;
           // //this.control.BackgroundColor = Color.LightSteelBlue;
           // this.control.IsDrawHeader = true;
            //this.control.HeaderStyle = KRBTabControl.TabHeaderStyle.Texture; ;
            //this.control.IsCaptionVisible = false;
            this.control.Location = new Point(0, 0);
            //this.control.Left = 0;
            //this.control.TabGradient.ColorEnd = SystemColors.Control;
            //this.control.TabGradient.ColorStart = Color.AliceBlue;
            //this.control.IsDrawEdgeBorder = false;
            //this.control.TabGradient.GradientStyle = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
            //this.control.IsDrawTabSeparator = true;
            this.control.Multiline = true;
            this.control.Appearance = TabAppearanceEX.Button;

            
            //this.control.Dock = DockStyle.Fill;
            //this.control.TabStop = true;
            //this.control.FlatBorderColor = Color.Red;
            this.control.HotTrack = true;
            this.control.UseVisualStyles = false;
            //this.control.FlatBorderColor = Color.Red;
            this.control.SelectedTabColor = Color.LightBlue;
            this.control.HotColor = Color.AliceBlue;
            this.control.MouseDoubleClick += new MouseEventHandler(tabPageMouseDoubleClick);
            //this.control.UpDownStyle = nispi.KRBTabControl.UpDown32Style.OfficeBlue;////.Default;
            this.control.Font = new Font("ＭＳ Ｐゴシック", 11.0F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(128)));
            this.controlBase = (System.Windows.Forms.Control)control;

            //this.control.DrawMode = TabDrawMode.OwnerDrawFixed;
            //this.control.DrawItem += new DrawItemEventHandler(control_DrawItem);
        }

        public void control_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            String ss = this.control.TabPages[e.Index].Text;
            g.FillRectangle(Brushes.White, e.Bounds);
            g.DrawString(ss, e.Font, Brushes.Black, new Point(e.Bounds.Left + 3, e.Bounds.Top + 3));
        }

        public void tabPageMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Name.Equals("threadListTabControlParts") == true)
                MainFormConst.threadListTabMain.viewList[this.control.SelectedIndex].Reload();
            else if (this.Name.Equals("boardListTabControlParts") == true)
                MainFormConst.boardListTabMain.view_list[this.control.SelectedIndex].reload();
            else if (this.Name.Equals("boardIndexTabControlParts") == true)
            {
                if (Const.form.boardIndexTabControlParts.SelectedIndex == 0)
                    MainFormConst.boardIndexTabMain.boardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 1)
                    MainFormConst.boardIndexTabMain.scBoardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 2)
                    MainFormConst.boardIndexTabMain.open2chBoardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 3)
                    MainFormConst.boardIndexTabMain.next2chBoardIndexView.LoadBoardList();

                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 4)
                    MainFormConst.boardIndexTabMain.jikkyoBoardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 5)
                    MainFormConst.boardIndexTabMain.blogbanBoardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 6)
                    MainFormConst.boardIndexTabMain.shizuBoardIndexView.LoadBoardList();

                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 7)
                    MainFormConst.boardIndexTabMain.vip2chBoardIndexView.LoadBoardList();
                else if (Const.form.boardIndexTabControlParts.SelectedIndex == 8)
                    MainFormConst.boardIndexTabMain.machibbsBoardIndexView.LoadBoardList();

                else
                    MainFormConst.boardIndexTabMain.favoriteBoardIndexView.LoadFavoriteBoardList();
            }
        }

        public event TabControlEventHandler Selected
        {
            add
            {
                this.control.Selected += value;
            }
            remove
            {
                this.control.Selected -= value;
            }
        }

        public System.Windows.Forms.Control.ControlCollection Controls
        {
            get
            {
                return this.control.Controls;
            }
        }

        //オプション（設定）用の外観を作る
        public void SetOptionParts()
        {
            //this.control.TabGradient.ColorEnd = Color.RoyalBlue;
            //this.control.TabGradient.ColorStart = Color.White;
        }
        public void Set_TabSizeMode_Fixed()
        {
            this.control.SizeMode = TabSizeMode.Fixed;
        }

        //選択する時のイベント
        /*public TabControlCancelEventHandler currentTabControlCancelEventHandler;
        public void SetTabSelecting(TabControlPartsDelegate.MultiTabControlCancelEventHandler method)
        {
            currentTabControlCancelEventHandler = new TabControlCancelEventHandler(method);
            this.control.Selecting += currentTabControlCancelEventHandler;
        }

        public void ReSetTabSelecting()
        {
            this.control.Selecting -= currentTabControlCancelEventHandler;
        }
        public event TabControlPartsDelegate.MultiTabControlCancelEventHandler Selecting
        {
            add
            {
                currentTabControlCancelEventHandler = new TabControlCancelEventHandler(value);
                this.control.Selecting += currentTabControlCancelEventHandler;
            }
            remove
            {
                this.control.Selecting -= currentTabControlCancelEventHandler;
            }
        }*/
        public int Count
        {
            get
            {
                return this.control.Controls.Count;
            }
        }
        public TabPageExParts SelectedTab
        {
            set
            {
                this.control.SelectedTab = (TabPageEX)value.control;
            }
        }
        public TabPage GetSelectedTab()
        {
            return this.control.SelectedTab;
        }
        public Rectangle GetTabRect(int num)
        {
            return this.control.GetTabRect(num);
        }
        public ImageList ImageList
        {
            set
            {
                this.control.ImageList = value;
            }
            get
            {
                return this.control.ImageList;
            }
        }

        public int SelectedIndex
        {
            set
            {
                this.control.SelectedIndex = value;
            }
            get
            {
                return this.control.SelectedIndex;
            }
        }

        public bool IsCaptionVisible
        {
            set
            {
                //this.control.IsCaptionVisible = value;
            }
        }
        public string SelectedTabName
        {
            set
            {
                this.control.SelectedTab.Name = value;
            }
            get
            {
                return this.control.SelectedTab.Name;
            }
        }
        public string SelectedTabText
        {
            set
            {
                this.control.SelectedTab.Text = value;
            }
            get
            {
                return this.control.SelectedTab.Text;
            }
        }
        public Size ItemSize
        {
            set
            {
                this.control.ItemSize = value;
            }
        }

        public Color SelectedTabColorP
        {
            set
            {
                this.control.SelectedTabColor = value;
            }
            get
            {
                return this.control.SelectedTabColor;
            }
        }
        public Color HotColorP
        {
            set
            {
                this.control.HotColor = value;
            }
            get
            {
                return this.control.HotColor;
            }
        }
        public Color ForeColorP
        {
            set
            {
                this.control.ForeColor = value;
            }
            get
            {
                return this.control.ForeColor;
            }
        }

        public void RemoveIndex(int index)
        {
            this.control.Controls.RemoveAt(index);
        }
    }
    /// <summary>
    /// タブページのスタックを作る。足りなければ新たに作成する。
    /// 多すぎればある程度まで削減する機能があってもいい
    /// </summary>
    class TabControlPartsManager
    {
        public static LinkedList<TabControlParts> allFormList = new LinkedList<TabControlParts>();
        public static Stack<TabControlParts> tabPageStack = new Stack<TabControlParts>();
        public static Mutex mutex = new Mutex(false, "TabControlPartsManager");

        public static TabControlParts Pop()
        {
            mutex.WaitOne();

            if (tabPageStack.Count == 0)
            {
                TabControlParts tempParts = new TabControlParts();
                ChangeOutWard(tempParts);
                tabPageStack.Push(tempParts);
            }
            TabControlParts tabPage = tabPageStack.Pop();

            //GUIのリストを作成する
            if (null == allFormList.Find(tabPage))
                allFormList.AddLast(tabPage);

            mutex.ReleaseMutex();

            return tabPage;
        }

        public static void Push(TabControlParts tabPage)
        {
            tabPage.ResetControlBase();

            mutex.WaitOne();

            tabPageStack.Push(tabPage);

            mutex.ReleaseMutex();
        }

        public static void ChangeOutWard()
        {
            mutex.WaitOne();

            Const.form.mainForm.SuspendLayout();
            Const.mapForm.mapMainForm.SuspendLayout();

            foreach (TabControlParts parts in allFormList)
            {
                parts.SuspendLayout();

                //parts.SetFont(Const.opt.outWardTab.TabFontName, Const.opt.outWardTab.TabFontSize);

                parts.ForeColor = Color.FromArgb(
                    Const.opt.outWardTab.TabForeColorRed,
                    Const.opt.outWardTab.TabForeColorGreen,
                    Const.opt.outWardTab.TabForeColorBlue);

                parts.BackColor = Color.FromArgb(
                    Const.opt.outWardTab.TabBackColorRed,
                    Const.opt.outWardTab.TabBackColorGreen,
                    Const.opt.outWardTab.TabBackColorBlue);

                parts.SelectedTabColorP = Color.FromArgb(
                    Const.opt.outWardTab.TabSelectedColorRed,
                    Const.opt.outWardTab.TabSelectedColorGreen,
                    Const.opt.outWardTab.TabSelectedColorBlue);

                parts.HotColorP = Color.FromArgb(
                    Const.opt.outWardTab.TabHotColorRed,
                    Const.opt.outWardTab.TabHotColorGreen,
                    Const.opt.outWardTab.TabHotColorBlue);

                parts.ResumeLayout();
            }
            Const.form.mainForm.Refresh();
            Const.mapForm.mapMainForm.Refresh();
            Const.form.mainForm.Update();
            Const.mapForm.mapMainForm.Update();

            Const.mapForm.mapMainForm.ResumeLayout();
            Const.form.mainForm.ResumeLayout();



            mutex.ReleaseMutex();
        }

        public static void ChangeOutWard(TabControlParts parts)
        {
            //Const.form.mainForm.Invoke((Action)(() => parts.SetFont(Const.opt.outWardTab.TabFontName, Const.opt.outWardTab.TabFontSize)));

            parts.ForeColor = Color.FromArgb(
                Const.opt.outWardTab.TabForeColorRed,
                Const.opt.outWardTab.TabForeColorGreen,
                Const.opt.outWardTab.TabForeColorBlue);

            parts.BackColor = Color.FromArgb(
                Const.opt.outWardTab.TabBackColorRed,
                Const.opt.outWardTab.TabBackColorGreen,
                Const.opt.outWardTab.TabBackColorBlue);

            parts.SelectedTabColorP = Color.FromArgb(
                Const.opt.outWardTab.TabSelectedColorRed,
                Const.opt.outWardTab.TabSelectedColorGreen,
                Const.opt.outWardTab.TabSelectedColorBlue);

            parts.HotColorP = Color.FromArgb(
                Const.opt.outWardTab.TabHotColorRed,
                Const.opt.outWardTab.TabHotColorGreen,
                Const.opt.outWardTab.TabHotColorBlue);

            //parts.TabGradient_TabPageSelectedTextColor = Color.FromArgb(
            //    Const.opt.outWardTab.TabForeColorRed,
            //    Const.opt.outWardTab.TabForeColorGreen,
            //    Const.opt.outWardTab.TabForeColorBlue);

            //タブの詮索されてない時の色はFormの文字色にする
            //parts.TabGradient_TabPageTextColor = Color.FromArgb(
            //    Const.opt.outWardForm.FormForeColorRed,
            //    Const.opt.outWardForm.FormForeColorGreen,
            //   Const.opt.outWardForm.FormForeColorBlue);

            //float scaledFloat = Scale.GetScaledFloat();

            //parts.SetFont(Const.form.mainForm.GetFontName(), scaledFloat);
        }
    }
}
