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

namespace CNAS_DBSync
{
    public delegate void InstrumentItemDataHanlder(Dictionary<string,DataTable> dictInstrument);

    public partial class frmDatabaseParams : Form
    {
        public SyncInstrumentItemInfo syncInstrumentItem = new SyncInstrumentItemInfo();
        public Dictionary<string, DataTable> dictInstrument = new Dictionary<string, DataTable>();
        public DataBaseInfo dataBaseInfo = new DataBaseInfo();
        public InstumentCodeHanlder InstrumentDelegate;
        public InstrumentItemDataHanlder InstrumentItemData;

        public frmDatabaseParams(SyncInstrumentItemInfo syncInstrumentItem)
        {
            InitializeComponent();

            this.syncInstrumentItem = syncInstrumentItem;
        }

        private void frmDatabaseParams_Load(object sender, EventArgs e)
        {
            if (syncInstrumentItem.SyncInstrumentDSInfo == null)
                syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();

            //如果有旧值，加载旧的值
            switch (syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
            {
                case DataSourceType.Access:
                    this.txtAccessPath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
                    this.txtAccessUser.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserId;
                    this.txtAceessPwd.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserPwd;
                    break;
                case DataSourceType.Excel:
                    this.txtInportExcel.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
                    break;
                case DataSourceType.SQLLite:
                    this.txtSqlitePath.Text= syncInstrumentItem.SyncInstrumentDSInfo.LocalPath;
                    this.txtSqliteUser.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserId;
                    this.txtSqlitePwd.Text = syncInstrumentItem.SyncInstrumentDSInfo.UserPwd;
                    break;
                default:
                    break;           
            }

            //加载旧的CNAS数据库信息
            if (syncInstrumentItem.SyncTargetDBInfo == null)
                syncInstrumentItem.SyncTargetDBInfo = new DataBaseInfo();
            this.txtDBHost.Text = syncInstrumentItem.SyncTargetDBInfo.DBHost;
            this.txtDBName.Text = syncInstrumentItem.SyncTargetDBInfo.DBName;
            this.txtDBUser.Text = syncInstrumentItem.SyncTargetDBInfo.DBUser;
            this.txtDBPwd.Text = syncInstrumentItem.SyncTargetDBInfo.DBPwd;
            this.txtPort.Text= syncInstrumentItem.SyncTargetDBInfo.DBPort;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //点击确定按钮
            InstrumentData instrumentData = null;
       
            switch (syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
            {
                case DataSourceType.Access:

                    syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = this.txtAccessPath.Text.Trim();
                    syncInstrumentItem.SyncInstrumentDSInfo.UserId = this.txtAccessUser.Text.Trim();
                    syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = this.txtAceessPwd.Text.Trim();

                     instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath, syncInstrumentItem.SyncInstrumentDSInfo.UserId, syncInstrumentItem.SyncInstrumentDSInfo.UserPwd });
                    dictInstrument = instrumentData.GetInstrumentData();
                    break;                
                case DataSourceType.SQLLite:

                    syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = this.txtSqlitePath.Text.Trim();
                    syncInstrumentItem.SyncInstrumentDSInfo.UserId = this.txtSqliteUser.Text.Trim();
                    syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = this.txtSqlitePwd.Text.Trim();

                    instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath, syncInstrumentItem.SyncInstrumentDSInfo.UserId, syncInstrumentItem.SyncInstrumentDSInfo.UserPwd });
                    dictInstrument = instrumentData.GetInstrumentData();
                    break;
                case DataSourceType.Excel:
                default:
                    instrumentData = InstrumentDataFact.CreateInstrumentDataSource(syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType, new object[] { syncInstrumentItem.SyncInstrumentDSInfo.LocalPath });
                    dictInstrument = instrumentData.GetInstrumentData();
                    break;
            }

            //目标库的信息
            syncInstrumentItem.SyncTargetDBInfo.DBHost = this.txtDBHost.Text.Trim();
            syncInstrumentItem.SyncTargetDBInfo.DBName = this.txtDBName.Text.Trim();
            syncInstrumentItem.SyncTargetDBInfo.DBUser = this.txtDBUser.Text.Trim();
            syncInstrumentItem.SyncTargetDBInfo.DBPwd = this.txtDBPwd.Text.Trim();
            syncInstrumentItem.SyncTargetDBInfo.DBPort = this.txtPort.Text.Trim();

            this.InstrumentDelegate(syncInstrumentItem);
            this.InstrumentItemData(dictInstrument);

