using Hotline.CallCenter.Calls;
using Hotline.Share.Dtos.TrCallCenter;
using Hotline.Share.Enums.CallCenter;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Application.Mappers
{
    public class CallMapperConfigs : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<ReceiveCallRecordDto, TrCallRecord>()
                .Map(d => d.User, x => x.user)
                .Map(d => d.RecordingFileName, x => x.recording_file_name)
                .Map(d => d.CallDirection, x => x.call_direction == "in" ? ECallDirection.In : ECallDirection.Out)
                .Map(d => d.EndBy, x => x.hangup_side == "caller" ? EEndBy.From : (x.hangup_side == "callee" ? EEndBy.To : EEndBy.None))
                .Map(d => d.AgentTransferNumber, x => x.agent_transfer_number)
                .Map(d => d.CallAccept, x => x.call_accept)
                .Map(d => d.CPN, x => x.caller_id_number)
                .Map(d => d.CPNName, x => x.caller_id_name)
                .Map(d => d.CDPN, x => x.destination_number)
                .Map(d => d.RecordingFileUrl, x => x.recording_file_url)
                .Map(d => d.Gateway, x => x.gateway)
                .Map(d => d.OtherStr, x => x.other_str)
                .Map(d => d.OtherAccept, x => x.other_accept)
                .Map(d => d.Status, x => x.status)
                .Map(d => d.OlaQueue, x => x.ola_queue)
                .Map(d => d.BatchAccept, x => x.batch_accept)
                .Map(d => d.IvrDtmf, x => x.ivr_dtmf)
                .Map(d => d.DtmfType, x => x.dtmf_type)
                .AfterMapping((s, d) => { d.CreatedTime = DateTime.Parse(s.created_time); })
                .AfterMapping((s, d) => { d.AnsweredTime = FormatDateTime(s.answered_time); })
                .AfterMapping((s, d) => { d.OverTime = DateTime.Parse(s.over_time); })
                .AfterMapping((s, d) => { d.BeginIvrTime = FormatDateTime(s.beginIvrTime); })
                .AfterMapping((s, d) => { d.EndIvrTime = FormatDateTime(s.endIvrTime); })
                .AfterMapping((s, d) => { d.BeginQueueTime = FormatDateTime(s.beginQueueTime); })
                .AfterMapping((s, d) => { d.EndQueueTime = FormatDateTime(s.endQueueTime); })
                .AfterMapping((s, d) => { d.BeginRingTime = FormatDateTime(s.beginRingTime); })
                .AfterMapping((s, d) => { d.EndRingTimg = FormatDateTime(s.endRingTime); })
                .AfterMapping((s, d) =>
                {
                    if (string.IsNullOrEmpty(s.recording_file_url)) return;
                    var uri = new Uri(s.recording_file_url);
                    d.RecordingBaseAddress = $"{uri.Scheme}://{uri.Host}:{uri.Port}";
                    d.RecordingAbsolutePath = uri.AbsolutePath;
                })
                ;



            config.ForType<ReceiveCallEvaluateDto, TrCallEvaluate>()
                .Map(d => d.Dtmf, x => x.dtmf)
                .Map(d => d.CallAccept, x => x.call_accept)
                .Map(d => d.Type, x => x.type);
        }

        private DateTime? FormatDateTime(string? time)
        {
            if (string.IsNullOrEmpty(time)) return null;

            try
            {
                return DateTime.Parse(time);
            }
            catch
            {
                return null;
            }
        }

    }
}
