using MapsterMapper;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Repository;
using Hotline.CallCenter.Tels;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Caching.Interfaces;
using Hotline.Users;

namespace Hotline.Application.Xthx
{
    /// <summary>
    /// 兴唐恒信
    /// </summary>
    public class XthxApplication : IXthxApplication, IScopeDependency
    {

        #region 注册

        private readonly IRepository<TelOperationXthx> _telOperationXthxRepository;            //坐席动作类型
        private readonly IRepository<TelOperationXthxLog> _telOperationXthxLogRepository;      //坐席动作类型
        private readonly IRepository<User> _userRepository;                                    //用户
        private readonly ISystemSettingCacheManager _systemSettingCacheManager;                //系统参数
        private readonly ISessionContext _sessionContext;
        private readonly IMapper _mapper;

        public XthxApplication(
            IRepository<TelOperationXthx> telOperationXthxRepository,
            IRepository<TelOperationXthxLog> telOperationXthxLogRepository,
            IRepository<User> userRepository,
            ISystemSettingCacheManager systemSettingCacheManager,
            ISessionContext sessionContext,
            IMapper mapper)
        {
            _telOperationXthxRepository = telOperationXthxRepository;
            _telOperationXthxLogRepository = telOperationXthxLogRepository;
            _userRepository = userRepository;
            _systemSettingCacheManager = systemSettingCacheManager;
            _sessionContext = sessionContext;
            _mapper = mapper;
        }

        #endregion

        #region 分机状态日志新增

        /// <summary>
        /// 分机状态日志新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        public async Task<string> AddOperationLogAsync(TelOperationXthxDto dto, CancellationToken cancellationToken)
        {
            var log = _mapper.Map<TelOperationXthxLog>(dto);
            log.Json = System.Text.Json.JsonSerializer.Serialize(dto);
            return await _telOperationXthxLogRepository.AddAsync(log, cancellationToken);
        }

        #endregion

        #region 分机状态变更新增

        /// <summary>
        /// 分机状态变更新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="Id">日志Id</param>
        /// <param name="cancellationToken"></param>
        public async Task<string> AddOperationAsync(TelOperationXthxDto dto, string Id, CancellationToken cancellationToken)
        {
            var newId = string.Empty;
            var logTemp = await _telOperationXthxLogRepository.GetAsync(Id);
            if (logTemp != null)
            {
                TelOperationXthx xthxAttr = new TelOperationXthx();
                _mapper.Map(logTemp, xthxAttr);

                var user = await _userRepository.GetAsync(x => x.StaffNo == dto.StaffNo && x.IsDeleted == false);
                if (user != null)
                {
                    xthxAttr.UserId = user.Id;
                    xthxAttr.UserName = user.Name;
                }
                //xthxAttr.TelId = "";
                //xthxAttr.UserGroup = "";

                xthxAttr.StartTime = logTemp.CreationTime;
                xthxAttr.OperationStatus = dto.Status;
                newId = await _telOperationXthxRepository.AddAsync(xthxAttr, cancellationToken);
            }
            return newId;
        }

        #endregion

        #region 分机状态变更修改

        /// <summary>
        /// 分机状态变更修改
        /// </summary>
        /// <param name="Id">日志Id</param>
        /// <param name="UId">更新Id</param>
        /// <param name="cancellationToken"></param>
        public async Task UpdateOperationAsync(string Id, string UId, CancellationToken cancellationToken)
        {
            var data = await _telOperationXthxRepository.GetAsync(UId);
            if (data != null)
            {
                var logTemp = await _telOperationXthxLogRepository.GetAsync(Id);
                if (logTemp != null)
                {
                    data.EndTime = logTemp.CreationTime;
                    data.RestDuration = (data.EndTime.Value - data.StartTime.Value).TotalSeconds;
                    await _telOperationXthxRepository.UpdateAsync(data);
                }
            }
        }

        #endregion

    }
}