            this.Close();
        }

        private void btnOpenFileExcel_Click(object sender, EventArgs e)
        {
            if (syncInstrumentItem.Code == null) return;

            if (syncInstrumentItem.SyncInstrumentDSInfo == null)
                syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();

            syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Excel;

            OpenFileDialog openFile = new OpenFileDialog
            {
                Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                Multiselect = false
            };

            if (openFile.ShowDialog() == DialogResult.Cancel) return;
            this.txtInportExcel.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
            string fileType = System.IO.Path.GetExtension(openFile.FileName);
            if (string.IsNullOrEmpty(fileType)) return;
        }

        private void tabcDS_Selected(object sender, TabControlEventArgs e)
        {
            //切换的时候，检查其他类型数据源是否有值，如果有值，将提示删除旧值才能配置新值
            if (e.TabPage == tabExcel)
            {
                //检查tabAccess页面是否为空 //检查tabSQLite页面是否为空
                if (!CheckAccessControl() || !CheckSQLiteControl())
                {
                    if (MessageBox.Show("其他数据源存在已保存数据，继续将删除这些值，是否继续", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        this.txtAccessPath.Text = "";
                        this.txtAccessUser.Text = "";
                        this.txtAceessPwd.Text = "";

                        this.txtSqliteUser.Text = "";
                        this.txtSqlitePath.Text = "";
                        this.txtSqlitePwd.Text = "";

                        syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = "";
                        syncInstrumentItem.SyncInstrumentDSInfo.UserId = "";
                        syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = "";
                    }
                }
                syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Excel;
            }
            if (e.TabPage == tabAccess)
            {
                //检查tabAccess页面是否为空 //检查tabSQLite页面是否为空
                if (!CheckExcelControl() || !CheckSQLiteControl())
                {
                    if (MessageBox.Show("其他数据源存在已保存数据，继续将删除这些值，是否继续", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        this.txtAccessPath.Text = "";
                        this.txtAccessUser.Text = "";
                        this.txtAceessPwd.Text = "";

                        this.txtSqliteUser.Text = "";
                        this.txtSqlitePath.Text = "";
                        this.txtSqlitePwd.Text = "";

                        syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = "";
                        syncInstrumentItem.SyncInstrumentDSInfo.UserId = "";
                        syncInstrumentItem.SyncInstrumentDSInfo.UserPwd = "";
                    }
                }
                syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Access;
            }

        }

        private bool CheckExcelControl()
        {
            if (this.txtInportExcel.Text != "")
                return false;
            else
                return true;
        }

        private bool CheckAccessControl()
        {
            if (this.txtAccessPath.Text != "" && this.txtAccessUser.Text != "" && this.txtAceessPwd.Text != "")
                return false;
            else
                return true;
        }

        private bool CheckSQLiteControl()
        {
            if (this.txtSqlitePath.Text != "" && this.txtSqliteUser.Text != "" && this.txtSqlitePwd.Text != "")
                return false;
            else
                return true;
        }

        private void btnCNASTestLink_Click(object sender, EventArgs e)
        {
            if(CnasDataOperationFact.CnasDataOperation().TestConnect(this.txtDBHost.Text.Trim(), this.txtDBName.Text.Trim(), this.txtDBUser.Text.Trim(), this.txtDBPwd.Text.Trim(),this.txtPort.Text.Trim()))
                MessageBox.Show("连接成功！");
            else
                MessageBox.Show("连接失败！");
        }

        private void btnOpenFileAccess_Click(object sender, EventArgs e)
        {
            if (syncInstrumentItem.Code == null) return;

            if (syncInstrumentItem.SyncInstrumentDSInfo == null)
                syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();

            syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.Access;

            OpenFileDialog openFile = new OpenFileDialog
            {
                Filter = "Microsoft Access(*.mdb)|*.mdb",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                Multiselect = false
            };

            if (openFile.ShowDialog() == DialogResult.Cancel) return;
            this.txtAccessPath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
            string fileType = System.IO.Path.GetExtension(openFile.FileName);
            if (string.IsNullOrEmpty(fileType)) return;
        }

        private void btnOpenFileSQLite_Click(object sender, EventArgs e)
        {
            if (syncInstrumentItem.Code == null) return;

            if (syncInstrumentItem.SyncInstrumentDSInfo == null)
                syncInstrumentItem.SyncInstrumentDSInfo = new InstrumentDataSourceInfo();

            syncInstrumentItem.SyncInstrumentDSInfo.InstrumentDataSourceType = DataSourceType.SQLLite;

            OpenFileDialog openFile = new OpenFileDialog
            {
                Filter = "SQLite(*.db)|*.db",
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                Multiselect = false
            };

            if (openFile.ShowDialog() == DialogResult.Cancel) return;
            this.txtSqlitePath.Text = syncInstrumentItem.SyncInstrumentDSInfo.LocalPath = openFile.FileName;
            string fileType = System.IO.Path.GetExtension(openFile.FileName);
            if (string.IsNullOrEmpty(fileType)) return;
        }
    }
}
