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;
using EpsonC3Com;
using System.Net.Sockets;

namespace Testovaci_aplikace_diplomka
{
    public partial class NastaveniKomunikaceDialog : Form
    {
        public NastaveniKomunikaceDialog()
        {
            InitializeComponent();
        }
        
        private void btOpen_Click(object sender, EventArgs e)
        {
            if (GlobalTrackerClassSet.tcpInst == null)
                try
                {
                    GlobalTrackerClassSet.tcpInst = new TcpC3Com(tbIPAdr.Text, Convert.ToInt32(tbPort.Text));
                }
                catch (FormatException)
                {
                    MessageBox.Show("Špatně zadané parametry spojení");
                }


            if (GlobalTrackerClassSet.tcpInst != null)
            {
                try
                {
                    GlobalTrackerClassSet.tcpInst.Connect();
                }
                catch (SocketException)
                {
                    MessageBox.Show("Chyba socketu");
                }

                GlobalTrackerClassSet.tcpInst.DataRead -= new TcpC3Com.DataReadHandler(tcpInst_DataRead);
                GlobalTrackerClassSet.tcpInst.DataRead += new TcpC3Com.DataReadHandler(tcpInst_DataRead);

                GlobalTrackerClassSet.tcpInst.ClosingConnection -= new TcpC3Com.CloseHandler(tcpInst_ClosingConnection);
                GlobalTrackerClassSet.tcpInst.ClosingConnection += new TcpC3Com.CloseHandler(tcpInst_ClosingConnection);
            }

            if (GlobalTrackerClassSet.tcpInst.Connected)
            {
                statStrip.Items[0].Text = "Connected";
            }
            else
            {
                statStrip.Items[0].Text = "Disconnected";
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            if (GlobalTrackerClassSet.tcpInst != null)
            {
                GlobalTrackerClassSet.tcpInst.Close();
                GlobalTrackerClassSet.tcpInst = null;
            }

            if (GlobalTrackerClassSet.tcpInst.Connected)
            {
                statStrip.Items[0].Text = "Connected";
            }
            else
            {
                statStrip.Items[0].Text = "Disconnected";
            }
        }

        //private void readToList(String message)
        //{
        //    DateTime currTime = DateTime.Now;
        //    String item = "[";
        //    item += currTime.ToLongTimeString();
        //    item += "] ";

        //    if (message != "")
        //    {
        //        item += message;
        //    }
        //    lbReceive.Items.Add(item);
        //}

        void tcpInst_ClosingConnection()
        {
            if (GlobalTrackerClassSet.tcpInst != null)
            {
                if (GlobalTrackerClassSet.tcpInst.Connected)
                {
                    statStrip.Items[0].Text = "Connected";
                }
                else
                {
                    statStrip.Items[0].Text = "Disconnected";
                }
            }
        }

        void tcpInst_DataRead()
        {
            //Console.WriteLine(GlobalTrackerClassSet.tcpInst.RecBlock.ReceiveData);
            //readToList(GlobalTrackerClassSet.tcpInst.RecBlock.ReceiveData);
            Console.WriteLine(GlobalTrackerClassSet.tcpInst.RecBlock.State);
            //if (GlobalTrackerClassSet.tcpInst.SentData == "CurPosP")
                //showPosP();
            //else if (GlobalTrackerClassSet.tcpInst.SentData == "CurPosJ")
                //showPosJ();
            if (GlobalTrackerClassSet.tcpInst.RecBlock.State == TcpC3Com.ReceiveBlock.RecState.Disconnect || GlobalTrackerClassSet.tcpInst.RecBlock.State == TcpC3Com.ReceiveBlock.RecState.Timeout)
                statStrip.Items[0].Text = "Disconnected";
        }

        private void InitializeButton_Click(object sender, EventArgs e)
        {
            if (GlobalTrackerClassSet.tcpInst != null)
            {
                try
                {
                    GlobalTrackerClassSet.tcpInst.SendInit();
                }
                catch (FormatException)
                {
                    MessageBox.Show("Špatně zadané parametry");
                }
            }

            if (GlobalTrackerClassSet.tcpInst != null)
            {
                try
                {
                    RobCoord.XYZ coordFromTB = new RobCoord.XYZ(0, 0, 0, 0, 0, 0);
                    //kontroluje souradnice a pripadne je opravuje
                    //if (!coordFromTB.CheckCoord())
                    //    MessageBox.Show("Parametry mimo meze");

                    GlobalTrackerClassSet.tcpInst.SendToolSet(1, coordFromTB);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Špatně zadané parametry");
                }


                if (GlobalTrackerClassSet.tcpInst != null)
                {
                    try
                    {
                        GlobalTrackerClassSet.tcpInst.SendTool(1);
                        //readToList();
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("Špatně zadané parametry");
                    }
                }

            }
        }
    }
}
