using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Edu.Common
{
    /// <summary>
    /// 全局配置
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 配置文件注入
        /// </summary>
        public readonly static IConfiguration Configuration;
        static Config()
        {
            try
            {
                Configuration = new ConfigurationBuilder()
                .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                .Build();
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "Config");
            }
        }

        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            try
            {
                return Configuration.GetConnectionString(key);
            }
            catch
            {
                return "";
            }
        }


        /// <summary>
        /// 读取配置文件key的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ReadConfigKey(string key, string subKey = "")
        {
            string valueStr = "";
            try
            {
                if (!string.IsNullOrWhiteSpace(subKey))
                {
                    valueStr = Configuration.GetSection(key)[subKey].ToString();
                }
                else
                {
                    valueStr = Configuration.GetSection(key).Value;
                }
            }
            catch
            {
            }
            return valueStr;
        }

        /// <summary>
        /// 异常日志保存路径
        /// </summary>
        public static string LogPath
        {
            get
            {
                string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log/error");
                return logPath.ToString();
            }
        }

        /// <summary>
        /// 打印日志保存路径
        /// </summary>
        public static string InofLogPath
        {
            get
            {
                string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log/info");
                return logPath.ToString();
            }
        }

        /// <summary>
        /// 接口请求日志保存路径
        /// </summary>
        public static string RequestLogPath
        {
            get
            {
                string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log/request");
                return logPath.ToString();
            }
        }

        /// <summary>
        /// JWT加密秘钥
        /// </summary>
        public static string JwtSecretKey
        {
            get
            {
                return ReadConfigKey("JwtSecretKey");
            }
        }

        /// <summary>
        /// 过期时间，单位为秒
        /// </summary>
        public static int JwtExpirTime
        {
            get
            {
                Int32.TryParse(ReadConfigKey("JwtExpirTime"), out int JwtExpirTime);
                return JwtExpirTime;
            }
        }


        /// <summary>
        /// 阿里云oss域名
        /// </summary>
        public static string GetOssFileUrl
        {
            get
            {
                return ReadConfigKey("ViewFileSiteUrl");
            }
        }

        public static string ErpViewFileSiteUrl
        {
            get
            {
                return ReadConfigKey("ErpViewFileSiteUrl");
            }
        }

        public static string ErpUrl
        {
            get
            {
                return ReadConfigKey("ErpUrl");
            }
        }

        /// <summary>
        /// 企业应用APP地址
        /// </summary>
        public static string WorkAPPDomain
        {
            get
            {
                return ReadConfigKey("WorkAPPDomain");
            }
        }

        /// <summary>
        /// 获取Oss文件全路径
        /// </summary>
        /// <param name="path">文件相对路径</param>
        /// <returns></returns>
        public static string GetFileUrl(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return null;
            }
            else
            {
                string p = @"(http|https)://(?<domain>[^(:|/]*)";
                Regex reg = new Regex(p, RegexOptions.IgnoreCase);
                string value = reg.Match(path).Value;
                if (!string.IsNullOrEmpty(value))
                {
                    return path;
                }
                else
                {
                    return GetOssFileUrl + path;
                }
            }
        }

        /// <summary>
        /// 获取erp站点图片
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetErpFileUrl(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return null;
            }
            else
            {
                string p = @"(http|https)://(?<domain>[^(:|/]*)";
                Regex reg = new Regex(p, RegexOptions.IgnoreCase);
                string value = reg.Match(path).Value;
                if (!string.IsNullOrEmpty(value))
                {
                    return path;
                }
                else
                {
                    return ErpViewFileSiteUrl + path;
                }
            }
        }

        /// <summary>
        /// 获取文件相对路径
        /// </summary>
        public static string GetFilePath(string path)
        {
            if (!string.IsNullOrWhiteSpace(path))
            {
                string p = @"(http|https)://(?<domain>[^(:|/]*)";
                Regex reg = new Regex(p, RegexOptions.IgnoreCase);
                string value = reg.Match(path).Value;
                if (!string.IsNullOrEmpty(value))
                {
                    Uri url = new Uri(path);
                    return url.AbsolutePath;
                }
                else
                {
                    return path;
                }
            }
            else
            {
                return path;
            }
        }

        /// <summary>
        /// 上传站点地址
        /// </summary>
        public static string UploadSiteUrl
        {
            get
            {
                return ReadConfigKey("UploadSiteUrl");
            }
        }

        /// <summary>
        /// 获取wkhtmltopdf路径
        /// </summary>
        public static string wkhtmltopdfConfig
        {
            get
            {
                return ReadConfigKey("WkHtmlToPdfPath");
            }
        }

        /// <summary>
        /// 上传文件预览站点
        /// </summary>
        public static string ViewFileSiteUrl
        {
            get
            {
                return ReadConfigKey("ViewFileSiteUrl");
            }
        }

        /// <summary>
        /// 获取虚拟目录
        /// </summary>
        public static string VirtualDirectory
        {
            get
            {
                return ReadConfigKey("VirtualDirectory");
            }
        }

        /// <summary>
        /// Mongo连接地址
        /// </summary>
        public static string Mongo
        {
            get
            {
                return ReadConfigKey("Mongo");
            }
        }

        /// <summary>
        /// 获取MongoDBName
        /// </summary>
        public static string MongoDBName
        {
            get
            {
                return ReadConfigKey("MongoDBName");
            }
        }

        /// <summary>
        /// 默认密码
        /// </summary>
        public static string DefaultPwd
        {
            get { return "123456"; }
        }

        /// <summary>
        /// 是否线上
        /// </summary>
        public static bool IsOnline
        {
            get
            {
                bool.TryParse(ReadConfigKey("IsOnline"), out bool _isOnline);
                return _isOnline;
            }
        }

        /// <summary>
        /// 是否发送短信
        /// </summary>
        public static int IsSendMsg
        {
            get
            {
                int.TryParse(ReadConfigKey("IsSendMsg"), out int _isSendMsg);
                return _isSendMsg;
            }
        }
        /// <summary>
        /// 导出文件保存路径
        /// </summary>
        public static string FileExportPath
        {
            get
            {
                string fileExportPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "/upfile/ExportFile");
                if (!Directory.Exists(fileExportPath))
                {
                    Directory.CreateDirectory(fileExportPath);
                }
                return fileExportPath;
            }
        }

        /// <summary>
        /// 退课流程编号
        /// </summary>
        public static int BackClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("BackClassFlowId"), out int BackClassFlowId);
                return BackClassFlowId;
            }
        }

        /// <summary>
        /// 调课流程编号
        /// </summary>
        public static int ChangeClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("ChangeClassFlowId"), out int ChangeClassFlowId);
                return ChangeClassFlowId;
            }
        }

        /// <summary>
        /// 停课流程编号
        /// </summary>
        public static int StopClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("StopClassFlowId"), out int StopClassFlowId);
                return StopClassFlowId;
            }
        }

        /// <summary>
        /// 临时上课邀请流程编号
        /// </summary>
        public static int TempInvitationClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("TempInvitationClassFlowId"), out int TempInvitationClassFlowId);
                return TempInvitationClassFlowId;
            }
        }


        /// <summary>
        /// 订单转班流程编号
        /// </summary>
        public static int OrderTransClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("OrderTransClassFlowId"), out int OrderTransClassFlowId);
                return OrderTransClassFlowId;
            }
        }


        /// <summary>
        /// 订单分拆流程编号
        /// </summary>
        public static int OrderSplitClassFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("OrderSplitClassFlowId"), out int OrderSplitClassFlowId);
                return OrderSplitClassFlowId;
            }
        }
        /// <summary>
        /// 学生请假流程编号
        /// </summary>
        public static int StudentLeaveFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("StudentLeaveFlowId"), out int StudentLeaveFlowId);
                return StudentLeaveFlowId;
            }
        }

        /// <summary>
        /// 组卷审批流程编号
        /// </summary>
        public static int ZuJuanFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("ZuJuanFlowId"), out int ZuJuanFlowId);
                return ZuJuanFlowId;
            }
        }

        

        /// <summary>
        /// 考试申请流程编号
        /// </summary>
        public static int ExamApplyFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("ExamApplyFlowId"), out int ExamApplyFlowId);
                return ExamApplyFlowId;
            }
        }

        /// <summary>
        /// 投稿审批流程编号
        /// </summary>
        public static int ContributeFlowId
        {
            get
            {
                int.TryParse(ReadConfigKey("ContributeFlowId"), out int ContributeFlowId);
                return ContributeFlowId;
            }
        }


        /// <summary>
        /// okr关注事项配置
        /// </summary>
        public static string OKRMatterValue
        {
            get
            {
                return ReadConfigKey("OKRMatterValue");
            }
        }

        /// <summary>
        /// 分区校长岗位编号
        /// </summary>
        public static int SchoolPostId
        {
            get
            {
                int.TryParse(ReadConfigKey("SchoolPostId"), out int SchoolPostId);
                return SchoolPostId;
            }
        }

        /// <summary>
        /// 教务长岗位编号
        /// </summary>
        public static int JiaoWuZhang
        {
            get
            {
                int.TryParse(ReadConfigKey("JiaoWuZhang"), out int JiaoWuZhang);
                return JiaoWuZhang;
            }
        }

        /// <summary>
        /// 数字选择题前缀符号
        /// </summary>
        public static string NumberQuestionChar
        {
            get
            {
                return "@";
            }
        }

        /// <summary>
        /// 留学就业部门主管审核编号
        /// </summary>
        public static int StudyAbroadDirectorId
        {
            get
            {
                int.TryParse(ReadConfigKey("StudyAbroadDirectorId"), out int studyAbroadDirectorId);
                return studyAbroadDirectorId;
            }
        }

        /// <summary>
        /// 留学就业部门负责人审核编号
        /// </summary>
        public static int StudyAbroadManagerId
        {
            get
            {
                int.TryParse(ReadConfigKey("StudyAbroadManagerId"), out int studyAbroadManagerId);
                return studyAbroadManagerId;
            }
        }

        /// <summary>
        /// 获取合同默认抬头
        /// </summary>
        public static string ContractDefaultTitle
        {
            get
            {
                return ReadConfigKey("ContractDefaultTitle");
            }
        }

        /// <summary>
        /// 是否推送消息
        /// </summary>
        public static int IsPushMessage
        {
            get
            {
                int.TryParse(ReadConfigKey("IsPushMessage"), out int IsPushMessage);
                return IsPushMessage;
            }
        }

        /// <summary>
        /// 超过下班时间多少分钟数不能打卡
        /// </summary>
        public static int MoreThanMinutes
        {
            get
            {
                int.TryParse(ReadConfigKey("MoreThanMinutes"), out int MoreThanMinutes);
                return MoreThanMinutes;
            }
        }

        /// <summary>
        /// 连续缺课多少课时
        /// </summary>
        public static int AbsentHours
        {
            get
            {
                int.TryParse(ReadConfigKey("AbsentHours"), out int AbsentHours);
                if (AbsentHours == 0)
                {
                    AbsentHours = 12;
                }
                return AbsentHours;
            }
        }

        /// <summary>
        /// 消息推送AppId
        /// </summary>
        public static string PushAppId
        {
            get
            {
                return ReadConfigKey("PushAppId");
            }
        }


        /// <summary>
        /// 收入单据
        /// </summary>
        public static string IncomeFinanceApi
        {
            get
            {
                return ReadConfigKey("IncomeFinanceApi");
            }
        }
        /// <summary>
        /// 微信结算率
        /// </summary>
        public static string SettlementRate
        {
            get
            {
                return ReadConfigKey("SettlementRate");
            }
        }

        /// <summary>
        /// 销售发放提成的前提比例
        /// </summary>
        public static string SellCommissionFFBL
        {
            get
            {
                return ReadConfigKey("SellCommissionFFBL");
            }
        }

        /// <summary>
        /// 收入请求加密
        /// </summary>
        public static string FinanceKey
        {
            get
            {
                return ReadConfigKey("FinanceKey");
            }
        }
        /// <summary>
        /// 微信支付成功回调地址
        /// </summary>
        public static string sTenpayNotifyUrl
        {
            get
            {
                return ReadConfigKey("sTenpayNotifyUrl");
            }
        }
        /// <summary>
        /// 微信退款成功回调地址
        /// </summary>
        public static string sTenpayNotifyRefundUrl
        {
            get
            {
                return ReadConfigKey("sTenpayNotifyRefundUrl");
            }
        }


        /// <summary>
        /// 甲鹤教育接口请求地址
        /// </summary>
        public static string DmcMallApi
        {
            get
            {
                return ReadConfigKey("DmcMallApi");
            }
        }


        /// <summary>
        /// 甲鹤教育商户id
        /// </summary>
        public static string JHTenantId
        {
            get
            {
                return ReadConfigKey("JHTenantId");
            }
        }

        //JHMallUserOpenId
        /// <summary>
        /// 甲鹤教育固定用户OpenId
        /// </summary>
        public static string JHMallUserOpenId
        {
            get
            {
                return ReadConfigKey("JHMallUserOpenId");
            }
        }


        /// <summary>
        /// 甲鹤教育小程序id
        /// </summary>
        public static string JHMallBaseId
        {
            get
            {
                return ReadConfigKey("JHMallBaseId");
            }
        }

        /// <summary>
        /// 电商加密
        /// </summary>
        public static string MallJwtSecretKey
        {
            get
            {
                return ReadConfigKey("MallJwtSecretKey");
            }
        }

        /// <summary>
        /// 值班日期验证
        /// </summary>
        public static DateTime DutyDate
        {
            get
            {
                DateTime.TryParse(ReadConfigKey("DutyDate"), out DateTime dt);
                return dt;
            }
        }

        /// <summary>
        /// 获取字母排序
        /// </summary>
        public static string ClassNoLetterStr
        {
            get
            {
                return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            }
        }

        /// <summary>
        /// 公告字典中的key
        /// </summary>
        public static string Notice_BaseKey
        {
            get
            {
                return ReadConfigKey("Notice_BaseKey");
            }
        }

        /// <summary>
        /// 合同功能菜单code（学管）
        /// </summary>
        public static string EducationContractSchool
        {
            get
            {
                return ReadConfigKey("EducationContractSchool");
            }
        }


        /// <summary>
        /// 合同功能菜单code（行政）
        /// </summary>
        public static string EducationContractAdmin
        {
            get
            {
                return ReadConfigKey("EducationContractAdmin");
            }
        }

        /// <summary>
        /// 是否开启调课验证
        /// </summary>
        public static int IsOpenChangeClassVerify
        {
            get
            {
                int.TryParse(ReadConfigKey("IsOpenChangeClassVerify"), out int IsOpenChangeClassVerify);
                return IsOpenChangeClassVerify;
            }
        }

        /// <summary>
        /// 甲鹤小程序AppId
        /// </summary>
        public static string AppID
        {
            get { return "wx38e054ee42b054f4"; }
        }

        /// <summary>
        /// 甲小鹤AppId
        /// </summary>
        public static string JiaXiaoHeAppId {
            get { return "wx0f4b451960f8ef29"; }
        }

        /// <summary>
        /// 甲小鹤AppSecret
        /// </summary>
        public static string JiaXiaoHeAppSecret
        {
            get { return "62332ef58d16280fc05613fc61ecff1d"; }
        }

        /// <summary>
        /// 甲鹤小程序AppSecret
        /// </summary>
        public static string AppSecret
        {
            get { return "d3ad4699265ba885ae2c8b65bf574ea5"; }
        }

        /// <summary>
        /// 微信小程序Api地址
        /// </summary>
        public static string WechatApi
        {
            get { return "https://api.weixin.qq.com/"; }
        }

        /// <summary>
        /// 是否是本地服务器
        /// </summary>
        public static int IsLocal
        {
            get
            {
                Int32.TryParse(ReadConfigKey("IsLocal"), out int _IsLocal);
                return _IsLocal;
            }
        }

        /// <summary>
        /// 临时访问小程序二维码Token
        /// </summary>
        public static string TempToken
        {
            get
            {
                return ReadConfigKey("TempToken");
            }
        }


        /// <summary>
        /// 公众号的AppId
        /// </summary>
        public static string WeChatAccountAppId
        {
            get
            {
                return ReadConfigKey("WeChatAccountAppId");
            }
        }

        /// <summary>
        /// 公众号的AppSecret
        /// </summary>
        public static string WeChatAccountAppSecret
        {
            get
            {
                return ReadConfigKey("WeChatAccountAppSecret");
            }
        }

        /// <summary>
        /// 新业绩提成启用时间
        /// </summary>
        public static string NewAchievementsEnable
        {
            get
            {
                return ReadConfigKey("NewAchievementsEnable");
            }
        }
    }
}