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

namespace nispi
{
	class ThreadListTabMain
	{
        public List<ThreadViewCommon> viewList;
        public bool exit_app = false;
        public Mutex mutex = new Mutex(false, "ThreadListTabMain");

        public int threadCount = 0;
        public int num = 0;

        public ThreadListTabMain()
		{
            viewList = new List<ThreadViewCommon>();
            AddView();
            AddView();
            AddView();

            Const.form.threadListTabControlParts.SelectedIndex = 0;

            //ThreadView.mutex = mutex;
		}


		public void AddView()
		{
            //mutex.WaitOne();
            ThreadViewCommon view = (ThreadViewCommon)(new ThreadView());
            viewList.Add(view);
            view.threadViewControls.tabPageExParts.Text = "";
            view.threadViewControls.tabPageExParts.ToolTipText = "";
            //mutex.ReleaseMutex();
			//タブを加える
		}

        public void ShowThread(string dat_url, string thread_name)
		{
            /* 動作しているＧＵＩへの書き込みを停止 */
            /* 整数をインクリメントする */
            int threadLocalCount;
            int count = 0;
            mutex.WaitOne();
            threadCount += 1;
            threadLocalCount = threadCount;
            mutex.ReleaseMutex();

            mutex.WaitOne();

            foreach (ThreadViewCommon view in viewList)
			{
                if ((view.datUrl == null) || view.datUrl.Length == 0)
                {
                    //Const.form.mainForm.Invoke((Action)(() => view.threadViewControls.tabPageExParts.Text = thread_name));
                    //Const.form.mainForm.Invoke((Action)(() => view.threadViewControls.tabPageExParts.ToolTipText = thread_name));
                    view.threadViewControls.tabPageExParts.Text = thread_name;
                    view.threadViewControls.tabPageExParts.ToolTipText = thread_name;
                    //キャプション関係
                    //Const.form.mainForm.Invoke((Action)(() => Const.form.threadListTabControlParts.Refresh()));


                    view.datUrl = dat_url;
                    view.threadName = thread_name;
                    view.threadLocalCount = threadLocalCount;

                    SetIcon(view);

                    mutex.ReleaseMutex();          
       
                    Const.form.threadListTabControlParts.Enabled = false;
                    Const.form.threadListTabControlParts.SelectedTab = view.threadViewControls.tabPageExParts;
                    //Const.form.threadListTabControlParts.Refresh();
                    Const.form.threadListTabControlParts.Enabled = true;
                    //Const.form.threadListTabControlParts.SelectedIndex = count;
                    //MainFormConst.SetUrl(Util.dat_url_to_thread_url(dat_url));

                    tempParamThread tp = new tempParamThread(view,dat_url);
                    
                    Thread t = new Thread(new ParameterizedThreadStart(worker));
                    t.IsBackground = true;
                    t.Start((object)tp);/**/



                    //view.ShowThread(dat_url);
                    
                    return;
                }
                else if (view.datUrl.Equals(dat_url) == true)
                {
                    view.threadLocalCount = threadLocalCount;

                    mutex.ReleaseMutex();

                    Const.form.threadListTabControlParts.Enabled = false;
                    Const.form.threadListTabControlParts.SelectedTab = view.threadViewControls.tabPageExParts;
                    Const.form.threadListTabControlParts.Enabled = true;


                    return;
                }
                count += 1;
			}

			ThreadViewCommon view2 = (ThreadViewCommon)(new ThreadView());
            

            viewList.Add(view2);

            //Const.form.mainForm.Invoke((Action)(() => view2.threadViewControls.tabPageExParts.Text = thread_name));
            //Const.form.mainForm.Invoke((Action)(() => view2.threadViewControls.tabPageExParts.ToolTipText = thread_name));
            view2.threadViewControls.tabPageExParts.Text = thread_name;
            view2.threadViewControls.tabPageExParts.ToolTipText = thread_name;

            //↓ここで代入せず飛ばしたスレッドでurlをセットしていたために高速での連続表示時に上書きされてしまっていた。
            view2.datUrl = dat_url;
            view2.threadName = thread_name;
            view2.threadLocalCount = threadLocalCount;


            SetIcon(view2);

            mutex.ReleaseMutex();

            //Const.form.mainForm.BeginInvoke((Action)(() => Const.form.threadListTabControlParts.SelectedTab = view2.threadViewControls.tabPageExParts));
            Const.form.threadListTabControlParts.Enabled = false;
            Const.form.threadListTabControlParts.SelectedTab = view2.threadViewControls.tabPageExParts;
            Const.form.threadListTabControlParts.Enabled = true;
            //MainFormConst.SetUrl(Util.dat_url_to_thread_url(dat_url));

            tempParamThread tp2 = new tempParamThread(view2, dat_url);

            Thread t2 = new Thread(new ParameterizedThreadStart(worker));
            t2.IsBackground = true;
            t2.Start((object)tp2);/**/
            


            //view2.ShowThread(dat_url);


            return;
		}
        private void worker(object o)
        {
            ((tempParamThread)o).view.ShowThread(((tempParamThread)o).str);
        }
        
        /*public void remove_view(int num)
		{
            mutex.WaitOne();
            if ((viewList.Count == 1) && (Const.form != null))
				add_view();

            viewList.RemoveAt(num);
            mutex.ReleaseMutex();
		}*/
        private void SetIcon(ThreadViewCommon view)
        {

            string type = Util.BoardUrlToBoardTypeName(view.datUrl);
            if (true == type.Equals("2chsc"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 0;
            else if (true == type.Equals("2ch"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 1;
            else if (true == type.Equals("open2ch"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 4;
            else if (true == type.Equals("next2ch"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 5;
            else if (true == type.Equals("jikkyo"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 6;
            else if (true == type.Equals("blogban"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 7;
            else if (true == type.Equals("shizu"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 8;

            else if (true == type.Equals("vip2ch"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 9;
            else if (true == type.Equals("machibbs"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 10;
            else if (true == type.Equals("jbbs"))
                view.threadViewControls.tabPageExParts.control.ImageIndex = 11;
            else
                view.threadViewControls.tabPageExParts.control.ImageIndex = -1;
        }

        public void RemoveView(ThreadViewCommon view)
        {
            mutex.WaitOne();
            if ((viewList.Count == 3) && (exit_app == false))
                AddView();

            viewList.Remove(view);
            mutex.ReleaseMutex();
        }
	}
}
