using Api.SignToken;
using Microsoft.Extensions.Logging;
using Pivotal.Discovery.Client;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Api.SignToken
{
    /// <summary>
    /// 配送物品计划
    /// </summary>
    public class DistributionPlanService : IDistributionPlanService
    {
        DiscoveryHttpClientHandler _handler;
        ILogger<IDistributionPlanService> _logger;

        /// <summary>
        /// URL
        /// </summary>
        private const string URL = "http://distributionapi/api/";


        public DistributionPlanService(IDiscoveryClient client, ILoggerFactory logFactory)
        {
            _handler = new DiscoveryHttpClientHandler(client, logFactory.CreateLogger<DiscoveryHttpClientHandler>());
            _logger = logFactory.CreateLogger<DistributionPlanService>();
        }

        private HttpClient GetClient()
        {
            var client = new HttpClient(_handler, false);
            return client;
        }
        
        /// <summary>
        /// 添加计划任务 - 来源 HangfireDistributionPlanController
        /// </summary>
        /// <param name="planid"></param>
        /// <returns></returns>
        public async Task<string> AddDistriWorkOrderPlanAsync(string planid)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"planid", planid}
            });
            var response = await client.PostAsync(URL + "distriworkorderplan/addhangfire", postContent);
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 自动定时计划 - 自动分配
        /// </summary>
        /// <param name="wotype"></param>
        /// <returns></returns>
        public async Task<string> SaveAutoAssignAsync(int wotype)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"wotype", wotype.ToString()},

            });
            var response = await client.PostAsync(URL + "distriworkorderplan/saveautoassignhangfire", postContent);
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 超时系统评价工单 
        /// DistributionApi -> DistriWorkOrderController
        /// </summary>
        /// <param name="score"></param>
        /// <param name="createusername"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public async Task<string> TimeOutEvaluateAsync(int score = 5, string createusername = "系统", int channel = 5)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"score", score.ToString()},
               {"createusername", createusername},
               {"channel", channel.ToString()}
            });
            var response = await client.PostAsync(URL + "distriworkorder/distritimeoutevaluate", postContent);
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 获取计划定时事件详情 
        /// DistributionApi -> DistriWorkOrderPlanController
        /// </summary>
        /// <param name="score"></param>
        /// <param name="createusername"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public async Task<string> GetHangfirePlanInfo(string id)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id}
            });
            var response = await client.PostAsync(URL + "hangfireplantimeing/gethangfireplaninfo", postContent);
            return await response.Content.ReadAsStringAsync();
        }

    }
}
