using System;
using Server.DB.Redis;
using Server.Data;
using GameData;
using System.Windows.Forms;
using System.Text;
using Server;
using MrWu.Debug;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server.MQ;
using Server.Pack;
using MrWu.Basic;
using System.Threading;
using GameDAL.Game;
using Server.DB.Sql;

namespace GlobalSever.Form
{
    /// <summary>
    /// 通用table
    /// </summary>
    public partial class GeneralTable : ServerTable
    {
        /// <summary>
        /// 选项名称
        /// </summary>
        public override string tableText => "通用";

        /// <summary>
        /// 服务器信息
        /// </summary>
        public ServerInfo serverInfo
        {
            get { return GlobalManager.instance.serverinfo; }
        }

        /// <summary>
        /// 通用table
        /// </summary>
        public GeneralTable(ServerProxy server)
        {
            this.server = server;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (server == null)
                {
                    Debug.Error("server is null");
                    return;
                }

                if (!server.IsRun)
                    return;

                listBox1.Items.Clear();
                int userid;
                if (!int.TryParse(textBox1.Text, out userid))
                {
                    return;
                }

                playerData pl = PlayerFun.GetPlayerData(userid);

                if (pl != null)
                {
                    //PlayerLockInfo lockInfo = PlayerFun.GetPlayerLockInfo(userid);

                    int weight = -15;
                    listBox1.Items.Add(BaseFun.Align("userid", pl.userid, weight));
                    listBox1.Items.Add(BaseFun.Align("昵称", pl.nickname, weight));
                    listBox1.Items.Add(BaseFun.Align("地区", pl.area, weight));
                    listBox1.Items.Add(BaseFun.Align("性别", pl.sex, weight));
                    listBox1.Items.Add(BaseFun.Align("经验", pl.exp, weight));
                    listBox1.Items.Add(BaseFun.Align("用户类型", pl.userType, weight));

                    listBox1.Items.Add(BaseFun.Align("所在游戏", pl.gameid, weight));
                    listBox1.Items.Add(BaseFun.Align("城堡", pl.roomid, weight));
                    listBox1.Items.Add(BaseFun.Align("厅", pl.tingid, weight));
                    listBox1.Items.Add(BaseFun.Align("桌子", pl.deskid, weight));
                    listBox1.Items.Add(BaseFun.Align("座位", pl.seat, weight));
                    listBox1.Items.Add(BaseFun.Align("是否离线", pl.outline, weight));

                    listBox1.Items.Add(BaseFun.Align("ip", pl.ip, weight));
                    listBox1.Items.Add(BaseFun.Align("状态:", pl.state, weight));
                    listBox1.Items.Add(BaseFun.Align("朋友房", pl.friendRoomNum, weight));
                    listBox1.Items.Add(BaseFun.Align("唯一吗", pl.friendWeiYiMa, weight));
                    listBox1.Items.Add(BaseFun.Align("头像", pl.avatar, weight));
                    listBox1.Items.Add(BaseFun.Align("游戏币", pl.money, weight));
                    listBox1.Items.Add(BaseFun.Align("奖券", pl.coupon, weight));
                    listBox1.Items.Add(BaseFun.Align("房卡", pl.fangka, weight));
                    listBox1.Items.Add(BaseFun.Align("金币", pl.gold, weight));

                    //listBox1.Items.Add(BaseFun.Align("发型", pl.hairStyle, weight));
                    //listBox1.Items.Add(BaseFun.Align("脸型", pl.faceStyle, weight));
                    //listBox1.Items.Add(BaseFun.Align("衣服", pl.clothes, weight));
                    //listBox1.Items.Add(BaseFun.Align("宠物", pl.pet, weight));
                }
                else
                {
                    listBox1.Items.Add("玩家加载失败,或者玩家不在redis缓存中!");
                }
            }
            catch (Exception ex)
            {
                Debug.Error("错误:" + ex.ToString());
            }
        }

        private void SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lstBox = sender as ListBox;

