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;
using AR;

namespace Project.Dialog
{
    public partial class fVAR : Form
    {
        public fVAR()
        {
            InitializeComponent();
        }

        private void fVAR_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new string[] {
                "Byte",
                "Bool",
                "Time",
                "String",
                "UInt32",
                "Int32",
                "Dbl",
            });
            comboBox1.SelectedIndex = -1;
            timer1.Start();
        }

        object itemrefresh = new object();

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0) return;
            var strtype = comboBox1.Text.Trim().ToUpper();

            if (System.Diagnostics.Debugger.IsAttached)
                this.TopMost = false;

            lock (itemrefresh)
            {
                listView1.Items.Clear();
                Array valuelist;
                if (strtype == "BOOL")
                {
                    valuelist = Enum.GetValues(typeof(eVarBool));
                    foreach (var item in valuelist)
                    {
                        var v = (eVarBool)item;
                        var lv = listView1.Items.Add($"{(int)v}");
                        lv.SubItems.Add($"{item}");
                        lv.SubItems.Add("--");
                    }
                }
                //else if (strtype == "BYTE")
                //{
                //    valuelist = Enum.GetValues(typeof(eVarByte));
                //    foreach (var item in valuelist)
                //    {
                //        var v = (eVarByte)item;
                //        var lv = listView1.Items.Add($"{(int)v}");
                //        lv.SubItems.Add($"{item}");
                //        lv.SubItems.Add("--");
                //    }
                //}
                else if (strtype == "STRING")
                {
                    valuelist = Enum.GetValues(typeof(eVarString));
                    foreach (var item in valuelist)
                    {
                        var v = (eVarString)item;
                        var lv = listView1.Items.Add($"{(int)v}");
                        lv.SubItems.Add($"{item}");
                        lv.SubItems.Add("--");
                    }
                }
                else if (strtype == "TIME")
                {
                    valuelist = Enum.GetValues(typeof(eVarTime));
                    foreach (var item in valuelist)
                    {
                        var v = (eVarTime)item;
                        var lv = listView1.Items.Add($"{(int)v}");
                        lv.SubItems.Add($"{item}");
                        lv.SubItems.Add("--");
                    }
                }
                else if (strtype == "INT32")
                {
                    valuelist = Enum.GetValues(typeof(eVarInt32));
                    foreach (var item in valuelist)
                    {
                        var v = (eVarInt32)item;
                        var lv = listView1.Items.Add($"{(int)v}");
                        lv.SubItems.Add($"{item}");
                        lv.SubItems.Add("--");
                    }
                }
                else if (strtype == "DBL")
                {
                    valuelist = Enum.GetValues(typeof(eVarDBL));
                    foreach (var item in valuelist)
                    {
                        var v = (eVarDBL)item;
                        var lv = listView1.Items.Add($"{(int)v}");
                        lv.SubItems.Add($"{item}");
                        lv.SubItems.Add("--");
                    }
                }
                listView1.Tag = strtype;
            }


        }

        private void fVAR_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0) return;
            if (this.listView1.Items.Count < 1) return;

            lock (itemrefresh)
            {
                var strtype = comboBox1.Text.Trim().ToUpper();
                if (strtype != listView1.Tag.ToString()) return;

                foreach (ListViewItem item in listView1.Items)
                {
                    var idx = int.Parse(item.SubItems[0].Text);
                    //if (strtype == "UINT32")
                    //{
                    //    var v = VAR.U32[idx];
                    //    item.SubItems[2].Text = v.ToString();
                    //    item.ForeColor = v == 0 ? Color.DimGray : Color.Black;
                    //}
                    //else 
                    if (strtype == "INT32")
                    {
                        var v = VAR.I32[idx];//	((eVarByte)idx);
                        item.SubItems[2].Text = v.ToString();
                        item.ForeColor = v == 0 ? Color.DimGray : Color.Black;
                    }
                    else if (strtype == "BYTE")
                    {
                        var v = VAR.I32[idx];//	((eVarByte)idx);
                        item.SubItems[2].Text = v.ToString();
                        item.ForeColor = v == 0 ? Color.DimGray : Color.Black;
                    }
                    else if (strtype == "BOOL")
                    {
                        var v = VAR.BOOL[idx];//[(eVarBool)idx);
                        item.SubItems[2].Text = v ? "O" : "X";
                        item.ForeColor = v ? Color.Green : Color.DimGray;
                    }
                    else if (strtype == "STRING")
                    {
                        var v = VAR.STR[idx];// .GetString((eVarString)idx);
                        if (v == null) item.SubItems[2].Text = "(null)";
                        else item.SubItems[2].Text = v.ToString();
                        item.ForeColor = v.isEmpty() ? Color.DimGray : Color.Black;
                    }
                    else if (strtype == "TIME")
                    {
                        var v = VAR.TIME[idx];// ((eVarTime)idx);
                        if (v.Year == 1982)
                        {
                            item.SubItems[2].Text = "--";
                            item.ForeColor = Color.DimGray;
                        }
                        else
                        {
                            item.SubItems[2].Text = v.ToString();
                            item.ForeColor = Color.Black;
                        }
                    }
                    else if (strtype == "DBL")
                    {
                        var v = VAR.DBL[idx];// .GetString((eVarString)idx);
                        item.SubItems[2].Text = v.ToString();
                        item.ForeColor = v == 0 ? Color.DimGray : Color.Black;
                    }

                }
            }

        }
    }
}
