using Hotline.BatchTask;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using XF.Domain.Dependency;
using Hotline.CallCenter.Configs;
using Hotline.Share.Dtos.Order.OrderVisit;

namespace Hotline.Application.OrderApp.OrderVisitApp
{
    public class VoiceVisitTaskExecutor : IApptaskExecutor<VoiceVisitWithSessionRequest>, IScopeDependency
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IOptionsSnapshot<CallCenterConfiguration> _callcenterOptions;

        public VoiceVisitTaskExecutor(
            IHttpClientFactory httpClientFactory,
            IOptionsSnapshot<CallCenterConfiguration> callcenterOptions)
        {
            _httpClientFactory = httpClientFactory;
            _callcenterOptions = callcenterOptions;
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellation"></param>
        /// <returns>是否成功执行</returns>
        public async Task<ApptaskExecuteResult> ExecuteAsync(VoiceVisitWithSessionRequest? request, CancellationToken cancellation)
        {
            Console.WriteLine($"执行vv: {DateTime.Now}");
            if (request == null)
                return ApptaskExecuteResult.Fail("请求参数为空");

            var baseAddress = _callcenterOptions.Value.XingTang.Address;
            if (string.IsNullOrEmpty(baseAddress))
                return ApptaskExecuteResult.Fail("未配置请求地址");

            var client = _httpClientFactory.CreateClient();
            client.BaseAddress = new Uri(baseAddress);
            var url = $"{baseAddress}/groupcall?content=&called1={request.PhoneNo}&called2=&called3=&called4=&called5=&caller=12345&customerid={request.VisitId}";
            var result = await client.GetAsync(url, cancellation);
            return result.IsSuccessStatusCode
                ? ApptaskExecuteResult.Success()
                : ApptaskExecuteResult.Fail("请求呼叫中心外呼失败");
        }
    }
}
