//ライセンス情報はAgeOnフォルダのLISENCEフォルダを参照してください

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;


namespace nispi
{
	class BoardListTabMain
	{
        public List<BoardViewCommon> view_list;

        //public BoardViewCommon selectedBoardView;

        public FavoriteBoardView favoriteBoardView;
        public ZenbunSearchView zenbunSearchView;
		public static MainForm form = null;
        public bool exit_app = false;
        public Mutex mutex = new Mutex(false, "boardtabwidget");
        public static int threadCount = 0;
        //private TabPageEx mouseMoveTabPage = null;
        //private TabPageEx currentMouseMoveTabPage = null;

        public BoardListTabMain()
		{
            view_list = new List<BoardViewCommon>();

            AddFavoriteView();

            AddZenbunSearchView();
			//AddView();
        }

		public void AddView()
		{
            //mutex.WaitOne();

            BoardViewCommon view = (BoardViewCommon)(new BoardView());
            view.boardListControls.tabPageExParts.Text = "";
			view_list.Add(view);

            //mutex.ReleaseMutex();
		}
        public void AddFavoriteView()
        {
            mutex.WaitOne();
            
            int threadLocalCount;

            threadCount += 1;
            threadLocalCount = threadCount;

            favoriteBoardView = new FavoriteBoardView();
            BoardViewCommon view = (BoardViewCommon)favoriteBoardView;
            view_list.Add(view);

            string boardUrl = "local://favorite/thread";

            view.boardListControls.tabPageExParts.Text =  "お気に入り";//BoardDataBase.GetName(boardUrl);
            view.board_title = "お気に入り";//BoardDataBase.GetName(board_url);
            view.threadLocalCount = threadLocalCount;
            view.boardID = Util.BoardUrlToBoardID(boardUrl);
            view.board_url = boardUrl;
            SetIcon(view);
            Const.form.boardListTabControlParts.SelectedTab = view.boardListControls.tabPageExParts;

            //
            tempParam tp2 = new tempParam();
            tp2.str = boardUrl;
            tp2.view = view;

            Thread t2 = new Thread(new ParameterizedThreadStart(worker));
            t2.IsBackground = true;
            t2.Start((object)tp2);
            //↑成功↓失敗
            //view.show_board(boardUrl);

            mutex.ReleaseMutex();
        }
        public void AddZenbunSearchView()
        {
            mutex.WaitOne();

            int threadLocalCount;

            threadCount += 1;
            threadLocalCount = threadCount;

            zenbunSearchView = new ZenbunSearchView();
            BoardViewCommon view = (BoardViewCommon)zenbunSearchView;
            view_list.Add(view);

            string boardUrl = "http://refind2ch.org/";

            view.boardListControls.tabPageExParts.Text = "スレタイ検索";//BoardDataBase.GetName(boardUrl);
            view.board_title = "2ch横断検索";//BoardDataBase.GetName(board_url);
            view.threadLocalCount = threadLocalCount;
            view.boardID = Util.BoardUrlToBoardID(boardUrl);
            view.board_url = boardUrl;
            SetIcon(view);
            //Const.form.boardListTabControlParts.SelectedTab = view.boardListControls.tabPageExParts;

            //
            tempParam tp2 = new tempParam();
            tp2.str = boardUrl;
            tp2.view = view;

            Thread t2 = new Thread(new ParameterizedThreadStart(worker));
            t2.IsBackground = true;
            t2.Start((object)tp2);
            //↑成功↓失敗
            //view.show_board(boardUrl);

            mutex.ReleaseMutex();
        }
        public void show_board(string board_url, bool popUp)
        {
            /* 動作しているＧＵＩへの書き込みを停止 */
            /* 整数をインクリメントする */
            mutex.WaitOne();
            int threadLocalCount;



            threadCount += 1;
            threadLocalCount = threadCount;

            foreach (BoardViewCommon view in view_list)
            {
                if ((view.board_url == null) || (view.board_url.Length == 0))
                {
                    view.boardListControls.tabPageExParts.Text = BoardDataBase.GetName(board_url) + " ";
                    view.board_title = BoardDataBase.GetName(board_url);

                    //お気に入りのデータベースも照合
                    if (view.board_title.Length == 0)
                    {
                        view.boardListControls.tabPageExParts.Text = FavoriteBoardDataBase.GetName(board_url, Util.BoardUrlToBoardTypeName(board_url)) + " ";
                        view.board_title = FavoriteBoardDataBase.GetName(board_url, Util.BoardUrlToBoardTypeName(board_url)) + " ";
                    }

                    view.threadLocalCount = threadLocalCount;
                    view.boardID = Util.BoardUrlToBoardID(board_url);
                    view.board_url = board_url;

                    SetIcon(view);

                    mutex.ReleaseMutex();

                    //タブページをセットするとコントロール全体がポップアップするため。
                    //Const.form.boardListTabControlParts.Enabled = false;
                    if (popUp == true)
                        Const.form.boardListTabControlParts.SelectedTab = view.boardListControls.tabPageExParts;
                    //Const.form.boardListTabControlParts.Enabled = true;

                    //view.SetData();
                    tempParam tp = new tempParam();
                    tp.str = board_url;
                    tp.view = view;

                    Thread t = new Thread(new ParameterizedThreadStart(worker));
                    t.IsBackground = true;
                    t.Start((object)tp);


                    return;
                }

                if (view.board_url.Equals(board_url) == true)
                {
                    view.threadLocalCount = threadLocalCount;

                    mutex.ReleaseMutex();

                    if (popUp == true)
                        Const.form.boardListTabControlParts.SelectedTab = view.boardListControls.tabPageExParts;

                    return;
                }
            }
            BoardViewCommon view2 = (BoardViewCommon)(new BoardView());

            view_list.Add(view2);

            view2.boardListControls.tabPageExParts.Text = BoardDataBase.GetName(board_url) + " ";
            view2.board_title = BoardDataBase.GetName(board_url);

            //お気に入りのデータベースも照合
            if (view2.board_title.Length == 0)
            {
                view2.boardListControls.tabPageExParts.Text = FavoriteBoardDataBase.GetName(board_url, Util.BoardUrlToBoardTypeName(board_url)) + " ";
                view2.board_title = FavoriteBoardDataBase.GetName(board_url, Util.BoardUrlToBoardTypeName(board_url)) + " ";
            }

            view2.threadLocalCount = threadLocalCount;
            view2.boardID = Util.BoardUrlToBoardID(board_url);
            view2.board_url = board_url;

            SetIcon(view2);

            mutex.ReleaseMutex();

            //Const.form.boardListTabControlParts.Enabled = false;
            if (popUp == true)
            {
                Const.form.boardListTabControlParts.SelectedTab = view2.boardListControls.tabPageExParts;
            }
            //Const.form.boardListTabControlParts.Enabled = true;

            tempParam tp2 = new tempParam();
            tp2.str = board_url;
            tp2.view = view2;

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



            return;
        }
        //delegate void SimpleMethod();

