using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MrWu.Debug;
using System.Threading;
using Server.MQ;
using System.IO;
using System.Net;
using Server;
using Server.DB.Redis;

namespace GlobalSever.Form
{
    /// <summary>
    /// 服务器窗体
    /// </summary>
    public partial class ServerForm : System.Windows.Forms.Form
    {

        /// <summary>
        /// 所有的页面
        /// </summary>
        private readonly List<TabPage> pages = new List<TabPage>();

        /// <summary>
        /// 选项卡
        /// </summary>
        private readonly List<ServerTable> tabs = new List<ServerTable>();


        private string GetIconPath
        {
            get
            {
                return DirectoryManager.GetIconPath();
            }
        }

        /// <summary>
        /// 服务器
        /// </summary>
        ServerProxy server;

        private ServerForm()
        {
        }

        /// <summary>
        ///  构造
        /// </summary>
        public ServerForm(ServerProxy server, string[] args = null)
        {
            InitializeComponent();
            this.server = server;
            tabs.Add(new logTable(server));
            tabs.Add(new GeneralTable(server));
            #if DEBUG
            tabs.Add(new GameInfoTable(server));
            #endif
            
            //tabs.Add(new TestTab(server));
            if (args != null && args.Length > 0)
            {
                if ("LaunchByWatchdog".Equals(args[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    AutoRun = true;
                }

                if ("AutoRun".Equals(args[0]))
                {
                    AutoRun = true;
                }
            }
        }

        bool AutoRun = false;

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (server.Start(this))
            {
                button1.Text = "已启动";
                button1.Enabled = false;
                timer1.Enabled = true;
                foreach (var tb in tabs)
                {
                    tb.Start();
                }
            }
        }

        private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (GlobalManager.Factory != null && GlobalManager.instance.IsCanWar && GlobalManager.instance.myUtil.id>=11)
            {
                Debug.Error($"请先禁止开战，在关闭服务器！！！ {GlobalManager.instance.IsCanWar}");
                e.Cancel = true;
                return;
            }
            if (!server.IsRun)
                return;

            bool isQuit = server.isKill;
            if (!isQuit)
            {
                DialogResult result = MessageBox.Show("确认关闭吗？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                isQuit = result == DialogResult.OK;
            }

            if (isQuit)
            {
                timer1.Enabled = false;
                foreach (var tb in tabs)
                {
                    tb.Stop();
                }
                server.Stop();
                this.Text = "关闭中...";
                while (server.IsRun)
                {
                    Thread.Sleep(10);
                }
                LogPool.Flush();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Text = server.Title;
            
            if (server.isKill)
            {
                this.checkBox1.Checked = true;
                Close();
            }
        }

        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServerForm_Load(object sender, EventArgs e)
        {
            if (File.Exists(GetIconPath))
            {
                this.Icon = new Icon(GetIconPath);
            }

            var serTables = server.GetServerTable();

            if (serTables != null)
            {
                tabs.AddRange(serTables);
            }

            int len = tabs.Count;
            for (int i = 0; i < len; i++)
            {
                TabPage page = new TabPage();
                this.tabControl1.Controls.Add(page);

                page.Controls.Add(tabs[i]);
                page.Location = new Point(4, 22);
                page.Padding = new Padding(3);
                page.Size = new Size(921, 389);
                page.TabIndex = this.tabControl1.TabCount;
                page.Text = tabs[i].tableText;
                page.UseVisualStyleBackColor = true;

                tabs[i].Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));

                tabs[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                tabs[i].Location = new System.Drawing.Point(10, 6);
                tabs[i].Name = "table" + i;
                tabs[i].Size = new System.Drawing.Size(900, 376);
                tabs[i].TabIndex = 0;

                pages.Add(page);
            }

            if (AutoRun)
            {
                button1_Click(null, null);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void MsgLogCheck(object sender, EventArgs e)
        {
            GlobalMQ.openMsgLog = !GlobalMQ.openMsgLog;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            GlobalMQ.instance.ConnectionBlocked();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            GlobalMQ.instance.ConnectionUnBlocked();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 开始循环
        /// </summary>
        public void StartLoop()
        {
            timer2.Enabled = true;
        }

        /// <summary>
        /// 关闭循环
        /// </summary>
        public void StopLoop()
        {
            timer2.Enabled = true;
        }

        /// <summary>
        /// 循环事件
        /// </summary>
        public event Action Loop;

        private void Timer2_Tick(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            server.SetIsCanWar(((CheckBox)sender).Checked);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //server.SetIsCanLogin(((CheckBox)sender).Checked);
            //屏蔽禁止登录，禁止登录会影响自动重连
        }
    }
}
