using Hotline.Configurations;
using Hotline.Settings;
using Hotline.Share.Dtos.Settings;
using Microsoft.Extensions.Options;
using SqlSugar;
using System.Text.RegularExpressions;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.Systems
{
    public class SystemMobilAreaApplication : ISystemMobilAreaApplication, IScopeDependency
    {
        private readonly IRepository<SystemMobilArea> _systemMobilAreaRepository;
        private readonly IOptionsSnapshot<AppConfiguration> _appOptions;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="systemMobilAreaRepository"></param>
        /// <param name="appOptions"></param>
        public SystemMobilAreaApplication(IRepository<SystemMobilArea> systemMobilAreaRepository,
             IOptionsSnapshot<AppConfiguration> appOptions)
        {
            _systemMobilAreaRepository = systemMobilAreaRepository;
            _appOptions = appOptions;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ISugarQueryable<SystemMobilArea> GetList(SystemMobilAreaRequestDto dto)
        {
            return _systemMobilAreaRepository.Queryable()
                   .WhereIF(!string.IsNullOrEmpty(dto.TelCode), p => p.TelCode == dto.TelCode)
                   .WhereIF(!string.IsNullOrEmpty(dto.MobileCode), p => p.MobileCode.Contains(dto.MobileCode))
                   .WhereIF(!string.IsNullOrEmpty(dto.MobileAreaName), p => p.MobileAreaName.Contains(dto.MobileAreaName))
                   .WhereIF(!string.IsNullOrEmpty(dto.OFlag), p => p.OFlag == dto.OFlag)
                   .WhereIF(!string.IsNullOrEmpty(dto.OperatorName), p => p.OperatorName.Contains(dto.OperatorName))
                   .WhereIF(dto.StartTime.HasValue, p => p.CreationTime >= dto.StartTime)
                    .WhereIF(dto.EndTime.HasValue, p => p.CreationTime <= dto.EndTime)
                    .OrderByDescending(p => p.CreationTime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PhoneNum"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<SystemMobilAreaDto> GetPhoneCardArea(string PhoneNum, CancellationToken cancellationToken)
        {
            SystemMobilAreaDto areaDto = new SystemMobilAreaDto();
            if (true == string.IsNullOrEmpty(PhoneNum) || PhoneNum.Length < 6)
            {
                return areaDto;
            }

            // 验证电话号码是否是手机号码
            bool bCheckMobile = e_VerificationMobile(PhoneNum);
            // 电话归属编码
            string strPhoneCode = "";
            if (bCheckMobile)
            {
                // 手机号码第一位是否是0
                if (PhoneNum.Substring(0, 1) == "0")
                    strPhoneCode = PhoneNum.Substring(1, 7);
                else
                    strPhoneCode = PhoneNum.Substring(0, 7);

                var data = await _systemMobilAreaRepository.GetAsync(p => p.MobileCode.Trim() == strPhoneCode, cancellationToken);
                if (data != null)
                {
                    areaDto.OFlag = data.OFlag == "1" ? "电信" : data.OFlag == "2" ? "移动" : "联通";
                    areaDto.MobileAreaName = data.MobileAreaName;
                    areaDto.OperatorName = data.OperatorName;
                }
            }
            else
            {
                // 验证电话号码是否是座机号码
                bCheckMobile = e_VerificationTel(PhoneNum);
                if (true == bCheckMobile)
                {
                    strPhoneCode = PhoneNum.Substring(0, 3);

                    var data = await _systemMobilAreaRepository.GetAsync(p => p.TelCode.Trim() == strPhoneCode, cancellationToken);
                    if (data == null)
                    {
                        strPhoneCode = PhoneNum.Substring(0, 4);
                        data = await _systemMobilAreaRepository.GetAsync(p => p.TelCode.Trim() == strPhoneCode, cancellationToken);
                    }

                    if (data != null)
                    {
                        areaDto.MobileAreaName = data.MobileAreaName;
                    }
                }
            }
            return areaDto;
        }

        /// <summary>
        /// 验证电话外呼是否需要加0
        /// </summary>
        /// <param name="Mobile"></param>
        /// <returns></returns>
        public async Task<string> VaildMobile(string Mobile)
        {
            string strMobile = Mobile;
            // 验证是否存在0
            if (false == string.IsNullOrWhiteSpace(strMobile))
            {
                // 前缀是否为0
                string firstChar = strMobile.Substring(0, 1);
                if (firstChar == "0" && strMobile.Length > 1)
                {
                    strMobile = strMobile.Substring(1, strMobile.Length - 1);
                }
                Regex regex = new Regex("^1[0-9]{10}$");
                bool bCheck = regex.IsMatch(strMobile);
                if (true == bCheck)
                {// 是手机号码
                 // 获取前7位
                    string sevenChar = strMobile.Substring(0, 7);
                    var cityName = "四川省自贡市";
                    if (_appOptions.Value.IsZiGong)
                        cityName = "四川省自贡市";
                    else if (_appOptions.Value.IsYiBin)
                        cityName = "四川省宜宾市";
                    else if (_appOptions.Value.IsLuZhou)
                        cityName = "四川省泸州市";
                    var data = await _systemMobilAreaRepository.GetAsync(p => p.MobileAreaName == cityName && p.MobileCode == sevenChar);
                    if (data is null)
                    {
                        return strMobile = "0" + strMobile;
                    }
                }
            }
            // 返回结果
            return Mobile;
        }

        #region 验证手机号码
        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="strPhoneNum">电话号码</param>
        /// <returns></returns>
        public static bool e_VerificationMobile(string strPhoneNum)
        {
            // 手机号码正则表达式
            string strVer = @"^(0)?(13\d|15\d|18\d|17\d|14\d|19\d|16\d)\d{5}(\d{3}|\*{3})$";
            Regex regex = new Regex(strVer);
            if (true == regex.IsMatch(strPhoneNum))
                return true;
            else
                return false;
        }
        #endregion

        #region 验证座机号码
        /// <summary>
        /// 验证座机号码
        /// </summary>
        /// <param name="strPhoneNum">电话号码</param>
        /// <returns></returns>
        public static bool e_VerificationTel(string strPhoneNum)
        {
            // 座机号码正则表达式
            string strVer = @"^((0\d{2,3})(-)?)?(\d{7,8})(-(\d{3,}))?$";
            Regex regex = new Regex(strVer);
            if (true == regex.IsMatch(strPhoneNum))
                return true;
            else
                return false;
        }
        #endregion
    }
}
