using System.Collections.Generic;
using System;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using AR;
using vmsnet;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;

namespace vmsnet
{
    public partial class Frm_Alamlist
    {
        public Frm_Alamlist()
        {
            InitializeComponent();

            ////DB초기화작업
            PUB.INIT(out List<string> errorMessageList);
            if (errorMessageList.Any())
                foreach (var err in errorMessageList)
                    UTIL.MsgE(err);

            this.cmb_gubun.Items.Clear();
            this.cmb_gubun.Items.AddRange(new string[] { "ALL", "HIGH", "LOW", "A1", "A2" });
            //PUB.Alarm.Message += (s1, e1) => PUB.log.AddI(e1.Message);
            this.label2.Click += (s1,e1)=> {
                var f = new fLog();
                f.Show();
            };
        }

        public void Frm_Alam_Load(object sender, System.EventArgs e)
        {
            ////알람데이터년도를 변경
            this.tb_stime.Text = DateTime.Now.AddDays(0).ToString("yyyy-MM-dd 00:00:00");
            this.tb_etime.Text = DateTime.Now.AddDays(0).ToString("yyyy-MM-dd 23:59:59");

            if (cmb_gubun.Items.Count > 0)
                this.cmb_gubun.SelectedIndex = 1; // HIGH (Default)

            toolStripLabel1.Text = PUB.CONFIG.GetDatabasePath();
        }

        public void ToolStripTextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string str = this.tb_search.Text.Trim().Replace("'", "").ToUpper();
                if (string.IsNullOrEmpty(str))
                {
                    DisplayData(Dt.Item2.AsEnumerable());
                }
                try
                {
                    if (Dt.Item2 != null)
                    {
                        var dt = Dt.Item2.Where(t => t.TIME.Contains(str) || t.CHNAME.Contains(str) || t.RTYPESTR.Contains(str) || t.ATYPESTR.Contains(str));
                        DisplayData(dt);
                    }
                    else UTIL.MsgE("먼저 자료를 조회하세요");
                    
                    this.tb_search.SelectAll();
                }
                catch (Exception)
                {
                    this.tb_search.Text = "";
                    this.bs.Filter = "";

                }
            }
        }

        (TimeSpan, DocumentElement.ALARMDataTable) Dt;
        private void button1_Click(object sender, EventArgs e)
        { // 조회
            if (DateTime.TryParse(this.tb_stime.Text, out DateTime sdate) == false)
            {
                UTIL.MsgE("시작일시가 잘못 입력되었습니다\r\n확인하세요");
                return;
            }

            if (DateTime.TryParse(this.tb_etime.Text, out DateTime edate) == false)
            {
                UTIL.MsgE("종료일시가 잘못 입력되었습니다\r\n확인하세요");
                return;
            }

            
            Dt.Item1 = new TimeSpan(0);
            Dt.Item2 = null;

            var gubunstr = cmb_gubun.Text.Trim().ToUpper();
            this.button1.Enabled = false;

            switch (gubunstr)
            {
                case "HIGH": ////h
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate, 0, 1);
                    break;
                case "LOW": ////l;
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate, 2, 3);
                    break;
                case "A1": ////nullb alam1
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate, 4, 5);
                    break;
                case "A2": ////nullb alam2
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate, 6, 7);
                    break;
                case "OVER": ////o
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate, 8, 9);
                    break;
                default:    //ALL
                    Dt = PUB.Alarm.GetAlarmData(sdate, edate);
                    break;
            }

            PUB.log.AddI($"read time : {Dt.Item1.TotalSeconds} sec");
            Application.DoEvents();


            this.prb1.Value = 0;
            this.prb1.Maximum = Dt.Item2.Rows.Count;


            var dtEdit = DateTime.Now;
            foreach (var Dr in Dt.Item2)
            {

                this.prb1.Value += 1;
                if (Dr.RTYPE == 4 || Dr.RTYPE == 5 || Dr.RTYPE == 6 || Dr.RTYPE == 7)
                {
                    Dr.CHNAME = PUB.GetGrpName(Dr.CH);
                }
                else
                {
                    /* 작성자: 김치균, 작성일: 2024-10-16, 작성내용: Dr.CH + 1 -> Dr.CH 변경 */
                    Dr.CHNAME = PUB.GetChannelName(Dr.CH);
                }

                var rtype = (COMM.EALAMRAISETYPE)Dr.RTYPE;
                Dr.RTYPESTR = rtype.ToString();
                switch (rtype)
                {
                    case COMM.EALAMRAISETYPE.A1_ON:
                        Dr.RTYPESTR = "NB_ALARM1-ON";
                        break;
                    case COMM.EALAMRAISETYPE.A1_OFF:
                        Dr.RTYPESTR = "NB_ALARM1-OFF";
                        break;
                    case COMM.EALAMRAISETYPE.A2_ON:
                        Dr.RTYPESTR = "NB_ALARM2-ON";
                        break;
                    case COMM.EALAMRAISETYPE.A2_OFF:
                        Dr.RTYPESTR = "NB_ALARM2-OFF";
                        break;
                }

                var atype = (COMM.EALAMTYPE)Dr.ATYPE;
                Dr.ATYPESTR = atype.ToString();
            }
            Dt.Item2.AcceptChanges();
            var tsEdit = DateTime.Now - dtEdit;
            PUB.log.AddI($"edit time : {tsEdit.TotalSeconds} sec");
            Application.DoEvents();


            dtEdit = DateTime.Now;
            DisplayData(Dt.Item2.AsEnumerable());
            tsEdit = DateTime.Now - dtEdit;
            PUB.log.AddI($"DisplayData time : {tsEdit.TotalSeconds} sec");
            button1.Enabled = true;
        }

        void DisplayData(EnumerableRowCollection<vmsnet.DocumentElement.ALARMRow> search)
        {
            this.listView1.Items.Clear();
       
     
            this.listView1.SuspendLayout();
            foreach (var item in search)
            {
                var lv = this.listView1.Items.Add(item.TIME);
                lv.SubItems.Add(item.CHNAME);
                lv.SubItems.Add(item.RTYPESTR);
                lv.SubItems.Add(item.VOLT.ToString());
                lv.SubItems.Add(item.MAXVOLT.ToString());
                lv.SubItems.Add(item.MINVOLT.ToString());
                lv.SubItems.Add(item.ATYPESTR);
                lv.SubItems.Add(item.REMARK);

                switch (item.RTYPESTR.ToUpper())
                {
                    case "NB_ALARM1_ON":
                        lv.BackColor = Color.HotPink;
                        break;
                    case "NB_ALARM2_ON":
                        lv.BackColor = Color.OrangeRed;  //Color.Red;
                        break;
                    case "HIGH_ON":
                        lv.BackColor = Color.Orange;
                        break;
                    case "LOW_ON":
                        lv.BackColor = Color.LightSkyBlue;  //Color.Blue;
                        break;
                    case "OVER_ON":
                        lv.BackColor = Color.Violet;  //Color.Magenta;
                        break;
                    default:
                        lv.BackColor = Color.Empty;
                        break;
                }
            }
            this.listView1.ResumeLayout();
        }
    }
}
