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;

namespace Project.Dialog
{
    public partial class fLog : Form
    {
        public fLog()
        {
            InitializeComponent();
            this.FormClosed += FLog_FormClosed;
        }

        private void FLog_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();
            PUB.log.RaiseMsg -= Log_RaiseMsg;
            PUB.logagv.RaiseMsg -= Log_RaiseMsgagv;
            PUB.logplc.RaiseMsg -= Log_RaiseMsgplc;
            PUB.logbms.RaiseMsg -= Log_RaiseMsgbms;
            PUB.logxbee.RaiseMsg -= Log_RaiseMsgcal;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //포트열림상태표시 2600202
            if (PUB.XBE.IsOpen)
                titleXBEE.BackColor = Color.Lime;
            else
                titleXBEE.BackColor = Color.Tomato;

            if (PUB.AGV.IsOpen)
                titleAGV.BackColor = Color.Lime;
            else
                titleAGV.BackColor = Color.Tomato;

            if (PUB.BMS.IsOpen)
                titleBMS.BackColor = Color.Lime;
            else
                titleBMS.BackColor = Color.Tomato;
        }

        private void fLog_Load(object sender, EventArgs e)
        {
            this.titleXBEE.Text = $"XBEE({PUB.setting.Port_XBE},ID:{PUB.setting.XBE_ID})";
            this.titleAGV.Text = $"AGV({PUB.setting.Port_AGV}:{PUB.setting.Baud_AGV})";
            this.titleBMS.Text = $"BMS({PUB.setting.Port_BAT}:{PUB.setting.Baud_BAT})";
            var colorlist = new arCtl.sLogMessageColor[]
            {
                new arCtl.sLogMessageColor("NOR",Color.Black),
                new arCtl.sLogMessageColor("NORM",Color.Black),
                new arCtl.sLogMessageColor("NORMAL",Color.Black),
                new arCtl.sLogMessageColor("ERR",Color.Red),
                new arCtl.sLogMessageColor("FLAG",Color.Magenta),
                new arCtl.sLogMessageColor("TX",Color.SkyBlue),
                new arCtl.sLogMessageColor("SETUP",Color.Gold),
                new arCtl.sLogMessageColor("MFLAG",Color.BlueViolet),
            };
            this.rtsys.ColorList = colorlist;
            this.rtTx.ColorList = colorlist;
            this.rtAGV.ColorList = colorlist;

            PUB.log.RaiseMsg += Log_RaiseMsg;
            PUB.logagv.RaiseMsg += Log_RaiseMsgagv;
            PUB.logplc.RaiseMsg += Log_RaiseMsgplc;
            PUB.logbms.RaiseMsg += Log_RaiseMsgbms;
            PUB.logxbee.RaiseMsg += Log_RaiseMsgcal;
            timer1.Start();
        }

        private void Log_RaiseMsg(DateTime LogTime, string TypeStr, string Message)
        {
            showlog(rtsys,LogTime, TypeStr, Message);
        }
        private void Log_RaiseMsgagv(DateTime LogTime, string TypeStr, string Message)
        {
            showlog(rtAGV, LogTime, TypeStr, Message);
        }
        private void Log_RaiseMsgplc(DateTime LogTime, string TypeStr, string Message)
        {
            //showlog(rtPLC, LogTime, TypeStr, Message);
        }
        private void Log_RaiseMsgbms(DateTime LogTime, string TypeStr, string Message)
        {
            showlog(rtBMS, LogTime, TypeStr, Message);
        }
        private void Log_RaiseMsgcal(DateTime LogTime, string TypeStr, string Message)
        {
            showlog(rtXbee, LogTime, TypeStr, Message);
        }
        void showlog(arCtl.LogTextBox rtRx, DateTime LogTime, string TypeStr, string Message)
        {
            if (rtRx.Visible)
            {
                rtRx.AddMsg(LogTime, TypeStr, Message);
            }
        }

    }
}
