using System;
using System.Windows.Forms;
using CRVM.Utility;
using CRVM.CIDExcuter;
using CRVM.Entity;

namespace CRVM
{
    public partial class DataAccessForm : Form
    {
        private DbHelper dbHelper;

        public DataAccessForm(DbHelper dbHelper)
        {
            this.dbHelper = dbHelper;
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            FillString(dbHelper.SouDB, "sourcedb");
            FillString(dbHelper.DesDB, "destdb");
            Timer1Int.Text = dbHelper.Freq.ToString();
            if (!dbHelper.isStart)
                StopStatus();
            else
                StartStatus();
        }

        private EDBItem FillSourceDb1()
        {
            EDBItem soudb = new EDBItem();
            soudb.DataType = cbxDbTypeBpc1.Text.Trim();
            soudb.Database = txtDbNameBpc1.Text.Trim();
            soudb.Userid = txtDbUserBpc1.Text.Trim();
            soudb.Password = txtDbPwdBpc1.Text.Trim();
            soudb.Ipaddress = txtDbIpBpc1.Text.Trim();
            return soudb;
        }

        private EDBItem FillSourceDb2()
        {
            EDBItem soudb = new EDBItem();
            soudb.DataType = cbxDbTypeBpc2.Text.Trim();
            soudb.Database = txtDbNameBpc2.Text.Trim();
            soudb.Userid = txtDbUserBpc2.Text.Trim();
            soudb.Password = txtDbPwdBpc2.Text.Trim();
            soudb.Ipaddress = txtDbIpBpc2.Text.Trim();
            return soudb;
        }

        private void StartStatus()
        {
            cbxDbTypeBpc1.Enabled = false;
            txtDbNameBpc1.ReadOnly = true;
            txtDbUserBpc1.ReadOnly = true;
            txtDbPwdBpc1.ReadOnly = true;
            txtDbIpBpc1.ReadOnly = true;
            txtDbNameBpc2.ReadOnly = true;
            txtDbUserBpc2.ReadOnly = true;
            txtDbPwdBpc2.ReadOnly = true;
            txtDbIpBpc2.ReadOnly = true;
            cbxDbTypeBpc2.Enabled = false;
            btnDBName1.Visible = false;
            btnDBName2.Visible = false;

            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnModify.Enabled = false;
            btnSave.Enabled = false;

            Timer1Int.ReadOnly = true;
        }

        private void StopStatus()
        {
            cbxDbTypeBpc1.Enabled = true;
            txtDbNameBpc1.ReadOnly = false;
            txtDbUserBpc1.ReadOnly = false;
            txtDbPwdBpc1.ReadOnly = false;
            txtDbIpBpc1.ReadOnly = false;
            txtDbNameBpc2.ReadOnly = false;
            txtDbUserBpc2.ReadOnly = false;
            txtDbPwdBpc2.ReadOnly = false;
            txtDbIpBpc2.ReadOnly = false;
            cbxDbTypeBpc2.Enabled = true;

            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnModify.Enabled = true;
            btnSave.Enabled = true;

            Timer1Int.ReadOnly = false;
        }

        private void FillString(EDBItem soudb, string dbName)
        {
            if (dbName == "sourcedb")
            {
                cbxDbTypeBpc1.Text = soudb.DataType;
                txtDbNameBpc1.Text = soudb.Database;
                txtDbUserBpc1.Text = soudb.Userid;
                txtDbPwdBpc1.Text = soudb.Password;
                txtDbIpBpc1.Text = soudb.Ipaddress;
            }
            else if (dbName == "destdb")
            {
                cbxDbTypeBpc2.Text = soudb.DataType;
                txtDbNameBpc2.Text = soudb.Database;
                txtDbUserBpc2.Text = soudb.Userid;
                txtDbPwdBpc2.Text = soudb.Password;
                txtDbIpBpc2.Text = soudb.Ipaddress;
            }
        }

        private void btnDBName1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.accdb|*.accdb";
            openFileDialog1.ShowDialog();
            txtDbNameBpc1.Text = openFileDialog1.FileName;
        }

        private void btnDBName2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.accdb|*.accdb";
            openFileDialog1.ShowDialog();
            txtDbNameBpc2.Text = openFileDialog1.FileName;
        }

        private void btnStart1_Click(object sender, EventArgs e)
        {
            if (!dbHelper.IsConnected(FillSourceDb1()))
            {
                MessageBox.Show("L2数据库连接失败，请修改连接配置或查看网络");
                return;
            }
            if (!dbHelper.IsConnected(FillSourceDb2()))
            {
                MessageBox.Show("本地数据库连接失败，请修改连接配置或查看网络");
                return;
            }
            dbHelper.isStart = true;
            StartStatus();
        }

        private void btnStop1_Click(object sender, EventArgs e)
        {
            dbHelper.isStart = false;
            StopStatus();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dbHelper.SouDB = FillSourceDb1();
            dbHelper.DesDB = FillSourceDb2();

            if (!IsFreRight(Timer1Int.Text.Trim()))
            {
                Timer1Int.Text = dbHelper.Freq.ToString();
                MessageBox.Show("频率必须设置为正整数！");
                return;
            }
            dbHelper.Freq = Convert.ToInt32(Timer1Int.Text.Trim());

            dbHelper.SaveConfigData();
            MessageBox.Show("保存文件成功");
        }

        private void soulinktest_Click(object sender, EventArgs e)
        {
            if (dbHelper.IsConnected(FillSourceDb1()))
            {
                MessageBox.Show("数据库连接成功！");
            }
            else
            {
                MessageBox.Show("数据库连接失败！");
            }
        }

        private void deslinktest_Click(object sender, EventArgs e)
        {
            if (dbHelper.IsConnected(FillSourceDb2()))
            {
                MessageBox.Show("数据库连接成功！");
            }
            else
            {
                MessageBox.Show("数据库连接失败！");
            }
        }

        private void Timer1Int_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!IsFreRight(Timer1Int.Text.Trim()))
            {
                Timer1Int.Text = dbHelper.Freq.ToString();
                MessageBox.Show("频率必须设置为正整数！");
                return;
            }
        }

        private bool IsFreRight(string freStr)
        {
            string pattren = @"^[1-9]\d*$";
            return SysParam.Instance.DataIsMatch(freStr, pattren);
        }
    }
}

