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

namespace WechatApi.Controllers.traffic
{
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class AnalysisController : BaseController
    {
        private readonly ILogger<AnalysisController> _logger;
        private readonly ICall_RecordsRepository _call_recordsRepository;
        private readonly ISys_User_AccountRepository _sys_user_accountRepository;

        public AnalysisController(ILogger<AnalysisController> logger, ICall_RecordsRepository call_recordsRepository, ISys_User_AccountRepository sys_user_accountRepository)
        {
            _sys_user_accountRepository = sys_user_accountRepository;
            _call_recordsRepository = call_recordsRepository;
            _logger = logger;
        }
        [HttpGet("view")]
        public IActionResult view()
        {
            return Success("成功");
        }
        /// <summary>
        /// 读取话务统计
        /// </summary>
        /// <param name="datetype">0当天、1本周、2本月、3本年、4累计时间</param>
        /// <param name="isself"></param>
        /// <returns></returns>
        [HttpGet("getstatistics")]
        public IActionResult GetStatisticsAsync(int datetype,string usercode)
        {
            string nowusercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string starttime = string.Empty;
            string etime = string.Empty;
            GetStartEndTime(datetype, out starttime, out etime);
            string agentcode = "";
            if (!string.IsNullOrWhiteSpace(usercode))
                agentcode = usercode;
            else
                agentcode = nowusercode;

            #region  呼入呼出情况
            string ccount = GetHighChartData(starttime, etime, "", "", agentcode);//总次数
            string cjcount = GetHighChartData(starttime, etime, "", "1", agentcode);//总接通次数
            string cicount = GetHighChartData(starttime, etime, "0", "", agentcode);//呼入次数
            string cijcount = GetHighChartData(starttime, etime, "0", "1", agentcode);//呼入接通次数
            string cocount = GetHighChartData(starttime, etime, "1", "", agentcode);//呼出次数
            string cojcount = GetHighChartData(starttime, etime, "1", "1", agentcode);//呼出接通次数
            string clongs = GetCallRecordTotalTime(agentcode,starttime, etime, "", "1");//总通话时长
            string cilongs = GetCallRecordTotalTime(agentcode, starttime, etime, "0", "1");//呼入通话时长
            string colongs = GetCallRecordTotalTime(agentcode, starttime, etime, "1", "1");//呼出通话时长
            #endregion
            var obj24 = Get24HourCall(starttime, etime, agentcode);//24小时话务情况
            //var agentstate = GetAgentState(starttime, etime, agentcode);//综合数据分析
            var obj = new
            {
                call = new
                {//总话务
                    count = ccount,
                    jtcount = cjcount,
                    talklongs = clongs,
                },
                callin = new
                {//呼入
                    count = cicount,
                    jtcount = cijcount,
                    talklongs = cilongs,
                },
                callout = new
                {//呼出
                    count = cocount,
                    jtcount = cojcount,
                    talklongs = colongs,
                },
                hour24 = obj24,
               // allstate = agentstate,
            };
            return Success("获取话务统计", obj);
        }
        /// <summary>
        /// 获取当天、本周、本月、本年、累计时间
        /// </summary>
        /// <param name="datetype"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void GetStartEndTime(int datetype, out string start, out string end)
        {
            start = string.Empty;
            end = string.Empty;

            var datenow = DateTime.Now;
            var datestart = datenow;
            var dateend = datenow;
            switch (datetype)
            {
                case 0://当天
                    break;
                case 1://本周
                    int n = (int)datenow.DayOfWeek == 0 ? 7 : (int)datenow.DayOfWeek;
                    datestart = datenow.AddDays(1 - n);//本周周一
                    dateend = datestart.AddDays(6);//本周周日
                    break;
                case 2://本月

                    datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                    dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 3://本年
                    datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                    dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                    break;
                case 4://全部
                    break;
                default:
                    break;
            }
            if (datetype != 4)
            {
                start = datestart.ToString("yyyy-MM-dd") + " 00:00:00";
                end = dateend.ToString("yyyy-MM-dd") + " 23:59:59";
            }
        }
        #region 获取通话量和时长
        /// <summary>
        /// 获取通话时长
        /// </summary>
        /// <param name="agentcode">坐席工号</param>
        /// <param name="BeginTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="tag">呼叫类型</param>
        /// <param name="state">呼叫状态</param>
        private string GetCallRecordTotalTime(string agentcode, string BeginTime, string EndTime, string tag, string state)
        {
            var x = _call_recordsRepository.GetCallRecordTotalTime(agentcode, tag, BeginTime, EndTime, state).ToString();
            int i = x.IndexOf(",") + 1;
            return x.Substring(i, x.Length - i);
        }
        /// <summary>
        /// 获取通话量
        /// </summary>
        /// <param name="agentcode">坐席工号</param>
        /// <param name="BeginTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="tag">呼叫类型</param>
        /// <param name="state">呼叫状态</param>
        private string GetHighChartData(string BeginTime, string EndTime, string tag, string state, string agentcode)
        {
            var x = _call_recordsRepository.GetHighChartData(agentcode, BeginTime, EndTime, tag, state).ToString();

            return x;
        }
        /// <summary>
        /// 获取24小时通话情况
        /// </summary>
        /// <param name="agentcode">坐席工号</param>
        /// <param name="BeginTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="tag">呼叫类型</param>
        /// <param name="state">呼叫状态</param>
        private object Get24HourCall(string BeginTime, string EndTime, string agentcode)
        {
            var x = _call_recordsRepository.GetHW24CountReport(agentcode, BeginTime, EndTime);
            
            return x;
        }
        private object GetAgentState(string BeginTime, string EndTime, string agentcode)
        {
            var x = _call_recordsRepository.GetAgentState(agentcode, BeginTime, EndTime);
            return x;
        }
        #endregion
    }
}