using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.Tools;
using Microsoft.Extensions.Logging;
using PoJun.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace ASHermed.SinoInterface.Api.Service
{
    public class VerifyCodeLogService : BaseService, IVerifyCodeLogService
    {
        #region 初始化

        /// <summary>
        /// 用户登录服务
        /// </summary>
        private IPubVerifyCodeRepository _pubVerifyCodeRepository;
        private AppConfigurtaion _appConfigurtaion;
        private IZhuTongSMSService _zhuTongSMSService;
        private ILogger<VerifyCodeLogService> _logger;
        public VerifyCodeLogService(IPubVerifyCodeRepository pubVerifyCodeRepository, AppConfigurtaion appConfigurtaion, IZhuTongSMSService zhuTongSMSService, ILogger<VerifyCodeLogService> logger)
        {
            _pubVerifyCodeRepository = pubVerifyCodeRepository;
            _appConfigurtaion = appConfigurtaion;
            _zhuTongSMSService = zhuTongSMSService;
            _logger = logger;
        }

        #endregion
        public int CheckCode(string acc, string code)
        {
            var codeLog = _pubVerifyCodeRepository.CheckCode(acc, code);
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //DateTime ndate = DateTime.Now;
            if (codeLog == null)
            {
                return 0;
            }
            else if (codeLog.Create_Time.AddMinutes(30) < codeLog.Cur_Time)
            {
                return -1;
            }
            else
            {
                return 1;
            }
            //return pubVerifyCodeRepository.CheckCode(acc, code);
        }

        //public PubVerifyCodeLogModel selectByAccCode(string acc, string code)
        //{
        //    return pubVerifyCodeRepository.CheckCode(acc, code);
        //}

        public int CheckCodeRes(string acc, string code)
        {

            var codeLog = _pubVerifyCodeRepository.CheckCode(acc, code);
            //var dasd = codeLog.Create_Time.AddMilliseconds(appConfigurtaion.GetCodePeriod.ToLong());
            if (codeLog == null)
            {
                return 0;
            }
            else if (codeLog.Cur_Time > codeLog.Create_Time.AddMilliseconds(_appConfigurtaion.GetCodePeriod.ToLong()))
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }

        public List<Pub_Verify_Code_Log> FindByIp(string ip)
        {
            return _pubVerifyCodeRepository.FindByIp(ip);
        }

        public List<Pub_Verify_Code_Log> FindByAcc(string acc)
        {
            return _pubVerifyCodeRepository.FindByAcc(acc);
        }

        public string GetCode(string phone, string ip)
        {
            string code = GenerateCode();
            Pub_Verify_Code_Log pubVerifyCodeLog = new Pub_Verify_Code_Log();
            pubVerifyCodeLog.Code = code;
            pubVerifyCodeLog.Account_Code = phone;
            pubVerifyCodeLog.IP_Address = ip;
            pubVerifyCodeLog.Create_Time = DateTime.Now;
            _pubVerifyCodeRepository.InsertPubVerifyCode(pubVerifyCodeLog);
            return code;
        }

        private string GenerateCode()
        {
            var retCode = string.Empty;
            var num = _appConfigurtaion.GetCodeDigit.ToInt();
            Random random = new Random();
            for (int i = 0; i < num; i++)
            {
                retCode += random.Next(0, 9).ToString();
            }
            return retCode;
        }

        public Dictionary<string, object> SendToSms(string account, string ip, int language)
        {
            string code = GetCode(account, ip);
            //只发短信
            string content = _appConfigurtaion.GetCnCodeTemp.Replace("[code]", code);
            if (language == 2)
            {
                content = _appConfigurtaion.GetEnCodeTemp.Replace("[code]", code);
            }
            string sign = "【" + _appConfigurtaion.GetZhuTongSMSCNSgin + "】";
            if (language == 2)
            {
                sign = "【" + _appConfigurtaion.GetZhuTongSMSENSgin + "】";
            }
            content += sign;
            _logger.LogError("codecontra----------" + content);
            var res = _zhuTongSMSService.SendSMS(account, content);
            if (res.Code == 200)
            {
                return new Dictionary<string, object>()
                {
                    { "code",1},
                    { "msg","发送成功"},
                    { "data",res.MsgId}
                };
            }
            else
            {
                return new Dictionary<string, object>()
                {
                    { "code",0},
                    { "msg","发送失败"},
                    { "data",null}
                };
            }
        }

        /// <summary>
        /// 一般发一条短信
        /// </summary>
        /// <param name="account">手机号</param>
        /// <param name="content">内容</param>
        /// <param name="language"></param>
        /// <returns></returns>
        public bool SendCusSms(string account, string content, int language)
        {
            if (string.IsNullOrEmpty(account))
            {
                return false;
            }
            string sign = "【" + _appConfigurtaion.GetZhuTongSMSCNSgin + "】";
            if (language == 2)
            {
                sign = "【" + _appConfigurtaion.GetZhuTongSMSENSgin + "】";
            }
            content += sign;
            _logger.LogError("codecontra----------" + content);
            var res = _zhuTongSMSService.SendSMS(account, content);
            if (res.Code == 200)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool SendCignaSMS(string phone, string content)
        {
            string sign = "【" + _appConfigurtaion.GetZhuTongSMSENSgin + "】";
            var res = _zhuTongSMSService.SendSMS(phone, sign + content);
            return true;
        }

    }
}