        private void worker(object o)
        {
            ((tempParam)o).view.show_board(((tempParam)o).str);            
        }

        //アドレスの種類によってアイコンを変える
        private void SetIcon(BoardViewCommon view2)
        {
            string type = Util.BoardUrlToBoardTypeName(view2.board_url);
            if (true == type.Equals("2chsc"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 0;
            else if (true == type.Equals("2ch"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 1;
            else if (true == type.Equals("favorite"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 2;
            else if (true == type.Equals("screening"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 3;
            else if (true == type.Equals("open2ch"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 4;
            else if (true == type.Equals("next2ch"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 5;

            else if (true == type.Equals("jikkyo"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 6;
            else if (true == type.Equals("blogban"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 7;
            else if (true == type.Equals("shizu"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 8;

            else if (true == type.Equals("vip2ch"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 9;
            else if (true == type.Equals("machibbs"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 10;
            else if (true == type.Equals("jbbs"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 11;
            else if (true == type.Equals("refind2ch"))
                view2.boardListControls.tabPageExParts.control.ImageIndex = 12;
            else
                view2.boardListControls.tabPageExParts.control.ImageIndex = -1;
        }

        public void remove_view(BoardViewCommon view)
        {
            mutex.WaitOne();

            if ((view_list.Count == 2) && (exit_app == false))
                AddView();

            view_list.Remove(view);

            mutex.ReleaseMutex();
        }
    }
}
