using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using LFP_Manager.Forms;
using LFP_Manager.DataStructure;
using LFP_Manager.Function;

namespace LFP_Manager.Controls
{
    public delegate void ButtonEvent(object sender, EventArgs e);

    public partial class ucTargetControl : DevExpress.XtraEditors.XtraUserControl
    {
        #region VARIABLES

        public event ButtonEvent OnConfig = null;
        public event ButtonEvent OnStart = null;

        #endregion

        #region CONSTRUCTORS

        public ucTargetControl()
        {
            InitializeComponent();

            lbStatus.Appearance.ForeColor = System.Drawing.Color.Red;
        }

        #endregion

        #region UPDATE DATA

        public void UpdateConfig(CommConfig Config)
        {
            string info;

            if (Config.CommType == 1)
            {
                info = String.Format("INTERFACE: UART,  PORT: {0},  BAUDRATE: {1},  MODEL: {2}"
                    , Config.UartPort
                    , Config.UartBaudrate
                    , csConstData.CommType.UART_MODEL[Config.UartModelIndex]
                    );
            }
            else
            {
                info = String.Format("INTERFACE: CAN, DEVICE: {0}({1}), PORT: {2}, BAUDRATE: {3}, MODEL: {4}"
                    , csCanConstData.CanDeviceInfo.DeviceNames[Config.CanDevice]
                    , Config.CanIndex
                    , Config.CanNo
                    , csCanConstData.BaudRate.BaudRateStrings[Config.CanBaudrate]
                    , csConstData.CommType.CAN_MODEL[Config.TargetModelIndex]
                    );
            }

            lbTargetInfo.Text = info;
        }

        #endregion

        public void UpdateStatus(bool flag)
        {
            if (flag)
            {
                btnConfig.Enabled = false;
                btnStart.Text = "STOP";

                lbStatus.Text = "RUN";
                lbStatus.Appearance.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {
                btnConfig.Enabled = true;
                btnStart.Text = "START";

                lbStatus.Text = "STOP";
                lbStatus.Appearance.ForeColor = System.Drawing.Color.Red;
            }
        }

        #region BUTTON EVENT

        private void btnConfig_Click(object sender, EventArgs e)
        {
            if (OnConfig != null)
                OnConfig(sender, e);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (OnStart != null)
                OnStart(sender, e);
        }

        #endregion
    }
}
