using System;
using System.Collections.Generic;
using System.Common;
using System.IRepositories;
using System.Linq;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace TVShoppingCallCenter_ZLJ.Controllers.CallCenter
{
    /// <summary>
    /// 呼入控制器
    /// </summary>
    [Authorize]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class CallInOptController : BaseController
    {
        private readonly IConfiguration config;
        private readonly ISys_MobileDataRepository busMobileDataRepository;
      public CallInOptController( IConfiguration _configuration, ISys_MobileDataRepository _busMobileDataRepository)
        {
            config = _configuration;
            busMobileDataRepository = _busMobileDataRepository;
        }

        /// <summary>
        /// 外呼判断本地外地以及加前缀返回
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost("GetCallOutprefix")]
        public async Task<IActionResult> GetCallOutprefix(string phone)
        {
            string phone1 = StringHelper.ToDBC(StringHelper.RemoveNotNumber(StringHelper.NoHtml(phone)));
            string tophone = phone1;
            string fix = ""; string bfix = ""; string wfix = "";
            string zipcode = config["appSettings:CallOutZipCode"];
            bfix = config["appSettings:CallOutBPre"];
            wfix = config["appSettings:CallOutWPre"];
            int zip =await GetZipCodeByPhone(phone1, zipcode);
            if (zip == 1)
            {
                fix = bfix;
            }
            else if (zip == 2)
            {
                fix = wfix;
            }
            else
            {
                tophone = phone1.TrimStart('0');
                if (zip == 3)
                {//本地固话去0加9 比如：988888517，937188888517
                    fix = bfix;
                }
                else if (zip == 4)
                {//外地固话前加9 比如：9037188888517
                    fix = wfix;
                }
            }

            var obj = new
            {
                phone = fix + tophone,
                fix = fix
            };

            return Success("外呼号码加前缀", obj);

        }

        /// <summary>
        /// 根据号码和区号判断号码是否为归属地号码
        /// 返回0为分机号或特殊号码
        /// 返回1为本地号码
        /// 返回2为外地号码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="zipcode"></param>
        /// <returns></returns>
        [NonAction]
        public async Task<int> GetZipCodeByPhone(string phone, string zipcode)
        {
            int res = 0;
            if (phone.Trim().Length >= 7)
            {
                //7位及7位以上是固定电话或手机
                //判断是否手机
                if (phone.Trim().Length == 11 && phone[0] == '1')
                {//号码为11位，首位是1，为手机号
                    string p7 = phone.Substring(0, 7);
                    T_Sys_MobileData mobileModel = await busMobileDataRepository.GetFirst(q=>q.F_MobileNum== p7); 
                    if (mobileModel != null)
                    {
                        if (mobileModel.F_ZipCode.Equals(zipcode))
                        {
                            res = 1;
                        }
                        else
                        {
                            res = 2;
                        }
                    }
                }
                else
                {
                    if (phone.Trim().Length == 11 && phone.Substring(0, 3).Equals(zipcode))
                    {//号码为11位
                        //截取前三位区号判断是否本地
                        bool resbd3 = phone.Substring(0, 3).Equals(zipcode);
                        //截取前四位区号判断是否本地
                        bool resbd4 = phone.Substring(0, 4).Equals(zipcode);
                        if (resbd3 || resbd4)
                        {
                            res = 3;
                        }
                        else
                        {
                            res = 4;
                        }
                    }
                    else if (phone.Trim().Length < 11)
                    {//号码小于11位，为本地
                        res = 1;
                    }
                    else if (phone.Trim().Length < 11)
                    {//号码小于11位，为本地
                        res = 3;
                    }
                    else if (phone.Trim().Length > 11 && phone.Substring(0, 4).Equals(zipcode))
                    {//号码大于11位，截取前四位区号判断是否本地
                        res = 3;
                    }
                    else
                    {
                        res = 4;
                    }
                }
            }
            return res;
        }

    }
}