using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using solarApp.Service;

namespace solarApp
{
    public partial class fmMain : Form
    {
        get_inv_svc inv_svc = new get_inv_svc();
        getStationSvc stationSvc = new getStationSvc();
        getSensorSvc sensorSvc = new getSensorSvc();
        public fmMain()
        {
            InitializeComponent();
           // init_GridView();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            lbMsg_inv.Text = "loading ... ";
            string date2 = dtselect_inv.Value.ToString("yyyy-MM-dd");
            //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
            string date1 = dtselect_inv.Value.AddDays(-7).ToString("yyyy-MM-dd");
            gv_fic_inv_raw.DataSource = "";
            gv_fic_inv_hour.DataSource = "";
            try
            {
                gv_fic_inv_raw.DataSource = inv_svc.Get_rawInv(date2, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text);

                gv_fic_inv_hour.DataSource = inv_svc.get_Inv_rawAvg(date2, lbInverterID.Text, lbSiteDB_inv.Text, lbSiteID_inv.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); 
            } 

            gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_hour(date2, lbInverterID.Text);

            gv_web_inv_day.DataSource = inv_svc.get_web_Inv_day(date1, date2, lbInverterID.Text);

            gv_web_inv_month.DataSource = inv_svc.get_web_Inv_month( date1.Substring(0, 7), date2.Substring(0, 7), lbInverterID.Text);
            
            lbMsg_inv.Text = " done " + System.DateTime.Now.ToShortTimeString();
        }

        private void btInv_15min_Click(object sender, EventArgs e)
        {
            lbMsg_inv.Text = "loading ... ";
            string date2 = dtselect_inv.Value.ToString("yyyy-MM-dd");
            //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
            string date1 = dtselect_inv.Value.AddDays(-7).ToString("yyyy-MM-dd");

            gv_web_inv_hour.DataSource = inv_svc.get_web_Inv_15min(date2, lbInverterID.Text , lbSiteID_inv.Text.Substring(0, 9));
        }

        private void fmMain_Load(object sender, EventArgs e)
        {
           

            dtselect_station1.Value = DateTime.Today.AddDays(-1);

            dtselect_inv.Value = DateTime.Today.AddDays(-1);

            dtSelect_sensor1.Value = DateTime.Today.AddDays(-1);



            tabControl1.SelectedTab = tabControl1.TabPages[1];

            #region 電站清單
            int i = 0;
            var site_list = stationSvc.get_station_list();
            foreach (var item in site_list)
            {
                RadioButton rb = new RadioButton();
                rb.Name = item.SiteID;
                rb.Text = item.SiteName;
                rb.Tag = item.SiteDB;
                rb.Font = new Font(Font.FontFamily, 14);
                rb.AutoSize = true;
                rb.CheckedChanged += new EventHandler(rb_site_CheckedChanged);
                fp_site.Controls.Add(rb);
                if (i == 0) rb.Checked = true;
                i++;
            }
            #endregion

            
            #region 顯示逆變器list
            //add_inv_list();
            #endregion
        }

