using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using EMDServer.Common;

namespace VirtualSerialTestApplication
{
    public partial class Form1 : Form
    {
        private SerialPort com;

        Thread t;
        bool isRunning = true;

        public int commandSizeReceive = 2;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.com != null && this.com.IsOpen)
            {
                this.label4.Text = "Port already open";
            }
            else
            {
                this.com = null;

                this.com = new SerialPort(Properties.Settings.Default.ComPort, 115200, Parity.None, 8, StopBits.One);
                this.com.Encoding = Encoding.ASCII;

                try
                {
                    this.com.Open();

                    t = new Thread(new ThreadStart(run));
                    t.Start();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine("VirtualComPortApp: \t" + ex.Message);
                }
                this.label4.Text = "Port open";
            }
        }

        public void run()
        {
            while (isRunning)
            {
                int read = 0;
                byte[] buf = new byte[commandSizeReceive];
                byte[] result = new byte[commandSizeReceive];

                while (read < this.commandSizeReceive)
                {
                    try
                    {
                        int actualRead = this.com.BaseStream.Read(buf, 0, commandSizeReceive);
                        for (int i = 0; i < actualRead; i++)
                        {
                            if (read + i > result.Length)
                            {
                                break;
                            }
                            result[read + i] = buf[i];
                        }
                        read += actualRead;
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("EXCEPTION: " + e.Message);
                    }
                }

                string text = ((int)result[0]).ToString() + "\tValue: " + (byte)result[1];
                // + (Constants.EMD_COMMAND)buf[1] 
                this.setText(text);
            }
        }

        public delegate void setTextDelegate(string text);

        public void setText(string text)
        {
            if (this.listBox1.InvokeRequired)
            {
                this.listBox1.Invoke(new setTextDelegate(setText), new object[] { text });
            }
            else
            {
                this.listBox1.Items.Insert(0,text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string text = this.textBox1.Text;
            byte[] value = new byte[text.Length];

            string id = this.textBox2.Text;
            byte[] idByte = new byte[1];
            idByte[0] = (byte)int.Parse(id);

            string cmd = this.textBox3.Text;
            byte[] cmdByte = new byte[1];
            cmdByte[0] = (byte)int.Parse(cmd);

            for (int i = 0; i < text.Length; i++)
            {
                value[i] = byte.Parse(text[i].ToString());
            }

            if (this.com != null && this.com.IsOpen)
            {
                this.com.BaseStream.Write(idByte, 0, 1);
                this.com.BaseStream.Write(cmdByte, 0, 1);
                this.com.BaseStream.Write(value, 0, value.Length);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.isRunning = false;
            t.Abort();
        }
    }
}
