using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

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

namespace LFP_Manager.Function
{
    class csHistoryFunction
    {
        #region DB CREATE

        public static void DbCreate(string mPath)
        {
            string dbFilename = mPath + DbConstData.Database.FileName;

            if (Directory.Exists(System.IO.Path.GetDirectoryName(dbFilename)) == false)
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(dbFilename));

            if (File.Exists(dbFilename) == false)
                // Create database
                SQLiteConnection.CreateFile(dbFilename);

            // Open database
            string strConn = @"Data Source=" + dbFilename;
            using (var connection = new SQLiteConnection(strConn))
            {
                connection.Open();
                try
                {
                    // Create table
                    using (SQLiteCommand command = connection.CreateCommand())
                    {
                        command.CommandText = DbConstData.Database.CreateTable;
                        command.ExecuteNonQuery();
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        #endregion

        #region DB INSERT DATA
        private void IDInsert(DataTable aData, string mPath, string Quary)
        {
            string dbFilename = mPath + DbConstData.Database.FileName;

            // Open database
            string strConn = @"Data Source=" + dbFilename;
            using (var connection = new SQLiteConnection(strConn))
            {
                connection.Open();
                try
                {
                    // Insert data
                    using (SQLiteCommand command = connection.CreateCommand())
                    {
                        //WaitForm.ShowWaitForm();

                        command.CommandText = "BEGIN;"; //명시적 트렌젝션 시작
                        command.ExecuteNonQuery();

                        //sSQL = "insert into TrendTable ( TrendStamp, TagName, TagValue) Values ( " + IntToStr(stamp) + "," + name + "," + value + ");";
                        //command.CommandText = "INSERT INTO " + csDbConstData.DataBase.TableName + "(id) " + " Values (@id);";
                        command.CommandText = Quary;
                        SQLiteParameter p = new SQLiteParameter("@id", DbType.String);

                        command.Parameters.Add(p);

                        for (int i = 0; i < aData.Rows.Count; i++)
                        {
                            p.Value = String.Format("{0}", aData.Rows[i][0].ToString());       // id

                            try
                            {
                                command.ExecuteNonQuery();
                            }
                            catch (Exception)
                            {
                                //MessageBox.Show(e1.ToString() + ":" + i.ToString());
                            }
                            finally
                            {
                                //System.Windows.Forms.Application.DoEvents();
                                //WaitForm.SetWaitFormDescription(String.Format("{0}//{1}", i, aData.Rows.Count));
                            }
                        }
                        command.CommandText = "COMMIT;"; //명시적 트렌젝션 시작
                        command.ExecuteNonQuery();

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

        #endregion

    }
}
