using JWH;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DDUtilityApp.DATA
{

    /// <summary>
    /// Log Server 접속정보
    /// </summary>
    public class LogServer
    {

        #region [ Properties ] ------------------------------------------------

        public string Name { get; set; }

        public string DBConnectionString { get; set; }

        public Dictionary<string, Account> NetworkAccount { get; set; } = new Dictionary<string, Account>();

        public string FTPAddress { get; set; }

        public int FTPPort { get; set; } = 21;

        public string FTPUserID { get; set; }

        public string FTPPassword { get; set; }

        public string DBGetEquipments { get; set; }

        public string DBGetModelDetails { get; set; }

        public string DBGetModelInfo { get; set; }

        /// <summary>
        /// GetLogPath()에서 사용 할 Format 문자열을 설정하거나 가져옵니다.
        /// <para>{0}\eisap01_eislog\Log\\{1}</para>
        /// </summary>
        //public Dictionary<string, string> QueryGetLogPath { get; set; } = new Dictionary<string, string>();

        #endregion

        #region [ LogServer ] -------------------------------------------------

        public LogServer(string name, string dbConnectionString = "")
        {
            this.Name = name;
            this.DBConnectionString = dbConnectionString;
        }

        #endregion

        #region [ Method ] ----------------------------------------------------

        public DataTable GetEquipments()
        {
            SqlConnection sqlConnection = null;
            DataTable dt = null;

            try
            {
                if (string.IsNullOrEmpty(this.DBConnectionString)) throw new Exception($"ConnectionString 속성에 값이 존재하지 않습니다.");
                if (string.IsNullOrEmpty(this.DBGetEquipments)) throw new Exception($"QueryGetEquipment 속성에 값이 존재하지 않습니다.");

                sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = this.DBConnectionString;
                sqlConnection.Open();

                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = this.DBGetEquipments;

                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
                sqlAdapter.Fill(ds);
                sqlConnection.Close();

                if (ds != null && ds.Tables.Count > 0) dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                throw ex;
            }
            finally
            {
                if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }

            return dt;
        }

        public DataTable GetModelDetails(string modelID)
        {
            SqlConnection sqlConnection = null;
            DataTable dt = null;

            try
            {
                if (string.IsNullOrEmpty(this.DBConnectionString)) throw new Exception($"ConnectionString 속성에 값이 존재하지 않습니다.");
                if (string.IsNullOrEmpty(this.DBGetEquipments)) throw new Exception($"QueryGetEquipment 속성에 값이 존재하지 않습니다.");

                sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = this.DBConnectionString;
                sqlConnection.Open();

                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = this.DBGetModelDetails;
                sqlCommand.Parameters.Add(new SqlParameter("@MODEL_ID", modelID));

                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
                sqlAdapter.Fill(ds);
                sqlConnection.Close();

                if (ds != null && ds.Tables.Count > 0) dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                throw ex;
            }
            finally
            {
                if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }

            return dt;
        }

        public DataTable GetModelInfo(string modelID)
        {
            SqlConnection sqlConnection = null;
            DataTable dt = null;

            try
            {
                if (string.IsNullOrEmpty(this.DBConnectionString)) throw new Exception($"ConnectionString 속성에 값이 존재하지 않습니다.");
                if (string.IsNullOrEmpty(this.DBGetEquipments)) throw new Exception($"QueryGetEquipment 속성에 값이 존재하지 않습니다.");

                sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = this.DBConnectionString;
                sqlConnection.Open();

                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = this.DBGetModelInfo;
                sqlCommand.Parameters.Add(new SqlParameter("@MODEL_ID", modelID));

                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
                sqlAdapter.Fill(ds);
                sqlConnection.Close();

                if (ds != null && ds.Tables.Count > 0) dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                throw ex;
            }
            finally
            {
                if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }

            return dt;
        }

        public string GetLogPath(string serverIP, string equipmentID)
        {
            try
            {
                Account account = this.NetworkAccount[serverIP];
                return $@"\\{account.IPAddress}\{account.DefaultPath}{equipmentID}\";
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                return string.Empty;
            }
        }

        public DataSet ExecuteQuery(string query)
        {
            SqlConnection sqlConnection = null;
            DataSet ds = new DataSet();

            try
            {
                if (string.IsNullOrEmpty(this.DBConnectionString)) throw new Exception($"ConnectionString 속성에 값이 존재하지 않습니다.");

                sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = this.DBConnectionString;
                sqlConnection.Open();

                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.CommandTimeout = 5000;
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.Text;
                sqlCommand.CommandText = query;

                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
                sqlAdapter.Fill(ds);
                sqlConnection.Close();
            }
            catch (Exception ex)
            {
                XLogger.Instance.Fatal(ex);
                throw ex;
            }
            finally
            {
                if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }

            return ds;
        }

        public override string ToString()
        {
            return this.Name;
        }

        #endregion

    }

    public class Account
    {

        public AccessType Access { get; set; } = AccessType.FTPS;

        public string IPAddress { get; set; }

        public string UserID { get; set; }

        public string Password { get; set; }

        public string DefaultPath { get; set; }

        public Account()
        {
        }

        public Account(string ipAddress, string uid, string pwd, string defaultPath = "", AccessType access = AccessType.SMB)
        {
            this.IPAddress = ipAddress;
            this.UserID = uid;
            this.Password = pwd;
            this.DefaultPath = defaultPath;
            this.Access = access;
        }

    }


}
