﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutobotServer
{
    public partial class Form1 : Form
    {

        private ServerManager m_ServerManager;
        public Form1()
        {
            InitializeComponent();
            m_ServerManager = new ServerManager(this);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_ServerManager.BindingServerList(dg_AutobotManager, dg_global_stats);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_ServerManager.KillAll();
        }

        #region Helpers
        // Append text of the given color.
        void AppendText(RichTextBox box, Color color, string text)
        {
            int iTextLength = text.Length;
            if (0 == iTextLength)
            {
                return;
            }
            int start = box.TextLength;
            box.AppendText(text);
            int end = box.TextLength;
            box.Select(start, end - start);
            box.SelectionColor = color;

            int iExcess = end - 10000;
            if (iExcess > 0)
            {
                box.Select(0, iExcess + 3);
                box.SelectedText = "...";
            }

            box.Select(box.Text.Length - 1, 0);
            box.ScrollToCaret();
        }
        #endregion//Helpers

        public void IncomingChatMsg(String a_sMsg)
        {
            AppendText(rt_chat_log, Color.Blue, a_sMsg + "\n");
        }

        public void IncomingResponse(String a_sMsg)
        {
            AppendText(rt_response_output, Color.Brown, a_sMsg + "\n");
        }

        private void btn_send_cmd_selected_Click(object sender, EventArgs e)
        {
            AppendText(rt_response_output, Color.Blue, rt_Command.Text);
            foreach (DataGridViewRow row in dg_AutobotManager.SelectedRows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                AppendText(rt_response_output, Color.Blue, "[" + oInst.ShortClientID + "]");
                oInst.SendMessage(rt_Command.Text);
            }
            AppendText(rt_response_output, Color.Blue, "\n");
        }

        private void btn_send_cmd_all_Click(object sender, EventArgs e)
        {
            AppendText(rt_response_output, Color.Blue, rt_Command.Text + "[ALL]\n");
            foreach (DataGridViewRow row in dg_AutobotManager.Rows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                oInst.SendMessage(rt_Command.Text);
            }
        }

        private void btn_send_chat_Selected_Click(object sender, EventArgs e)
        {
            String sDateTime = DateTime.Now.ToString() + " : ";
            AppendText(rt_chat_log, Color.Brown, sDateTime);
            AppendText(rt_chat_log, Color.Black, rt_chat_send.Text + "\n");
            string sChat = "chat " + sDateTime + rt_chat_send.Text;
            rt_chat_send.Text = "";

            foreach (DataGridViewRow row in dg_AutobotManager.SelectedRows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                oInst.SendMessage(sChat);
            }
        }

        private void btn_send_chat_all_Click(object sender, EventArgs e)
        {
            String sDateTime = DateTime.Now.ToString() + " : ";
            AppendText(rt_chat_log, Color.Brown, sDateTime);
            AppendText(rt_chat_log, Color.Black, rt_chat_send.Text + "\n");
            string sChat = "chat " + "[all]" +sDateTime + rt_chat_send.Text;
            rt_chat_send.Text = "";

            foreach (DataGridViewRow row in dg_AutobotManager.Rows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                oInst.SendMessage(sChat);
            }
        }

        private void ping_timer_Tick(object sender, EventArgs e)
        {
            List<ServerInstance> oList = new List<ServerInstance>();

            foreach (DataGridViewRow row in dg_AutobotManager.Rows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                if(!oInst.TryPing())
                {
                    oList.Add(oInst);
                }
            }
            foreach(var oInst in oList)
            {
                oInst.Cancel();
            }
        }

        private void dg_AutobotManager_SelectionChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dg_AutobotManager.SelectedRows)
            {
                ServerInstance oInst = row.DataBoundItem as ServerInstance;
                oInst.BindUi(dg_per_manager);
                return;
            }
            dg_per_manager.DataSource = null;
        }

        private void btn_Refresh_global_stats_Click(object sender, EventArgs e)
        {
            dg_global_stats.SuspendLayout();
            m_ServerManager.RefreshStats();
            dg_global_stats.ResumeLayout();

        }

        public void SuspendLayoutForAutoM(ServerInstance a_Server)
        {
            if(dg_per_manager.DataSource == a_Server.m_lStats)
            {
                dg_per_manager.SuspendLayout();
            }
        }

        public void ResumeLayoutForAutoM(ServerInstance a_Server)
        {
            if (dg_per_manager.DataSource == a_Server.m_lStats)
            {
                dg_per_manager.ResumeLayout();
            }
        }

    }
}
