using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.CallCenter.Calls;
using SqlSugar;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace Hotline.Application.Bigscreen
{
	public  class SeatStateDataService: ISeatStateDataService , IScopeDependency
	{

		public static DateTime now = DateTime.Now.Date;
		private readonly IRepository<TrCallRecord> _callRepository;
		public SeatStateDataService(IRepository<TrCallRecord> callRepository) {
			_callRepository= callRepository;
		}

		public async Task<object> GetCall24(CancellationToken stoppingToken) {
			List<string> timeList = new List<string>();
			for (int i = 0; i < 24; i++)
			{
				var time = i < 10 ? $"0{i}" : i.ToString();
				timeList.Add(time);
			}
			var call24 = await _callRepository.Queryable()
				.Where(x => x.CreatedTime.Date == now
					&& x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
				)
				.Select(x => new { time = x.CreatedTime.ToString("hh"), x.CallDirection }).MergeTable()
				.GroupBy(x => x.time)
				.Select(x => new
				{
					Time = x.time.ToString(),
					In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
					Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
				}).MergeTable().ToListAsync(stoppingToken);

			var call24List = (from t1 in timeList
				join t2 in call24 on t1 equals t2.Time into t1_t2
				from item in t1_t2.DefaultIfEmpty()
				select new { 
					Time = t1 + ":00",
					In = t1_t2.Select(x => x.In).FirstOrDefault() ,
					Out = t1_t2.Select(x => x.Out).FirstOrDefault() 
				}).ToList();
			return call24List;
		}

		public async Task<object> GetCallTop10(CancellationToken stoppingToken)
		{

			var callTop10 = await _callRepository.Queryable()
				.Where(x => x.CreatedTime.Date == DateTime.Now.Date
					&& x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
				)
				.GroupBy(x => x.UserName)
				.Select(x => new
				{
					UserName = x.UserName,
					In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0))
				}).MergeTable().OrderByDescending(x => x.In).Take(10).ToListAsync(stoppingToken);
			return callTop10;
		}

		public async Task<object> GetCallList(CancellationToken stoppingToken)
		{
			var callList = await _callRepository.Queryable()
				.Where(x => x.CreatedTime.Date == now
					&& x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
				)
				.Select(x => new
				{
					InOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.On, 1, 0)),
					ValidOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.On && x.Duration > 5, 1, 0)),
					InNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.NoOn, 1, 0)),
					OutOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out && x.OnState == Share.Enums.CallCenter.EOnState.On, 1, 0)),
					InQueueNoOn = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In && x.OnState == Share.Enums.CallCenter.EOnState.NoOn && x.QueueTims > 0, 1, 0)),
				}).MergeTable().ToListAsync(stoppingToken);
			return callList;
		}

		public async Task<object> GetCallAverage(CancellationToken stoppingToken)
		{
			List<string> timeList = new List<string>();
			for (int i = 0; i < 24; i++)
			{
				var time = i < 10 ? $"0{i}" : i.ToString();
				timeList.Add(time);
			}
			var callAverage = await _callRepository.Queryable()
				.Where(x => x.CreatedTime.Date == now
					&& x.CallOrderType == Share.Enums.CallCenter.ECallOrderType.Order
				)
				.Select(x => new { time = x.CreatedTime.ToString("hh"), x.CallDirection }).MergeTable()
				.GroupBy(x => x.time)
				.Select(x => new
				{
					Time = x.time.ToString(),
					In = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)),
					InAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.In, 1, 0)) / 60,
					Out = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)),
					OutAverag = SqlFunc.AggregateSum(SqlFunc.IIF(x.CallDirection == Share.Enums.CallCenter.ECallDirection.Out, 1, 0)) / 60,
				}).MergeTable().ToListAsync(stoppingToken);
			var callAverageList = (from t1 in timeList
				join t2 in callAverage on t1 equals t2.Time into t1_t2
				from item in t1_t2.DefaultIfEmpty()
				select new {
					Time = t1 + ":00",
					In = t1_t2.Select(x => x.In).FirstOrDefault(),
					InAverag = t1_t2.Select(x => x.InAverag).FirstOrDefault(),
					Out = t1_t2.Select(x => x.Out).FirstOrDefault(),
					OutAverag = t1_t2.Select(x => x.OutAverag).FirstOrDefault() 
				}).ToList();
			return callAverageList;
		}


	}
}
