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

namespace Project.ViewForm
{
    public partial class fFlag : Form
    {
        public fFlag()
        {
            InitializeComponent();


            this.FormClosed += FIO_FormClosed;
        }

        private void fFlag_Load(object sender, EventArgs e)
        {
            MakeControl();
            this.timer1.Start();
        }

        void MakeControl()
        {
            DataGridView[] dvs = new DataGridView[] { dv1, dv2, dv3, dv4 };
          
            foreach(var lv in dvs)
            {
                lv.Columns.Clear();
                lv.Columns.Add("idx", "*");
                lv.Columns.Add("Title", "Title");
                lv.Columns.Add("Value", "Value");
                lv.Columns[0].Width = 25;
                lv.Columns[1].Width = 150;
                lv.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                 
            }
            Array valuelist;

            valuelist = Enum.GetValues(typeof(COMM.eVarBool));
            foreach (var item in valuelist)
            {
                var v = (COMM.eVarBool)item;
                dv1.Rows.Add($"{(int)v}", item, "--");
            }

            valuelist = Enum.GetValues(typeof(COMM.eVarInt32));
            foreach (var item in valuelist)
            {
                var v = (COMM.eVarInt32)item;
                dv2.Rows.Add($"{(int)v}", item, "--");
            }

            valuelist = Enum.GetValues(typeof(COMM.eVarString));
            foreach (var item in valuelist)
            {
                var v = (COMM.eVarString)item;
                dv3.Rows.Add($"{(int)v}", item, "--");
            }

            valuelist = Enum.GetValues(typeof(COMM.eVarTime));
            foreach (var item in valuelist)
            {
                var v = (COMM.eVarString)item;
                dv4.Rows.Add($"{(int)v}", item, "--");
            }

            foreach (var dv in dvs)
                dv.AutoResizeColumns();


        }

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


        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            foreach(DataGridViewRow item in this.dv1.Rows)
            {
                var idx = int.Parse(item.Cells["idx"].Value.ToString());
                var v = VAR.BOOL.Get(idx);
                item.Cells["value"].Value = v ? "O" : "X";
            }

            foreach (DataGridViewRow item in this.dv2.Rows)
            {
                var idx = int.Parse(item.Cells["idx"].Value.ToString());
                var v = VAR.I32.Get(idx);
                item.Cells["value"].Value = v.ToString();
            }

            foreach (DataGridViewRow item in this.dv3.Rows)
            {
                var idx = int.Parse(item.Cells["idx"].Value.ToString());
                var v = VAR.STR.Get(idx);
                item.Cells["value"].Value = v;
            }
            foreach (DataGridViewRow item in this.dv4.Rows)
            {
                var idx = int.Parse(item.Cells["idx"].Value.ToString());
                var v = VAR.TIME.Get(idx);
                item.Cells["value"].Value = v.ToString("HH:mm:ss.fff");
            }
            timer1.Start();
        }

        private void fFlag_VisibleChanged(object sender, EventArgs e)
        {
            this.timer1.Enabled = this.Visible;
            if (timer1.Enabled) timer1.Start();
            else timer1.Stop();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
