using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MOKA_Factory_Tools.Database
{
    public class ConnectionParameters
    {
#if DEBUG
        public ConnectionParameters()
        {
            DatabaseServer = @"DESKTOP-71AIDNV\SQLEXPRESS";
            DatabaseName = "MES";
            connectionStringTrusted = string.Format(_msdbconnect_trusted, DatabaseServer, DatabaseName);
        }
#endif
        #region 连接串
        // 无端口号;
        private const string _msdbconnect = "Data Source={0};Initial Catalog={1};User ID={2};Password={3}";
        // 有端口号;
        private const string _msdbconnect_port = "Data Source={0},{1};Initial Catalog={2};User ID={3};Password={4}";
        // windows信任连接;
        private const string _msdbconnect_trusted = "server={0};database={1};Trusted_Connection=SSPI";
        #endregion

        #region 私有变量;
        /// <summary>
        /// 数据库连接串;
        /// </summary>
        private string connectionString = "";
        private string connectionStringTrusted = "";
        #endregion

        #region 公共属性;
        /// <summary>
        /// 数据库服务器;
        /// </summary>
        public string DatabaseServer { get; set; } = "10.126.64.131";
        /// <summary>
        /// 数据库账号;
        /// </summary>
        public string DatabaseAccount { get; set; } = "XMUpdate";
        /// <summary>
        /// 数据库密码;
        /// </summary>
        public string DatabasePassword { get; set; } = "695Xm@123#";
        /// <summary>
        /// 数据库名称;
        /// </summary>
        public string DatabaseName { get; set; } = "MESOtherData";
        /// <summary>
        /// 数据库端口号;
        /// </summary>
        public uint DatabasePortNumber { get; set; } = 0;

        /// <summary>
        /// 连接超时值;
        /// </summary>
        public uint ConnectTimeout { get; set; } = 3000;

        [JsonIgnore]
        /// <summary>
        /// 是否设置了数据库连接参数;
        /// </summary>
        public bool IsSetParameters { get; private set; } = false;

        [JsonIgnore]
        /// <summary>
        /// 数据库连接串;
        /// </summary>
        public string ConnectionString
        {
            get
            {
#if DEBUG
                return connectionStringTrusted;
#else
                if (IsSetParameters)
                    return connectionString;
                else
                {
                    SetConnectParameters();
                    return connectionString;
                }
#endif
            }
        }

        [JsonIgnore]
        public string ConnectionStringTrusted
        {
            get
            {
                if (IsSetParameters)
                    return connectionStringTrusted;
                else
                {
                    SetConnectParameters();
                    return connectionStringTrusted;
                }
            }
        }
#endregion

        public void SetConnectParameters()
        {
            try
            {
                // 标记连接参数已设置;
                IsSetParameters = true;

                // 生成默认的连接串;
                connectionString = DatabasePortNumber == 0
                    ? string.Format(_msdbconnect, DatabaseServer, DatabaseName, DatabaseAccount, DatabasePassword)
                    : string.Format(_msdbconnect_port, DatabaseServer, DatabasePortNumber, DatabaseName, DatabaseAccount, DatabasePassword);

                connectionStringTrusted = string.Format(_msdbconnect_trusted, DatabaseServer, DatabaseName);
            }
            catch
            {
                throw;
            }
        }

        public void SetConnectParameters(string databaseServer, string databaseName, string databaseAccount, string databasePassword, uint databasePortNumber=0)
        {
            DatabaseServer = databaseServer;
            DatabaseName = databaseName;
            DatabaseAccount = databaseAccount;
            DatabasePassword = databasePassword;
            DatabasePortNumber = databasePortNumber;

            // 标记连接参数已设置;
            IsSetParameters = true;

            try
            {
                // 生成默认的连接串;
                connectionString = DatabasePortNumber == 0
                    ? string.Format(_msdbconnect, DatabaseServer, DatabaseName, DatabaseAccount, DatabasePassword)
                    : string.Format(_msdbconnect_port, DatabaseServer, DatabasePortNumber, DatabaseName, DatabaseAccount, DatabasePassword);

                connectionStringTrusted = string.Format(_msdbconnect_trusted, DatabaseServer, DatabaseName);
            }
            catch
            {
                throw;
            }
        }
    }
}
