using Ease.NetCore.DataAccess;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace OnlineSalesAutoCrop.CoreAPI.Models.Global
{
	/// <summary>
	/// AppSettings
	/// </summary>
	public class AppSettings
	{
		/// <summary>
		/// Default Database Connection to be used 
		/// </summary>
		[Required]
		public string DefaultDB { get; set; }

		/// <summary>
		/// Used to dec from User info got over http
		/// </summary>
		public string CipherSecretKey { get; set; }

		/// <summary>
		/// Gets or sets a value that indicates whether a cookie is inaccessible by client-side script.
		/// </summary>
		public bool CookieHttpOnly { get; set; }

		/// <summary>
		/// Gets or sets a value that indicates whether to transmit the cookie using Secure Sockets Layer (SSL)--that is, over HTTPS only.
		/// </summary>
		public bool CookieSecure { get; set; }

		/// <summary>
		/// Gets or sets the max-age for the cookie in minutes.
		/// </summary>
		public int CookieLifeTime { get; set; } = 360;

		/// <summary>
		/// Gets or sets the value for the SameSite attribute of the cookie. 
		/// -1: No SameSite field will be set, the client should follow its default cookie policy.
		/// 0: Indicates the client should disable same-site restrictions.
		/// 1: Indicates the client should send the cookie with "same-site" requests, and with "cross-site" top-level navigations.
		/// 2: Indicates the client should only send the cookie with "same-site" requests.
		/// </summary>
		public int CookieSameSite { get; set; }

		/// <summary>
		/// For Jwt Token
		/// </summary>
		public string JwtCryptoKey { get; set; }
		public string JwtIssuer { get; set; }
		public string JwtAudience { get; set; }
		public bool JwtValidateIssuer { get; set; }
		public bool JwtValidateAudience { get; set; }
		public int RefreshTokenDuration { get; set; }
		public int AccessTokenDuration { get; set; }
		
        /// <summary>
        /// Folder management
        /// </summary>
        public string UploadFolder { get; set; }
		public string ProfileImageFolder { get; set; }
		public string ReportFolder { get; set; }
		public string FileProcessFolder { get; set; }

		/// <summary>
		/// For user password
		/// </summary>
		[Required]
		public string PwdSecretKey { get; set; }

		/// <summary>
		/// For SMS Management
		/// </summary>
		public string SmsSecretKey { get; set; }
		public string SmsApiUrl { get; set; }
		public string SmsAccessInfo { get; set; }

		/// <summary>
		/// Email management
		/// </summary>
		public int EmailPort { get; set; }
		public bool EmailEnableSsl { get; set; }
		public string EmailHost { get; set; }
		public string EmailSenderName { get; set; }
		public string EmailSenderIp { get; set; }
		public string EmailSenderId { get; set; }


		private string _emailSenderPwd;
		public string EmailSenderPwd
		{
			get
			{
				if (string.IsNullOrEmpty(_emailSenderPwd) || string.IsNullOrEmpty(PwdSecretKey))
					return string.Empty;

				string secretKey = GlobalFunctions.ConvertFromBase64String(PwdSecretKey);
				return Ease.NetCore.Utility.Global.CipherFunctions.DecryptByAES(privateKey: secretKey, publicKey: secretKey, data: _emailSenderPwd, input: 2);
			}
			set { _emailSenderPwd = value; }
		}
		public string EmailBcc { get; set; }
		public string EmailErrorLogPath { get; set; }
		public bool EmailUseDefaultCredentials { get; set; }
		public int EmailTlsVersion { get; set; }

		/// <summary>
		/// CORS CorsHeaders
		/// </summary>
		public string CorsHeaders { get; set; }

		/// <summary>
		/// CORS CorsMethods
		/// </summary>
		public string CorsMethods { get; set; }

		/// <summary>
		/// CORS CorsOrigins
		/// </summary>
		public string CorsOrigins { get; set; }

		/// <summary>
		/// Logger enabled if the is there is value
		/// </summary>
		public string LoggerPath { get; set; }
		public int LoggerMinLevel { get; set; }

        /// <summary>
        /// Database connection Hangfire
        /// </summary>
        public string HangfireDb { get; set; }
		//private string _hangfireDb;
		//public string HangfireDb
		//{
		//	get
		//	{
		//		if (string.IsNullOrEmpty(_hangfireDb) || string.IsNullOrEmpty(PwdSecretKey))
		//			return string.Empty;

		//		string secretKey = GlobalFunctions.ConvertFromBase64String(PwdSecretKey);
		//		return Ease.NetCore.Utility.Global.CipherFunctions.DecryptByAES(privateKey: secretKey, publicKey: secretKey, data: _hangfireDb, input: 2);
		//	}
		//	set { _hangfireDb = value; }
		//}



		// <summary>
		/// Use for caching
		/// </summary>
		public EnumCacheType CacheType { get; set; } = EnumCacheType.InMemory;
		public string CacheUrl { get; set; }
		public string CacheSchemaName { get; set; }
		public string CacheTableName { get; set; }

		/// <summary>
		/// What's app messaging
		/// </summary>
		public string WaSenderId { get; set; }
		public string WaAuthToken { get; set; }
		public string WaAccountSid { get; set; }
		public string WaMsgSvcSid { get; set; }

		/// <summary>
		/// Rabbit MQ Information
		/// </summary>        
		public string RabbitMQHost { get; set; }
		public string RabbitMQUser { get; set; }

		private string _rabbitMQPwd;
		public string RabbitMQPwd
		{
			get
			{
				if (string.IsNullOrEmpty(_rabbitMQPwd) || string.IsNullOrEmpty(PwdSecretKey))
					return string.Empty;

				string secretKey = GlobalFunctions.ConvertFromBase64String(PwdSecretKey);
				return Ease.NetCore.Utility.Global.CipherFunctions.DecryptByAES(privateKey: secretKey, publicKey: secretKey, data: _rabbitMQPwd, input: 2);
			}
			set { _rabbitMQPwd = value; }
		}
		public int RabbitMQPort { get; set; }

		/// <summary>
		/// Rate Limit by Ip Address
		/// </summary>
		public string RLName { get; set; }
		public int RLQueueLimit { get; set; }
		public int RLPermitLimit { get; set; }
		public int RLWindowInSecond { get; set; }
		public bool RLAutoReplenishment { get; set; }
		public int RLQueueProcessingOrder { get; set; }

		/// <summary>
		/// AI Service Information
		/// </summary>
		private string _apiKeyOpenAI;
		public string ApiKeyOpenAI
		{
			get
			{
				if (string.IsNullOrEmpty(_apiKeyOpenAI) || string.IsNullOrEmpty(PwdSecretKey))
					return string.Empty;

				string secretKey = GlobalFunctions.ConvertFromBase64String(PwdSecretKey);
				return Ease.NetCore.Utility.Global.CipherFunctions.DecryptByAES(privateKey: secretKey, publicKey: secretKey, data: _apiKeyOpenAI, input: 2);
			}
			set { _apiKeyOpenAI = value; }
		}
		public string AIModel { get; set; }
		public string AICustomBaseUrl { get; set; }


		/// <summary>
		/// Google API Key
		/// </summary>
		private string _apiKeyGoogle;
		public string ApiKeyGoogle
		{
			get
			{
				if (string.IsNullOrEmpty(_apiKeyGoogle) || string.IsNullOrEmpty(PwdSecretKey))
					return string.Empty;

				string secretKey = GlobalFunctions.ConvertFromBase64String(PwdSecretKey);
				return Ease.NetCore.Utility.Global.CipherFunctions.DecryptByAES(privateKey: secretKey, publicKey: secretKey, data: _apiKeyGoogle, input: 2);
			}
			set { _apiKeyGoogle = value; }
		}


		/// <summary>
		/// Product Hierarchy Maximum Level
		/// </summary>
		public int PHMaxLevel { get; set; }

		/// <summary>
		/// Channel Hierarchy Maximum Level
		/// </summary>
		public int CHMaxLevel { get; set; }

		/// <summary>
		/// Market Hierarchy Maximum Level
		/// </summary>
		public int MHMaxLevel { get; set; }

		/// <summary>
		/// Market Hierarchy Maximum Level
		/// </summary>
		public int EPMaxLevel { get; set; }

		/// <summary>
		/// Use to view payslip
		/// </summary>
		public string PayslipPath { get; set; }

		/// <summary>
		/// AD ie Active Domain Settings
		/// </summary>
		public ADSettings ADConfig { get; set; }

		/// <summary>
		/// Api Settings
		/// </summary>
		[Required]
		public ApiSettings API { get; set; }
		 
        public AutoCropSAPApi AutoCropSAPApi { get; set; }

        /// <summary>
        /// Swagger Setting
        /// </summary>
        [Required]
		public Swagger Swagger { get; set; }

		/// <summary>
		/// DB Connection Node
		/// </summary>
		[Required]
		public List<DbConnectionNode> DbConfig { get; set; }

		/// <summary>
		/// Default DB Connection Node
		/// </summary>
		public DbConnectionNode DefaultConnection => GetConnectionNode(DefaultDB);

		/// <summary>
		/// DB Connection Node by name
		/// </summary>
		/// <param name="key">Connection node name</param>
		/// <returns></returns>
		public DbConnectionNode GetConnectionNode(string key)
		{
			if (string.IsNullOrEmpty(key))
				key = DefaultDB;

			if (DbConfig == null || DbConfig.Count <= 0 || string.IsNullOrEmpty(key))
				return null;

			return DbConfig.FirstOrDefault(x => x.ConnectionNode.Key == key);
		}
	}

	/// <summary>
	/// 
	/// </summary>
	public class DbConnectionNode
	{
		public ConnectionNode ConnectionNode { get; set; }
	}

	/// <summary>
	/// 
	/// </summary>
	public class ADSettings
	{
		public bool Enabled { get; set; }
		public string Domain { get; set; }
		public string Path { get; set; }
	}

	/// <summary>
	/// 
	/// </summary>
	public class ApiSettings
	{
		[Required]
		public string Title { get; set; }

		public string Description { get; set; }

		public ApiContact Contact { get; set; }

		public string TermsOfServiceUrl { get; set; }

		public ApiLicense License { get; set; }
	}

	/// <summary>
	/// 
	/// </summary>
	public class ApiContact
	{
		public string Name { get; set; }
		public string Email { get; set; }
		public string Url { get; set; }
	}

    /// <summary>
    /// AutoCrop Integration API Details
    /// </summary>
    public class AutoCropSAPApi
    {
        public string BaseUrl { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ApiLicense
	{
		public string Name { get; set; }
		public string Url { get; set; }
	}

	/// <summary>
	/// 
	/// </summary>
	public class Swagger
	{
		[Required]
		public bool Enabled { get; set; }
	}
}