        private void rb_site_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked)
            {
                lbSiteDB_inv.Text = rb.Tag.ToString();
                lbSiteName_inv.Text = rb.Text;
                lbSiteID_inv.Text = rb.Name;

                lbSiteName_sensor.Text = lbSiteName_inv.Text;
                lbSiteDB_sensor.Text = lbSiteDB_inv.Text;
                lbSiteID_sensor.Text = lbSiteID_inv.Text;

                //顯示 table name on label
                lbSiteRaw.Text = lbSiteName_inv.Text + "  "+ lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Station";
                lbInvRaw.Text = lbSiteName_inv.Text + "  " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_Inv";
                lbSensorRaw.Text = lbSiteName_inv.Text + "  " + lbSiteDB_inv.Text + ".s" + lbSiteID_inv.Text + "_SensorAVG";

                add_inv_list(lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 9));
            }           
        }

        /// <summary>
        /// 顯示 Inventer 提供選擇
        /// </summary>
        /// <param name="siteDB"></param>
        /// <param name="siteID"></param>
        void add_inv_list(string siteDB, string siteID) {
            fp_inv.Controls.Clear();
            var inv_list = inv_svc.get_Inv_list(siteDB,  siteID);
            int i = 0;
            foreach (var item in inv_list)
            {
                RadioButton rb = new RadioButton();
                rb.Name = "rb" + item.inverterid;
                rb.Text = item.inverterid;
                //rb.Dock = DockStyle.Top;
                rb.Width = 200;
                rb.CheckedChanged += radio_inv_click;
                if (i == 0) rb.Checked = true; //預設第一個勾選
                fp_inv.Controls.Add(rb);
                i++;
            }
        }


        private void radio_inv_click(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Checked) 
            { 
                lbInverterID.Text = rb.Text;
                lbMsg_inv.Text = "...";
            }
        }

        private void gv_fic_inv_hour_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //int rowindex = e.RowIndex; e.ColumnIndex
            if (gv_fic_inv_hour.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_fic_inv_hour.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_fic_inv_hour.Rows[e.RowIndex].Cells["count"].Value.ToString() != "12")
                {
                    gv_fic_inv_hour.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
            //else
            //{
            //    gv_fic_inv_hour.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = gv_fic_inv_hour.DefaultCellStyle;
            //}
        }


        private void dtselect_ValueChanged(object sender, EventArgs e)
        {
            lbMsg_inv.Text = "...";
        }

        /// <summary>
        /// 電站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_find_station_Click(object sender, EventArgs e)
        {
            lbMsg_station.Text = "loading ... ";

            string date2 = dtselect_station1.Value.ToString("yyyy-MM-dd");
            //string date2 = dtselect_station2.Value.ToString("yyyy-MM-dd");
            string date1 = dtselect_station1.Value.AddDays(-7).ToString("yyyy-MM-dd");
            gv_fic_station_raw.DataSource = "";
            gv_fic_station_day.DataSource = "";
            //gv_fic_station_raw.column
            try
            {
                gv_fic_station_raw.DataSource = stationSvc.get_station_raw(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);
                gv_fic_station_day.DataSource = stationSvc.get_station_rawAvg(date1, date2, lbSiteDB_inv.Text, lbSiteID_inv.Text); 
            }
            catch (Exception ex )
            {
                MessageBox.Show( ex.Message);
            }
            gv_web_station_hour.DataSource = stationSvc.get_web_station_hour(date2, lbSiteID_inv.Text);

            gv_web_station_day.DataSource = stationSvc.get_web_station_day(date1, date2, lbSiteID_inv.Text.Substring(0, 9));

            gv_web_station_month.DataSource = stationSvc.get_web_station_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 9));

            lbMsg_station.Text = " done " + System.DateTime.Now.ToShortTimeString();
        }

        private void btSearch_sensor_Click(object sender, EventArgs e)
        {
            string date2 = dtSelect_sensor1.Value.ToString("yyyy-MM-dd");
            //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
            string date1 = dtSelect_sensor1.Value.AddDays(-7).ToString("yyyy-MM-dd");
            gv_fic_sensor_raw.DataSource = "";
            gv_fic_sensor_hour.DataSource = "";
            try
            {
                gv_fic_sensor_raw.DataSource = sensorSvc.get_sensor_raw(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text);
                gv_fic_sensor_hour.DataSource = sensorSvc.get_sensor_raw_hour(date2, lbSiteDB_inv.Text, lbSiteID_inv.Text.Substring(0, 11));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);  
            } 
            gv_web_sensor_hour.DataSource = sensorSvc.get_web_sensor_hour(date2, lbSiteID_inv.Text.Substring(0, 09));

            gv_web_sensor_day.DataSource = sensorSvc.get_web_sensor_day(date1, date2, lbSiteID_inv.Text.Substring(0, 09));

            gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09));
        }

        private void btSensor_switch_Click(object sender, EventArgs e)
        {
            string date2 = dtSelect_sensor1.Value.ToString("yyyy-MM-dd");
            //string date2 = dtSelect_sensor2.Value.ToString("yyyy-MM-dd");
            string date1 = dtSelect_sensor1.Value.AddDays(-7).ToString("yyyy-MM-dd");

            gv_web_sensor_hour.DataSource = sensorSvc.get_web_sensor50_hour(date2, lbSiteID_inv.Text.Substring(0, 09));

            gv_web_sensor_day.DataSource = sensorSvc.get_web_sensor50_day(date1, date2, lbSiteID_inv.Text.Substring(0, 09));

            gv_web_sensor_month.DataSource = sensorSvc.get_web_sensor50_month(date1.Substring(0, 7), date2.Substring(0, 7), lbSiteID_inv.Text.Substring(0, 09));
        }

        private void gv_fic_sensor_hour_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gv_fic_sensor_hour.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_fic_sensor_hour.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_fic_sensor_hour.Rows[e.RowIndex].Cells["count"].Value.ToString() != "12")
                {
                    gv_fic_sensor_hour.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
        }

        private void gv_web_inv_day_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gv_web_inv_day.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_web_inv_day.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_web_inv_day.Rows[e.RowIndex].Cells["count"].Value.ToString() != "24")
                {
                    gv_web_inv_day.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
        }

        private void gv_fic_station_day_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gv_fic_station_day.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_fic_station_day.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_fic_station_day.Rows[e.RowIndex].Cells["count"].Value.ToString() != "24")
                {
                    gv_fic_station_day.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
        }

        private void gv_web_station_day_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gv_web_station_day.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_web_station_day.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_web_station_day.Rows[e.RowIndex].Cells["count"].Value.ToString() != "24")
                {
                    gv_web_station_day.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
        }

        private void gv_web_sensor_day_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (gv_web_sensor_day.Rows[e.RowIndex].Cells["count"].Value != null && !string.IsNullOrWhiteSpace(gv_web_sensor_day.Rows[e.RowIndex].Cells["count"].Value.ToString()))
            {
                if (gv_web_sensor_day.Rows[e.RowIndex].Cells["count"].Value.ToString() != "24")
                {
                    gv_web_sensor_day.Rows[e.RowIndex].Cells["count"].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                }
            }
        }
    }
}
