//ライセンス情報は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);

            datUrl = null;

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

            try
            {
                this.threadViewControls.threadListControlsEvent.autoReloadOff(this.datUrl);
                
                this.datUrl = "";
                this.threadName = "";

                this.ageonMutex.Terminate();

                int selectedIndex = Const.form.threadListTabControlParts.SelectedIndex;

                Const.form.threadListTabControlParts.SuspendLayout();

                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.threadViewControls.threadListControlsEvent.autoReloadOff(this.datUrl);

                this.datUrl = "";
                this.threadName = "";

                this.ageonMutex.Terminate();

                this.threadViewControls.RemoveAllControls();

                MainFormConst.threadListTabMain.RemoveView((ThreadViewCommon)this);
            }
            catch { ;}
            SelectTabIndex(1);
            SelectTabIndexFirst(0);
            /* ロック開放 */
            //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
                {
                    try
                    {
                        Const.form.threadListTabControlParts.SelectedIndex = selectIndex + 1;
                    }
                    catch { ;}
                    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));
                MainFormConst.boardListTabMain.view_list[Const.form.boardListTabControlParts.SelectedIndex].boardListControls.listViewParts.Invalidate();
            }
            catch
            {
                //Const.debugForm.OutPutDebug("ThreadList", ex); ;
            }

            //再描画をする
            
        }

        public override void ReWrite(DatInfo dat)
        {
            Point scrollpos = new Point(0,0);
            try
            {
                Const.form.mainForm.Invoke((Action)(() => scrollpos = this.threadViewControls.browserParts.GetScrollRectangle()));
                Thread.Sleep(1);
            }
            catch
            {
                Thread.Sleep(1);
            }
            try
            {
                ReWriteToBrowser(dat, scrollpos);
            }
            catch (System.Exception ex)
            {
                //Const.debugForm.OutPutDebug("ThreadList", ex);
            }
        }
    }
}