using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.IO;

using DevExpress.XtraWaitForm;
using DevExpress.XtraSplashScreen;

using System.Data.SQLite;
using LFP_Manager.DataStructure;
using LFP_Manager.Utils;

namespace LFP_Manager.Controls
{
    public partial class ucHistroy : DevExpress.XtraEditors.XtraUserControl
    {
        #region VARIABLES

        CommConfig Config;

        DataTable dt;

        #endregion

        #region CONSTRUCTOR

        public ucHistroy()
        {
            InitializeComponent();

            dt = new DataTable();
        }

        public void SetCommCofig(CommConfig aConfig)
        {
            Config = aConfig;
        }
        #endregion

        #region SQL Quary Excute

        private void quaryExcute(string quary)
        {
            string dbFilename = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + csDbConstData.DataBase.FileName;

            // Open database
            string strConn = @"Data Source=" + dbFilename;
            using (var connection = new SQLiteConnection(strConn))
            {
                connection.Open();
                try
                {
                    DataTable aT = new DataTable();

                    DbWaitForm.ShowWaitForm();

                    csDbUtils.BeginTran(connection);
                    // Update data
                    using (SQLiteCommand command = connection.CreateCommand())
                    {
                        //sSQL = "insert into TrendTable ( TrendStamp, TagName, TagValue) Values ( " + IntToStr(stamp) + "," + name + "," + value + ");";
                        command.CommandText = quary;

                        try
                        {
                            SQLiteDataReader reader = command.ExecuteReader();
                            aT.Load(reader);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    csDbUtils.CommitTran(connection);
                    dt = aT;
                    //RealGridView.DataSource = dt;

                    DbWaitForm.CloseWaitForm();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        #endregion

        #region BUTTON EVENT

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DateTime start = dtpStart.Value;
            DateTime mid;
            DateTime end = dtpEnd.Value;
            TimeSpan span = end - start;

            DataTable result = new DataTable();

            int sDay = start.Day;
            int eDay = end.Day;

            int total_days = eDay - sDay;

            DataTable[] dDt = new DataTable[total_days + 1];

            string quary = "";

            try
            {
                if (total_days == 0)
                {
                    quary = String.Format(" where create_date > '{0:yyyy-MM-dd HH:mm:ss}'", start);
                    quary += String.Format(" and create_date < '{0:yyyy-MM-dd HH:mm:ss}'", end);
                    dDt[0] = csDbUtils.BmsDataSelectToDataTable(Config, start, quary);
                    if (dDt[0] == null)
                        MessageBox.Show("No data", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        gridSearchResult.DataSource = dDt[0];
                        //gridSearchResult.();
                    }
                }
                else
                {
                    DataTable bDt = new DataTable();
                    for (int i = 0; i < (total_days + 1); i++)
                    {
                        //dDt[i] = new DataTable();
                        if (i == 0)
                        {
                            quary = String.Format(" where create_date > '{0:yyyy-MM-dd HH:mm:ss}'", start);
                            dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, start, quary);
                        }
                        else if (i == total_days)
                        {
                            quary = String.Format(" where create_date < '{0:yyyy-MM-dd HH:mm:ss}'", end);
                            dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, end, quary);
                        }
                        else
                        {
                            quary = "";
                            mid = start.AddDays(i);
                            dDt[i] = csDbUtils.BmsDataSelectToDataTable(Config, mid, quary);
                        }
                        if (dDt[i] != null)
                            bDt.Merge(dDt[i]);
                    }
                    if (bDt == null)
                        MessageBox.Show("No data", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        gridSearchResult.DataSource = bDt;
                        //gridSearchResult.();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
