using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Server;
using Game.Player;

namespace GlobalSever.Form
{

    /// <summary>
    /// 
    /// </summary>
    public partial class GameControl : ServerTable
    {

        /// <summary>
        /// 
        /// </summary>
        public override string tableText => "游戏";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="server"></param>
        public GameControl(ServerProxy server) {
            this.server = server;
            InitializeComponent();
            
            gameinfo = server.Factory.Produce("GameInfo") as GameSerInfo;
            castleSerInfo = server.Factory.Produce("castleInfo") as CastleSerInfo;
            tingSerInfo = server.Factory.Produce("tingInfo") as TingSerInfo;
            deskSerInfo = server.Factory.Produce("deskInfo") as DeskSerInfo;
        }

        /// <summary>
        /// 游戏信息
        /// </summary>
        protected GameSerInfo gameinfo;

        /// <summary>
        /// 城堡信息
        /// </summary>
        protected CastleSerInfo castleSerInfo;

        /// <summary>
        /// 厅信息
        /// </summary>
        protected TingSerInfo tingSerInfo;

        /// <summary>
        /// 桌子信息
        /// </summary>
        protected DeskSerInfo deskSerInfo;

        /// <summary>
        /// 更新游戏信息
        /// </summary>
        protected void UpdateGameInfo() {
            if (server == null)
                return;
            gameinfo.Update();
            textBox1.Text = gameinfo.ToString();
        }

        private void button1_Click(object sender, EventArgs e) {
            UpdateGameInfo();
        }

        private void GameControl_Validated(object sender, EventArgs e) {
            UpdateGameInfo();
        }

        /// <summary>
        /// 更新城堡信息
        /// </summary>
        protected void UpdateCastle() {
            int id = 0;
            int.TryParse(textBox3.Text, out id);
            if (server == null)
                return;
            castleSerInfo.Update(id);
            textBox2.Text = castleSerInfo.ToString();
        }

        private void button2_Click(object sender, EventArgs e) {
            UpdateCastle();
        }

        /// <summary>
        /// 更新厅信息
        /// </summary>
        protected void UpdateTing() {
            int id = 0;
            int.TryParse(textBox4.Text, out id);
            if (server == null)
                return;
            tingSerInfo.Update(id);
            textBox5.Text = tingSerInfo.ToString();
        }

        private void button3_Click(object sender, EventArgs e) {
            UpdateTing();
        }

        /// <summary>
        /// 更新桌子信息
        /// </summary>
        protected void UpdateDesk() {
            int tingid = 0;
            int deskid = 0;
            int.TryParse(textBox6.Text, out tingid);
            int.TryParse(textBox8.Text, out deskid);
            if (server == null)
                return;
            deskSerInfo.Update(tingid, deskid);
            textBox7.Text = deskSerInfo.ToString();
        }

        private void textBox8_TextChanged(object sender, EventArgs e) {

        }

        private void button4_Click(object sender, EventArgs e) {
            UpdateDesk();
        }

        /// <summary>
        /// 更新玩家信息
        /// </summary>
        protected void UpdatePlayer() {
            try
            {
                int userid = 0;
                if (!int.TryParse(textBox10.Text, out userid))  //2020/02/21 加了判断
                    return;
                if (server == null)
                    return;
                var pl = PlayerCollection.Instance.FindPlayerByUserId(userid);
                if (pl != null)
                    textBox11.Text = pl.ToString();
                else
                    textBox11.Text = "无此玩家!";
            }
            catch (Exception e){
                textBox11.Text = "更新玩家信息出错，"+e.Message;
            }
           
        }

        private void button5_Click(object sender, EventArgs e) {
            UpdatePlayer();
        }
    }
}
