using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace ControlSystem
{
    /// <summary>
    /// Creator:Richard.liu
    /// 接口和监控数据库链接配置维护
    /// </summary>
    public partial class FrmConnectDB : Form
    {
        DBOperator dbo = new DBOperator(false);
        private string _connectType;
        string dbf;
        /// <summary>
        /// Manage代表管理数据库连接；Monitor代表监控数据库连接
        /// </summary>
        public string ConnectType
        {
            get { return _connectType; }
            set { _connectType = value; }
        }

        private static FrmConnectDB _formInstance;

        public static FrmConnectDB FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmConnectDB();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmConnectDB()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void FrmConnectDB_Load(object sender, EventArgs e)
        {
            try
            {
                //string dbf = dbo.GetValue("DBFactory");
                string[] temp,connstr;
                string server="",dbService="",uid="",pwd="";
                char[] dh = new char[1] { '=' };
                char[] fh = new char[1] { ';' };
                if (_connectType == "Manage")
                {
                    dbf = dbo.GetValue("ManDBFactory");
                    lblConnType.Text = "接口据库连接配置信息维护";
                    connstr = dbo.GetValue("ManConnString").Split(fh);

                }
                else //if (_connectType == "Monitor")
                {
                    dbf = dbo.GetValue("DBFactory");
                    lblConnType.Text = "调度数据库连接配置信息维护";
                    connstr = dbo.GetValue("ConnString").Split(fh);
                }
                for (int i = connstr.GetLowerBound(0); i <= connstr.GetUpperBound(0); i++)
                {
                    temp = connstr[i].Split(dh);
                    switch (dbf)
                    {
                        case "SQLServerDBFactory":
                            lblserver.Text = "数据库服务器计算机名";
                            //"Server = (local) ; uid = SA ; pwd =; database = SSWCMS ; MAX Pool Size = 512 "
                            switch (temp[0].Trim())
                            {
                                case "Server":
                                    server = temp[1].Trim();
                                    break;
                                case "database":
                                    dbService = temp[1].Trim();
                                    break;
                                case "uid":
                                    uid = temp[1].Trim();
                                    break;
                                case "pwd":
                                    pwd = temp[1].Trim();
                                    break;
                            }
                            
                            break;
                    
                        case "OracleDBFactory":
                            lblserver.Text = "服务器名称（任意值）";
                            //"Data Source=Richard-liu;User ID=pdadmin;Password=pdadmin ; MAX Pool Size = 512 "
                            switch (temp[0].Trim())
                            {
                                case "Data Source":
                                    dbService = temp[1].Trim();
                                    break;
                                case "User ID":
                                    uid = temp[1].Trim();
                                    break;
                                case "Password":
                                    pwd = temp[1].Trim();
                                    break;
                                //case "Unicode":
                                   // pwd = temp[1].Trim();
                                 //   break;

                                default:
                                    server = "DBServer";
                                    break;
                            }
                            break;
                        case "OleDBFactory":
                            //"Provider=OraOLEDB.Oracle.1;Data Source=Richard-liu;User ID=sa;Password= ; MAX Pool Size = 512 "
                            lblserver.Text = "OLEDB数据库提供者";
                            switch (temp[0].Trim())
                            {
                                case "Data Source":
                                    dbService = temp[1].Trim();
                                    break;
                                case "User ID":
                                    uid = temp[1].Trim();
                                    break;
                                case "Password":
                                    pwd = temp[1].Trim();
                                    break;
                                case "Provider":
                                    server = temp[1].Trim();
                                    break;
                            }
                            break;
                        default:
                            lblserver.Text = "数据库服务器计算机名";
                            //"Server = (local) ; uid = SA ; pwd =; database = SSWCMS ; MAX Pool Size = 512 "
                            switch (temp[0].Trim())
                            {
                                case "Server":
                                    server = temp[1].Trim();
                                    break;
                                case "database":
                                    dbService = temp[1].Trim();
                                    break;
                                case "uid":
                                    uid = temp[1].Trim();
                                    break;
                                case "pwd":
                                    pwd = temp[1].Trim();
                                    break;
                                default:
                                    server = "(local)";
                                    uid = "SA";
                                    
                                    dbService = "SSWCMS";
                                    break;
                            }

                           
                            break;
                    }
                }
                tbserver.Text = server;
                tbdbService.Text = dbService;
                tbuid.Text = uid;
                tbpwd.Text = pwd;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {

            if (this.tbserver.Text.Trim().Length == 0)
            {
                MessageBox.Show("数据库服务器计算机名不允许是空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.tbdbService.Text.Trim().Length == 0)
            {
                MessageBox.Show("数据库服务名称不允许是空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.tbuid.Text.Trim().Length == 0)
            {
                MessageBox.Show("登录数据库的用户名不允许是空值！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show("您确认要保存数据库连接配置文件信息吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            try
            {
               // string dbf = dbo.GetValue("DBFactory");
                string key="",connstr="",pwd="";
                if (_connectType == "Manage")
                {
                    key="ManConnString";
                }
                else //if (_connectType == "Monitor")
                {
                    key = "ConnString";
                }
                if (this.tbpwd.Text.Trim() == "")
                {
                    pwd = null;
                }
                else
                {
                    pwd = this.tbpwd.Text.Trim();
                }

                switch (dbf)
                {
                    case "SQLServerDBFactory":
                        //"Server = (local) ; uid = SA ; pwd =; database = SSWCMS ; MAX Pool Size = 512 "
                        connstr = "Server = " + tbserver.Text.Trim() + " ; uid = " + tbuid.Text.Trim() + " ; pwd =" + pwd + "; database = " + tbdbService.Text.Trim() + " ; MAX Pool Size = 512 ";
                        break;

                    case "OracleDBFactory":
                        //"Data Source=Richard-liu;User ID=pdadmin;Password=pdadmin ; MAX Pool Size = 512 "
                        connstr = "Data Source=" + tbdbService.Text.Trim() + " ; User ID= " + tbuid.Text.Trim() + " ; Password=" + pwd + "; Unicode=True ; MAX Pool Size = 512 ";
                        break;
                    case "OleDBFactory":
                        //"Provider=OraOLEDB.Oracle.1;Data Source=Richard-liu;User ID=sa;Password= ; MAX Pool Size = 512 "
                        connstr = "Provider="+ tbserver.Text.Trim() +";Data Source=" + tbdbService.Text.Trim() + " ; User ID= " + tbuid.Text.Trim() + " ; Password=" + pwd + " ; MAX Pool Size = 512 ";
                        break;
                    default:
                        break;
                }
                dbo.SetValue(key, connstr);
                if (MessageBox.Show("数据库连接配置文件信息保存成功！确认要退出本系统，重新启动程序吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    Application.Exit();
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            if (_connectType == "Monitor"&& FrmMain.FormInstance.notifyIcon1.Visible==false)
            {
                Application.Exit();
            }

        }
    }
}