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;

namespace AGVEmulator
{
    public partial class SerialConn : UserControl
    {
        public AR.Dev.RS232 dev { get; set; }
        public SerialConn()
        {
            InitializeComponent();
        }

        void AttachEvent()
        {
            dev.Message += Dev_Message;
        }
        void DetachEvent()
        {
            dev.Message -= Dev_Message;
        }
        private void Dev_Message(object sender, AR.Dev.RS232.MessageEventArgs e)
        {
            if (e.MsgType == AR.Dev.RS232.MessageType.Recv)
                ToggleRX();
            else if (e.MsgType == AR.Dev.RS232.MessageType.Send)
                ToggleTX();
        }

        public void SetPortList(string[] list)
        {
            cmbPOrt.Items.Clear();
            foreach (var item in list)
                cmbPOrt.Items.Add(item);
        }
        public string PortName
        {
            get
            {
                return cmbPOrt.Text;
            }
            set { this.cmbPOrt.Text = value; }
        }
        public int BaudRate
        {
            get
            {
                return int.Parse(cmbBaud.Text);
            }
            set
            {
                cmbBaud.Text = value.ToString();
            }
        }
        public void ToggleTX()
        {
            if (lbtx.BackColor == Color.DeepSkyBlue)
                lbtx.BackColor = Color.White;
            else lbtx.BackColor = Color.DeepSkyBlue;
        }
        public void ToggleRX()
        {
            if (lbrx.BackColor == Color.Lime)
                lbrx.BackColor = Color.White;
            else lbrx.BackColor = Color.Lime;
        }
        public void Disconnect()
        {
            if (this.dev.IsOpen)
            {
                DetachEvent();
                dev.Close();
            }
            if (dev.IsOpen)
                button1.BackColor = Color.Lime;
            else button1.BackColor = Color.Tomato;
        }
        public void Connect()
        {
            if (this.dev.IsOpen)
            {
                DetachEvent();
                dev.Close();
            }
            else
            {
                this.dev.PortName = cmbPOrt.Text;
                this.dev.BaudRate = int.Parse(cmbBaud.Text);
                AttachEvent();
                try
                {
                    dev.Open();
                }
                catch (Exception ex)
                {
                    button1.BackColor = Color.Red;
                }
            }

            if (dev.IsOpen)
                button1.BackColor = Color.Lime;
            else button1.BackColor = Color.Tomato; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dev == null)
            {
                MessageBox.Show("시리얼 개체가 연결되지 않았습니다");
                return;
            }
            if(int.TryParse(cmbBaud.Text,out int baud)==false)
            {
                MessageBox.Show("Baudrate 값이 올바르지 않습니다");
                return;
            }
            Connect();




        }
    }
}
