using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using tpDomeWinAPP.Models;
using tpDomeWinAPP.Service;

namespace tpDomeWinAPP
{
    public partial class fmObixStatus : Form
    {
        //IotGrid.Porcess proc = new IotGrid.Porcess();
        getObixSvc obixSvc = new getObixSvc();
        procCompare svcCompar = new procCompare();
        public string building { get; set; }
        public fmObixStatus()
        {
            InitializeComponent();
        }

        private void btSearch_N4vsDB_Click(object sender, EventArgs e)
        {
            List<dbDevice> ds = new List<dbDevice>();
            if (tbSec4.Text.Trim().Length == 0)
            {
                MessageBox.Show("請輸入 tag 第四碼");
                return;
            } 
            ds = queryDbDevice();
            getObixResultAsync(ds, dgv); 
        }

        private List<dbDevice> queryDbDevice() {
            building = ((fmCheckTagName)this.Parent.Parent.Parent.Parent.Parent).building;
           
            StringBuilder sb = new StringBuilder();
            //foreach (Control c in tableLayoutPanel1.Controls)
            //{
            //    if (c is TextBox)
            //    {
            //        if (c.Text.Trim().Length > 0)
            //        {
            //            string num = c.Name.Substring(c.Name.Length - 1, 1);//取出最後的數字
            //            switch (num)
            //            {
            //                // case "1": sb.Append($@" and device_building_tag like '%{c.Text.Trim()}%' ");  break;
            //                case "2": sb.Append($@" and device_system_tag like '%{c.Text.Trim()}%' "); break;
            //                case "3": sb.Append($@" and device_floor_tag like '%{c.Text.Trim()}%' "); break;
            //                case "4": sb.Append($@" and device_name_tag like '%{c.Text.Trim()}%' "); break;
            //                case "5": sb.Append($@" and device_serial_tag like '%{c.Text.Trim()}%' "); break;
            //            }
            //        }
            //    }
            //}
            sb.Insert(0, $@"select * from import_dbTag_niagaraTag where device_building_tag = '{building}'  and device_number = 'H_E4_B5F_WHT_EHB5a'");
            //if (cbShowEmpty.Checked)
            //{
            //    sb.Append($@" and  niagaraTag = ''");
            //}
            //sb.Append($@" order by device_number");
            var ds = svcCompar.filterDBTags(sb.ToString()); 
            lbMsg.Text = "共 " + ds.Count.ToString() + " 筆資料 " + Environment.NewLine + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            return ds;
        }

        public async void getObixResultAsync(List<dbDevice> ds, DataGridView dv) {

            List<device_obix_value> dsDevice = new List<device_obix_value>();
            //string api = "http://60.251.164.125:8080/obix/config/Arena/H/E4/B5F/WHT/H_E4_B5F_WHT_EHB5a/";
            string api = "http://60.251.164.125:8080/obix/config/Arena/";
            foreach (var item in ds)
            {
                string apiURL = api + $@"{item.device_building_tag}/{item.device_system_tag}/{item.device_floor_tag}/{item.device_name_tag}/{item.device_number}";
                await obixSvc.GetIotData(apiURL, dsDevice, item.device_number);
            }
            dv.DataSource = dsDevice;
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (dgv.Columns.Count == 5 || e.RowIndex == -1) return;
            if (e.RowIndex > 0) return;

            string tagID = dgv.Rows[e.RowIndex].Cells[1].Value.ToString();

            //MessageBox.Show(inverterID);

            //string date1 = dtSelect1.Value.ToString("yyyy-MM-dd");
            //string date2 = dtSelect2.Value.ToString("yyyy-MM-dd");
            //get_inv_svc invSvc = new get_inv_svc();
            //var ds = invSvc.get_ck_invDay_detail2(inverterID, date1, date2);
            //gv_inv_detail.DataSource = ds;
            //gv_inv_detail.Columns[0].Width = 60;
            //gv_inv_detail.Columns[1].Width = 150;
            //gv_inv_detail.Columns[2].Width = 80;
            //gv_inv_detail.Columns[3].Width = 100;
            //gv_inv_detail.Columns[4].Width = 80;
            //gv_inv_detail.Columns[5].Width = 80;
        }
    }
}