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

namespace WinformSocketServer
{
    public partial class MySocketForm : Form
    {
        public string hostIp;
        public int portNum;
        public bool isServer = false;
        public bool isClient = false;

        SocketServer socketServer;
        SocketClient socketClient;

        public System.Action<string> onReceive;
        uint txCount = 0;
        uint rxCount1 = 0;
        uint rxCount2 = 0;

        public MySocketForm()
        {
            InitializeComponent();
            this.socketServer = new SocketServer();
            Console.WriteLine("Form : {0}", this);
            this.socketServer.InitForm(this);
            this.socketClient = new SocketClient();
            this.socketClient.InitForm(this);

            this.textBox_hostIp.Text = "127.0.0.1";
            this.textBox_portNum.Text = "5000";
        }

        public void Receive(string data)
        {
            if (this.isServer)
            {
                if (data == "1")
                {
                    this.textBox_count1.Invoke(new Action(() => this.textBox_count1.Text = this.rxCount1++.ToString()));
                }
                else if (data == "2")
                {
                    this.textBox_count1.Invoke(new Action(() => this.textBox_count1.Text = this.rxCount2++.ToString()));
                }
            }
            else if(this.isClient)
            {
                if(data == "1")
                {
                    Console.WriteLine("in data, rx : {0}", this.textBox_sendMsg.Text);
                    this.socketClient.SendMessage(this.textBox_sendMsg.Text);
                }
            }
        }
        private void btnServerOn_Click(object sender, EventArgs e)
        {
            if (this.isServer == false)
            {
                try
                {
                    this.hostIp = this.textBox_hostIp.Text;
                    this.portNum = Int32.Parse(this.textBox_portNum.Text);

                    //Server
                    if (socketServer.StartServer(this.hostIp, this.portNum))
                    {
                        this.isServer = true;
                        this.btnServerOn.Text = "Server Off";
                        this.btnClientOn.Enabled = false;
                        this.textBox_hostIp.Enabled = false;
                        this.textBox_portNum.Enabled = false;
                    }
                }
                catch
                {
                    Console.WriteLine("bind err");
                }
            }
            else
            {
                //Server Off
                this.isServer = false;
                this.btnServerOn.Text = "Server On";
                this.btnClientOn.Enabled = true;
                this.textBox_hostIp.Enabled = true;
                this.textBox_portNum.Enabled = true;
                this.socketServer.CloseServer();
            }
        }

        private void btnClientOn_Click(object sender, EventArgs e)
        {
            if (this.isClient == false)
            {
                //Client On
                this.hostIp = this.textBox_hostIp.Text;
                this.portNum = Int32.Parse(this.textBox_portNum.Text);
                if (this.socketClient.Connect(this.hostIp, this.portNum))
                {
                    this.isClient = true;
                    this.btnClientOn.Text = "Client Off";
                    this.btnServerOn.Enabled = false;
                    this.textBox_hostIp.Enabled = false;
                    this.textBox_portNum.Enabled = false;
                }
            }
            else
            {
                //Client Off
                this.socketClient.Disconnect();
                this.isClient = false;
                this.btnClientOn.Text = "Client On";
                this.btnServerOn.Enabled = true;
                this.textBox_hostIp.Enabled = true;
                this.textBox_portNum.Enabled = true;
            }
        }

        private bool isSend;
        private void btnSend_Click(object sender, EventArgs e)
        {
            string txMsg = this.textBox_sendMsg.Text;

            if(!this.isSend)
            {
                this.isSend = true;
                Thread threadSend = new Thread(new ThreadStart(TestSend));
                threadSend.Start();
            }
            else
            {
                this.isSend = false;
            }

        }

        private void TestSend()
        {
            while (this.isSend)
            {
                try
                {
                    this.socketServer.SendMessageAll("1");
                    this.textBox_sendCount.Invoke(new Action(() => textBox_sendCount.Text = this.txCount++.ToString()));
                }
                catch
                {
                    Console.WriteLine("TestSending Err");
                }
                Thread.Sleep(500);
            }
        }
    }
}