            if (lstBox != null && lstBox.SelectedItem != null)
                Clipboard.SetDataObject(lstBox.SelectedItem);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RefreshServers();
        }

        /// <summary>
        /// 在线的服务器
        /// </summary>
        private void RefreshServers()
        {
            if (!isStart || !this.Visible)
                return;

            if (server == null)
            {
                Debug.Error("server is null");
                return;
            }

            if (!server.IsRun)
                return;

            textBox2.Text = serverInfo.ToString();

            List<ServerNode> sers = ServerHeart.instanece.GetAllOnline();

            int len = listBox2.Items.Count;
            for (int i = len - 1; i >= 0; i--)
            {
                object item = listBox2.Items[i];
                bool isContains = false;
                foreach (var ser in sers)
                {
                    if (ser.Equals(item))
                    {
                        isContains = true;
                        break;
                    }
                }

                if (!isContains)
                    listBox2.Items.RemoveAt(i);
            }

            len = sers.Count;
            for (int i = 0; i < len; i++)
            {
                bool isContains = false;
                foreach (var item in listBox2.Items)
                {
                    if (sers[i].Equals(item))
                    {
                        isContains = true;
                        break;
                    }
                }

                if (!isContains)
                    listBox2.Items.Add(sers[i]);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (server == null)
            {
                Debug.Error("server is null");
                return;
            }

            if (!server.IsRun)
                return;
            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.Ping;
            if (listBox2.SelectedItem == null)
            {
                int len = listBox2.Items.Count;
                for (int i = 0; i < len; i++)
                {
                    ServerNode node = listBox2.Items[i] as ServerNode;
                    if (node != null)
                    {
                        GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, GlobalManager.instance.myUtil),
                            true);
                    }
                    else
                    {
                        Debug.Error("不是ServerNode:" + listBox2.Items[i].ToString());
                    }
                }
            }
            else
            {
                ServerNode node = listBox2.SelectedItem as ServerNode;
                if (node != null)
                    GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, GlobalManager.instance.myUtil),
                        true);
                else
                    Debug.Error("不是ServerNode:" + listBox2.SelectedItem.ToString());
            }
            head.Dispose();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;

            if (checkBox.Checked)
            {
                GlobalMQ.instance.AddFailConsumer();
            }
            else
            {
                GlobalMQ.instance.RemoveFailConsumer();
            }
        }

        /// <summary>
        /// 获取选中的节点
        /// </summary>
        /// <returns></returns>
        private ServerNode GetServerNode()
        {
            return listBox2.SelectedItem as ServerNode;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            //发给指定节点
            var nowdt = GetServerNode();

            if (nowdt == null)
            {
                MessageBox.Show("请选择目标服务器！");
                return;
            }

            PackHead head = PackHead.Create();
            //模块测试
            head.packlx = ServerPackAgreement.PackTest;
            head.otherInt = new int[] { 0 /*单发测试!*/ };

            //指定发送给某个模块
            GlobalMQ.instance.DeliveryOne(nowdt, GlobalMQ.CreateMessage(head, null), true);
            head.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //发给任意节点
            var nowdt = GetServerNode();

            if (nowdt == null)
            {
                MessageBox.Show("请选择目标服务器!");
                return;
            }

            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.PackTest;
            head.otherInt = new int[] { 1 /*发给任意模块*/ };

            GlobalMQ.instance.DeliveryEveryOne(nowdt.id, GlobalMQ.CreateMessage(head, null), true);
            head.Dispose();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //群发给某个集群
            var nowdt = GetServerNode();

            if (nowdt == null)
            {
                MessageBox.Show("请选择目标服务器!");
                return;
            }

            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.PackTest;
            head.otherInt = new int[] { 2 /* 群发给某个集群*/ };

            GlobalMQ.instance.DeliveryAll(nowdt.id, GlobalMQ.CreateMessage(head, null), true);
            head.Dispose();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //群发给所有服务器
            var nowdt = GetServerNode();

            if (nowdt == null)
            {
                MessageBox.Show("请选择目标服务器!");
                return;
            }

            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.PackTest;
            head.otherInt = new int[] { 3 /*群发给所有服务器*/ };

            GlobalMQ.instance.DeliveryAll(GlobalMQ.CreateMessage(head, null), true);
            head.Dispose();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //RPC
            var nowdt = GetServerNode();

            if (nowdt == null)
            {
                MessageBox.Show("请选择目标服务器!");
                return;
            }

            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.PackTest;
            head.otherInt = new int[] { 4 /*群发给所有服务器*/ };

            var result = GlobalMQ.instance.Call(nowdt, head);
            Debug.Log("RPC返回结果:" + result);
            head.Dispose();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (server == null)
            {
                Debug.Error("server is null");
                return;
            }

            if (!server.IsRun)
                return;
        }

        private void button8_Click_1(object sender, EventArgs e)
        {
        }

        private void button8_Click_2(object sender, EventArgs e)
        {
            Thread thread = new Thread(
                () =>
                {
                    string str = "";

                    for (int i = 0; i < 2000; i++)
                    {
                        str += "0";
                    }

                    //Debug.Log("111");
                    for (int j = 0; j < 10000000; j++)
                    {
                        string tmp = str + BaseFun.random.Next(0, 100);

                        PackHead head = PackHead.Create();
                        head.packlx = -29; // ServerPackAgreement.Ping;
                        if (listBox2.SelectedItem == null)
                        {
                            int len = listBox2.Items.Count;
                            for (int i = 0; i < len; i++)
                            {
                                ServerNode node = listBox2.Items[i] as ServerNode;
                                if (node != null)
                                {
                                    GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, tmp), true);
                                }
                                else
                                {
                                    Debug.Error("不是ServerNode:" + listBox2.Items[i].ToString());
                                }
                            }
                        }
                        else
                        {
                            ServerNode node = listBox2.SelectedItem as ServerNode;
                            if (node != null)
                                GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, tmp),
                                    true); //, GlobalManager.instane.myUtil));
                            else
                                Debug.Error("不是ServerNode:" + listBox2.SelectedItem.ToString());
                        }
                        head.Dispose();
                        Thread.Sleep(1);
                    }
                }); //.Start();
            thread.Start();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void button8_Click_3(object sender, EventArgs e)
        {
            if (server == null)
            {
                Debug.Error("server is null");
                return;
            }

            if (!server.IsRun)
                return;
            PackHead head = PackHead.Create();
            head.packlx = ServerPackAgreement.KillMe;
            if (listBox2.SelectedItem == null)
            {
                int len = listBox2.Items.Count;
                for (int i = 0; i < len; i++)
                {
                    ServerNode node = listBox2.Items[i] as ServerNode;
                    if (node != null)
                    {
                        GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, GlobalManager.instance.myUtil),
                            true);
                    }
                    else
                    {
                        Debug.Error("不是ServerNode:" + listBox2.Items[i].ToString());
                    }
                }
            }
            else
            {
                ServerNode node = listBox2.SelectedItem as ServerNode;
                if (node != null)
                    GlobalMQ.instance.DeliveryOne(node, GlobalMQ.CreateMessage(head, GlobalManager.instance.myUtil),
                        true);
                else
                    Debug.Error("不是ServerNode:" + listBox2.SelectedItem.ToString());
            }
            head.Dispose();
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
        }
        
        
    }
}

// update 