using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Calls;
using Hotline.CallCenter.Tels;
using Hotline.Share.Dtos.TrCallCenter;
using Hotline.Share.Enums.CallCenter;
using Hotline.Users;
using Microsoft.AspNetCore.Http;
using Tr.Sdk;
using Tr.Sdk.Tels;
using XF.Domain.Cache;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Application.Tels;

public class TelApplication : ITelApplication, IScopeDependency
{
    private readonly IUserCacheManager _userCacheManager;
    private readonly ITelRestRepository _telRestRepository;
    private readonly ITypedCache<Work> _cacheWork;
    private readonly IWorkRepository _workRepository;
    private readonly IRepository<TelActionRecord> _telActionRecordRepository;

    public TelApplication(
        IUserCacheManager userCacheManager,
        ITelRestRepository telRestRepository,
        ITypedCache<Work> cacheWork,
        IWorkRepository workRepository,
        IRepository<TelActionRecord> telActionRecordRepository
        )
    {
        _userCacheManager = userCacheManager;
        _telRestRepository = telRestRepository;
        _cacheWork = cacheWork;
        _workRepository = workRepository;
        _telActionRecordRepository = telActionRecordRepository;
    }

    /// <summary>
    /// 用户签出分机
    /// </summary>
    public async Task SignOutAsync(string userId, CancellationToken cancellationToken)
    {
        var work = _userCacheManager.GetWorkByUser(userId);
        if (work is null) return;

        var telRest = await _telRestRepository.GetAsync(x => x.TelNo == work.TelNo && !x.EndTime.HasValue, cancellationToken);
        if (telRest is not null)
        {
            telRest.EndRest();
            await _telRestRepository.UpdateAsync(telRest, cancellationToken);
        }

        work.OffDuty();
        await _workRepository.UpdateAsync(work, cancellationToken);
        _cacheWork.Remove(work.GetKey(KeyMode.UserId));
        _cacheWork.Remove(work.GetKey(KeyMode.TelNo));

        var list = await _telActionRecordRepository.Queryable().Where(x => x.TelNo == work.TelNo && !x.EndTime.HasValue).ToListAsync();
        foreach (var item in list)
        {
            item.EndAction();
            await _telActionRecordRepository.UpdateAsync(item);
        }

        //var telAction = await _telActionRecordRepository.GetAsync(x => x.TelNo == work.TelNo && x.ActionType == EActionType.TelRest && !x.EndTime.HasValue, cancellationToken);
        //if (telAction != null)
        //{
        //    telAction.EndAction();
        //    await _telActionRecordRepository.UpdateAsync(telAction);
        //}

        //var telarrangeAction = await _telActionRecordRepository.GetAsync(x => x.TelNo == work.TelNo && x.ActionType == EActionType.CallEndArrange && !x.EndTime.HasValue, cancellationToken);
        //if (telarrangeAction != null)
        //{
        //    telarrangeAction.EndAction();
        //    await _telActionRecordRepository.UpdateAsync(telarrangeAction);
        //}
    }

    public async Task SignOutByTelNoAsync(string telNo, CancellationToken cancellationToken)
    {
        var work = _userCacheManager.GetWorkByTelNoExp(telNo);
        if (work is null) return;

        var telRest = await _telRestRepository.GetAsync(x => x.TelNo == work.TelNo && !x.EndTime.HasValue, cancellationToken);
        if (telRest is not null)
        {
            telRest.EndRest();
            await _telRestRepository.UpdateAsync(telRest, cancellationToken);
        }

        work.OffDuty();
        await _workRepository.UpdateAsync(work, cancellationToken);
        _cacheWork.Remove(work.GetKey(KeyMode.UserId));
        _cacheWork.Remove(work.GetKey(KeyMode.TelNo));



        var list = await _telActionRecordRepository.Queryable().Where(x => x.TelNo == work.TelNo && !x.EndTime.HasValue).ToListAsync();
        foreach (var item in list)
        {
            item.EndAction();
            await _telActionRecordRepository.UpdateAsync(item);
        }

        //var telAction = await _telActionRecordRepository.GetAsync(x => x.TelNo == work.TelNo && x.ActionType == EActionType.TelRest && !x.EndTime.HasValue, cancellationToken);
        //if (telAction != null)
        //{
        //    telAction.EndAction();
        //    await _telActionRecordRepository.UpdateAsync(telAction);
        //}

        //var telarrangeAction = await _telActionRecordRepository.GetAsync(x => x.TelNo == work.TelNo && x.ActionType == EActionType.CallEndArrange && !x.EndTime.HasValue, cancellationToken);
        //if (telarrangeAction != null)
        //{
        //    telarrangeAction.EndAction();
        //    await _telActionRecordRepository.UpdateAsync(telarrangeAction);
        //}
    }
  }