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

namespace CallCenterApi.Controllers
{
    /// <summary>
    /// 首页话务数据展示
    /// </summary>
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class HomePageController : BaseController
    {
        private readonly ILogger<HomePageController> _logger;
        private readonly ICall_RecordsRepository _call_recordsRepository;
        //private readonly ISys_User_AccountRepository _sys_user_accountRepository;
        public HomePageController(ILogger<HomePageController> logger, ICall_RecordsRepository call_recordsRepository)//, ISys_User_AccountRepository sys_user_accountRepository)
        {
            _call_recordsRepository = call_recordsRepository;
            _logger = logger;
            //_sys_user_accountRepository = sys_user_accountRepository;
        }

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

        //获取数据
        [Authorize]
        [HttpGet("gethwtj")]
        public async Task<IActionResult> GetHWTJ()
        {
            string stime = string.Empty; string etime = string.Empty;
            #region 今日数据
            GetStartEndTime(1, out stime, out etime);           
            var list = new List<FilterDefinition<mw_call_records>>();          
            if (!string.IsNullOrWhiteSpace(stime))
            {
                list.Add(Builders<mw_call_records>.Filter.Gt("begintime", DateTime.Parse(stime.Trim())));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
               list.Add(Builders<mw_call_records>.Filter.Lt("begintime", DateTime.Parse(etime.Trim())));
            }
            var jrldflist = list.ToList();
            var jrhfflist = list.ToList();
            jrldflist.Add(Builders<mw_call_records>.Filter.Eq("calltype", 0));
            jrhfflist.Add(Builders<mw_call_records>.Filter.Eq("calltype", 1));
            var jrldfilter = Builders<mw_call_records>.Filter.And(jrldflist);
            var jrhffilter = Builders<mw_call_records>.Filter.And(jrhfflist);
            var jrldcount = await _call_recordsRepository.CountAsync(jrldfilter);
            var jrhfcount = await _call_recordsRepository.CountAsync(jrhffilter);
            #endregion

            #region 本月数据
            stime = string.Empty;  etime = string.Empty;
            GetStartEndTime(3, out stime, out etime);
            var listm = new List<FilterDefinition<mw_call_records>>();
            if (!string.IsNullOrWhiteSpace(stime))
            {
                listm.Add(Builders<mw_call_records>.Filter.Gt("begintime", DateTime.Parse(stime.Trim())));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                listm.Add(Builders<mw_call_records>.Filter.Lt("begintime", DateTime.Parse(etime.Trim())));
            }
            var jrldflistm = listm.ToList();
            var jrhfflistm = listm.ToList();
            var wjtlistm = listm.ToList();
            jrldflistm.Add(Builders<mw_call_records>.Filter.Eq("calltype", 0));
            jrhfflistm.Add(Builders<mw_call_records>.Filter.Eq("calltype", 1));
            wjtlistm.Add(Builders<mw_call_records>.Filter.Eq("callstate", 0));
            var jrldfilterm = Builders<mw_call_records>.Filter.And(jrldflistm);
            var jrhffilterm = Builders<mw_call_records>.Filter.And(jrhfflistm);
            var wjtfilterm = Builders<mw_call_records>.Filter.And(wjtlistm);
            var byldcount = await _call_recordsRepository.CountAsync(jrldfilterm);
            var byhfcount = await _call_recordsRepository.CountAsync(jrhffilterm);
            var wjcount = await _call_recordsRepository.CountAsync(wjtfilterm);
            #endregion
            var obj = new
            {
                jrldcount,
                jrhfcount,
                byldcount,
                byhfcount,
                wjcount 
            };

            return Success("获取成功", obj);
        }

        //获取数据
        [Authorize]
        [HttpGet("gethwstatecount")]
        public async Task<IActionResult> GetHWStateCount(int datetype = 0)
        {
            //string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string stime = string.Empty; string etime = string.Empty;
            if (datetype != 0)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }
            var list = new List<FilterDefinition<mw_call_records>>();
            
            var wjtflist = list.ToList();
            var yjtflist = list.ToList();

            if (!string.IsNullOrWhiteSpace(stime))
            {
                wjtflist.Add(Builders<mw_call_records>.Filter.Gt("begintime", DateTime.Parse(stime.Trim())));
                yjtflist.Add(Builders<mw_call_records>.Filter.Gt("begintime", DateTime.Parse(stime.Trim())));
            }
            if (!string.IsNullOrWhiteSpace(etime))
            {
                wjtflist.Add(Builders<mw_call_records>.Filter.Lt("begintime", DateTime.Parse(etime.Trim())));
                yjtflist.Add(Builders<mw_call_records>.Filter.Lt("begintime", DateTime.Parse(etime.Trim())));
            }

            wjtflist.Add(Builders<mw_call_records>.Filter.Eq("callstate", 0));
            var wjtfilter = Builders<mw_call_records>.Filter.And(wjtflist);

            yjtflist.Add(Builders<mw_call_records>.Filter.Eq("callstate", 1));
            var yjtfilter = Builders<mw_call_records>.Filter.And(yjtflist);

            var wjtcount = await _call_recordsRepository.CountAsync(wjtfilter);
            var yjtcount = await _call_recordsRepository.CountAsync(yjtfilter);

            var obj = new
            {
                wjtcount,
                yjtcount
            };

            return Success("获取成功", obj);
        }

        /// <summary>
        /// 获取日周月年话务24小时统计
        /// </summary>
        [HttpGet("gethw24count")]
        public IActionResult GetHW24CountReport(int datetype = 0)
        {
            //string optuser = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
            string stime = string.Empty; string etime = string.Empty;
            if (datetype != 0)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            var result = _call_recordsRepository.GetHW24CountReport(stime, etime);
            return Success("获取成功", result);
        }

        /// <summary>
        /// 获取排行榜
        /// </summary>
        [HttpGet("gethwrank")]
        public IActionResult GetHWRank(int top = 0, int datetype = 0)
        {
            string stime = string.Empty; string etime = string.Empty;
            if (datetype != 0)
            {
                GetStartEndTime(datetype, out stime, out etime);
            }

            var result = _call_recordsRepository.GetHWRank(top, stime, etime);

            return Success("获取成功", result);
        }

        /// <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 1://当天
                    break;
                case 2://本周
                    int n = (int)datenow.DayOfWeek == 0 ? 7 : (int)datenow.DayOfWeek;
                    datestart = datenow.AddDays(1 - n);//本周周一
                    dateend = datestart.AddDays(6);//本周周日
                    break;
                case 3://本月
                    datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                    dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 4://本年
                    datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                    dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                    break;
                default:
                    break;
            }

            start = datestart.ToString("yyyy-MM-dd") + " 00:00:00";
            end = dateend.ToString("yyyy-MM-dd") + " 23:59:59";
        }
    }
}