using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using CallCenterApi.IRepositories;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SignTokenApi.IRepositories;

namespace CallCenterApi.Controllers.trafficmanagement
{
    /// <summary>
    /// 外呼选择
    /// </summary>
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class CallOutOptController : BaseController
    {
        private readonly ILogger<CallOutOptController> _logger;
        private readonly ICall_MobileDataRepository _call_mobiledataRepository;
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        private readonly ISys_SeatGroupRepository _sys_seatgroupRepository;
        

        public CallOutOptController(ILogger<CallOutOptController> logger, ICall_MobileDataRepository call_mobiledataRepository, ISys_User_AccountRepository sys_user_accountRepository, ISys_SeatGroupRepository sys_seatgroupRepository)
        {
            _logger = logger;
            _call_mobiledataRepository = call_mobiledataRepository;
            _sys_user_accountRepository = sys_user_accountRepository;
            _sys_seatgroupRepository = sys_seatgroupRepository;
        }

        public IActionResult Index()
        {
            return View();
        }


        //外呼判断本地外地以及加前缀返回
        /// 根据号码和区号判断号码是否为归属地号码
        /// 返回0为分机号或特殊号码
        /// 返回1为手机本地号码
        /// 返回2为手机外地号码
        /// 返回3为固定电话本地号码
        /// 返回4为固定电话外地号码
        [Authorize]
        [HttpGet("getcalloutprefix")]
        public async Task<IActionResult> GetCallOutprefix(string phone)
        {
            #region 2018-7-31 方法问题暂不处理
            //string phone1 = RequestString.ToDBC(RequestString.RemoveNotNumber(WebHelper.NoHtml(phone)));
            //phone = "18999999999";
            #endregion
            int i;
            bool isnum = int.TryParse(phone.Substring(0,1), out i);//判断是否为数字
            if (!isnum)
            {
                return Error("外呼号码加前缀错误");
            }
            string phone1 = phone;
            string tophone = phone1;
            string zipcode = ""; string bfix = ""; string wfix = ""; string fix = "";

            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            
            var list =await _sys_user_accountRepository.Get(x => x.usercode == nowusercode);
            foreach (var item in list)
            {
                string userseatgroupid = item.group;
                if (userseatgroupid != "" && userseatgroupid != "0")
                {
                    var smodel = await _sys_seatgroupRepository.Get(x => x.id == userseatgroupid);
                    if (smodel != null)
                    {
                        foreach (var items in smodel)
                        {
                            zipcode = items.zxatt.Trim();
                            bfix = items.whbdkey.Trim();
                            wfix = items.whwdkey.Trim();
                        }
                            
                    }
                    int zip = 0;
                    #region
                    if (phone.Trim().Length >= 7)
                    {
                        //7位及7位以上是固定电话或手机
                        //判断是否手机
                        if (phone.Trim().Length == 11 && phone[0] == '1')
                        {//号码为11位，首位是1，为手机号
                         //BLL.T_Sys_MobileData mobile_Bll = new BLL.T_Sys_MobileData();
                         //Model.T_Sys_MobileData mobileModel = mobile_Bll.GetModelList("F_MobileNum='" + phone.Substring(0, 7) + "'").FirstOrDefault();
                         //_call_mobiledataRepository
                            var mobileModel =await _call_mobiledataRepository.Get(x => x.mobilenum == phone.Substring(0, 7));
                            if (mobileModel != null)
                            {
                                foreach (var items in mobileModel)
                                {
                                    if (items.zipcode.Equals(zipcode))
                                    {
                                        zip = 1;
                                    }
                                    else
                                    {
                                        zip = 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)
                                {
                                    zip = 3;
                                }
                                else
                                {
                                    zip = 4;
                                }
                            }
                            else if (phone.Trim().Length < 11)
                            {//号码小于11位，为本地
                                zip = 3;
                            }
                            else if (phone.Trim().Length > 11 && phone.Substring(0, 4).Equals(zipcode))
                            {//号码大于11位，截取前四位区号判断是否本地
                                zip = 3;
                            }
                            else
                            {
                                zip = 4;
                            }
                        }
                    }
                    #endregion
                    #region 
                    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;
                        }
                    }
                    #endregion
                    var obj = new
                    {
                        phone = fix + tophone,
                        fix = fix
                    };

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

                return Error("外呼号码加前缀错误");
            
            
        }

        
    }
}