using Hotline.Settings;
using Hotline.Share.Dtos.Settings;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Repository;

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

        public SystemMobilAreaApplication(IRepository<SystemMobilArea> systemMobilAreaRepository)
        {
            _systemMobilAreaRepository = systemMobilAreaRepository;
        }

        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;
        }


        #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
    }
}
