//ライセンス情報はAgeOnフォルダのLISENCEフォルダを参照してください
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using mshtml;
using System.Threading;
using MecInfoDll;

namespace nispi
{
    class ThreadView:ThreadViewCommon
    {
        public ThreadView():base()
        {
            this.threadViewControls = new ThreadListControls(this as ThreadViewCommon);

            //タブコントロールにセット
            Const.form.threadListTabControlParts.AddControl(this.threadViewControls.tabPageExParts);
            //Const.form.threadListTabControlParts.Update();//.boardListTabControlParts.Update();

            datUrl = null;

            shiori = new Shiori(this.threadViewControls.browserParts);
        }
        public override void dispoose()
        {
            /* ロック */
            //if (this.ageonMutex.WaitOne() == false)
            //{
            //    this.ageonMutex.ReleaseMutex();
            //    return;
            //}

            try
            {
                this.ageonMutex.Terminate();

                int selectedIndex = Const.form.threadListTabControlParts.SelectedIndex;

                //SelectTabIndex(selectedIndex);
                Const.form.threadListTabControlParts.SuspendLayout();

                //SelectTabIndexFirst(selectedIndex);

                Const.form.threadListTabControlParts.ControlsRemove(threadViewControls.tabPageExParts);

                Const.form.mainForm.Invoke((Action)(() => this.threadViewControls.RemoveAllControls()));

                MainFormConst.threadListTabMain.RemoveView((ThreadViewCommon)this);

                SelectTabIndex(selectedIndex);

                Const.form.threadListTabControlParts.ResumeLayout();
            }
            catch { ;}

            /* ロック開放 */
            //this.ageonMutex.ReleaseMutex();
        }

        public override void dispooseAll()
        {
            /* ロック */
            //if (this.ageonMutex.WaitOne() == false)
            //{
            //    this.ageonMutex.ReleaseMutex();
            //    return;
            //}
            try
            {
                this.ageonMutex.Terminate();

                //Const.form.threadListTabControlParts.ControlsRemove(threadViewControls.tabPageExParts);//.RemoveAllCtrl();//

                this.threadViewControls.RemoveAllControls();

                MainFormConst.threadListTabMain.RemoveView((ThreadViewCommon)this);
            }
            catch { ;}

            /* ロック開放 */
            //this.ageonMutex.ReleaseMutex();
        }

        public void SelectTabIndexFirst(int selectIndex)
        {
            try
            {
                Const.form.threadListTabControlParts.SelectedIndex = 0;
            }
            catch { ;}
        }

        public void SelectTabIndex(int selectIndex)
        {
            try
            {
                if (selectIndex >= 0)
                {
                    /*if (selectIndex == 0)
                        Const.form.threadListTabControlParts.SelectedIndex = 0;
                    else if (selectIndex == 1)
                        Const.form.threadListTabControlParts.SelectedIndex = 1;
                    else if (selectIndex == 2)
                        Const.form.threadListTabControlParts.SelectedIndex = 2;
                    else if (selectIndex == 3)
                        Const.form.threadListTabControlParts.SelectedIndex = 2;
                    //一番右の時にはマイナス１
                    else */if (selectIndex == Const.form.threadListTabControlParts.Count)
                        Const.form.threadListTabControlParts.SelectedIndex = selectIndex - 1;
                    //それ以外では、前の選択されていたタブを選択
                    else
                        Const.form.threadListTabControlParts.SelectedIndex = selectIndex;

                    while (Const.form.threadListTabControlParts.control.SelectedTab.Text.Length == 0)
                    {
                        if (Const.form.threadListTabControlParts.SelectedIndex < 1)
                            break;
                        Const.form.threadListTabControlParts.SelectedIndex -= 1;
                    }
                    

                }
            }
            catch { ;}
        }

        public override void write(DatInfo dat)
        {
            try
            {
                ReWriteToBrowser(dat, new Point(0,0));
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("ThreadList", ex); ;
            }

            //再描画をする
            MainFormConst.boardListTabMain.view_list[Const.form.boardListTabControlParts.SelectedIndex].boardListControls.listViewParts.Invalidate();
        }

        public override void ReWrite(DatInfo dat)
        {
            Point scrollpos = this.threadViewControls.browserParts.GetScrollRectangle();

            try
            {
                ReWriteToBrowser(dat, scrollpos);
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("ThreadList", ex);
            }
        }
    }
}