using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pivotal.Discovery.Client;
using System.Collections.Generic;
using System.Linq.Expressions;
using System;
using MadRunFabric.Common;

namespace Api.SignToken
{
    public class HangfireService : IHangfireService
    {
        DiscoveryHttpClientHandler _handler;
        ILogger<HangfireService> _logger;

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


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

        private HttpClient GetClient()
        {
            var client = new HttpClient(_handler, false);
            return client;
        }
        /// <summary>
        /// 保养计划
        /// </summary>
        /// <param name="proname"></param>
        /// <param name="sysname"></param>
        /// <param name="equipname"></param>
        /// <param name="id"></param>
        /// <param name="expirytime"></param>
        /// <param name="advancelong"></param>
        /// <param name="content"></param>
        /// <param name="maintype"></param>
        /// <param name="plancycle"></param>
        /// <param name="plandate"></param>
        /// <param name="planhour"></param>
        /// <returns></returns>
        public async Task<string> AddWoMaintainBase(string proname, string sysname, string equipname, string id,
            string expirytime, int advancelong, string content,string maintype,
            int plancycle, int plandate, int planhour)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"proname", proname},
               {"sysname", sysname},
               {"equipname", equipname},
               {"id", id},
                {"expirytime",expirytime },
                {"advancelong",advancelong.ToString() },
                {"content",content },
                {"maintype",maintype},
               {"plancycle", plancycle.ToString()},
               {"plandate", plandate.ToString()},
               {"planhour", planhour.ToString()}
            });
            var response = await client.PostAsync(URL + "equipment/addwomaintainbase", postContent);
            _logger.LogInformation("创建保养计划定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }
        /// <summary>
        /// 保养计划
        /// </summary>
        public async Task<string> AddWoMaintainBase(string id, int plancycle, int planinterval, int plantimes)
        {
            _logger.LogError("HangfireService/AddWoMaintainBase start");
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id},
               {"plancycle", plancycle.ToString()},
               {"planinterval", planinterval.ToString()},
               {"plantimes", plantimes.ToString()}
            });
            var response = await client.PostAsync(URL + "equipment/addwomaintainbase", postContent);
            _logger.LogInformation("创建保养计划定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }
        /// <summary>
        /// 添加 设备运维计划定时任务
        /// </summary>
        /// <param name="projectid">项目id</param>
        /// <param name="sysnameid">系统</param>
        /// <param name="equipmentid">设备</param>
        /// <param name="id">计划id</param>
        /// <param name="plancycle"></param>
        /// <param name="plandate"></param>
        /// <param name="planhour"></param>
        /// <returns></returns>
        public async Task<string> AddWoEquipmentRunning(string projectid, string sysnameid, string equipmentid, string id, 
            string expirytime,int advancelong, int plancycle, int plandate, int planhour)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"projectid", projectid},
               {"sysnameid", sysnameid},
               {"equipmentid", equipmentid},
               {"id", id},
                {"expirytime",expirytime },
                {"advancelong",advancelong.ToString() },
               {"plancycle", plancycle.ToString()},
               {"plandate", plandate.ToString()},
               {"planhour", planhour.ToString()}
            });
            var response = await client.PostAsync(URL + "equipment/addwoequipmentrunning", postContent);
            _logger.LogInformation("创建设备运维计划定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }
        public async Task<string> AddWoEquipmentRunning(string id, int plancycle, int planinterval, int plantimes)
        {
            _logger.LogError("HangfireService/AddWoEquipmentRunning start");
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id},
               {"plancycle", plancycle.ToString()},
               {"planinterval", planinterval.ToString()},
               {"plantimes", plantimes.ToString()}
            });
            var response = await client.PostAsync(URL + "equipment/addwoequipmentrunning", postContent);
            _logger.LogInformation("创建设备运维计划定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }
        /// <summary>
        /// 保持超时工单定时任务
        /// </summary>
        /// <param name="woid"></param>
        /// <param name="wotype"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> SaveTimeOutRunning(string id, int wotype, int plancycle, int planinterval)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id},
               {"wotype", wotype.ToString()},
               {"plancycle", plancycle.ToString()},
               {"planinterval", planinterval.ToString()},
            });
            var response = await client.PostAsync(URL + "equipment/savetimeout", postContent);
            _logger.LogInformation("创建超时工单定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 根据id删除定时任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> RemovePlan(string id)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id}
            });
            var response = await client.PostAsync(URL + "equipment/remove", postContent);
            _logger.LogInformation("删除定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }

        public string GetInfo()
        {
            var client = GetClient();
            var result = client.GetStringAsync(URL + "info").Result;
            _logger.LogInformation("返回的权限信息为: {0}", result);
            return result;
        }


        #region 配送物品计划
        /// <summary>
        /// 添加配送计划 - 来源 DistriWorkOrderPlanController
        /// </summary>
        /// <param name="id"></param>
        /// <param name="plancycle"></param>
        /// <param name="planinterval"></param>
        /// <param name="plantimes"></param>
        /// <returns></returns>
        public async Task<string> AddDistriWorkOrderPlan(string id, int plancycle, int planinterval, int plantimes, string extensiontime)
        {
            _logger.LogError("HangfireService/AddWoEquipmentRunning start ");
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id},
               {"plancycle", plancycle.ToString()},
               {"planinterval", planinterval.ToString()},
               {"plantimes", plantimes.ToString()},
               {"extensiontime", extensiontime}
            });
            //访问接口 HangfireDistributionPlanController
            var response = await client.PostAsync(URL + "hangfiredistributionplan/adddistributionplan", postContent);
            _logger.LogInformation("创建配送物品计划定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 删除配送计划 - 定时任务 - 来源 DistriWorkOrderPlanController
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> RemoveDistriWorkOrderPlan(string id)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id}
            });
            //访问接口 HangfireDistributionPlanController
            var response = await client.PostAsync(URL + "hangfiredistributionplan/remove", postContent);
            _logger.LogInformation("删除定时任务成功");
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 自动定时计划 - 自动分配
        /// </summary>
        /// <param name="id"></param>
        /// <param name="wotype"></param>
        /// <param name="plancycle"></param>
        /// <param name="planinterval"></param>
        /// <returns></returns>
        public async Task<string> SaveAutoAssign(string id, int wotype, int plancycle, int planinterval)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"id", id},
               {"wotype", wotype.ToString()},
               {"plancycle", plancycle.ToString()},
               {"planinterval", planinterval.ToString()},
            });
            var response = await client.PostAsync(URL + "hangfiredistributionplan/saveautoassign", postContent);
            _logger.LogInformation("创建自动定时(自动分配)任务成功");
            return await response.Content.ReadAsStringAsync();
        }
        #endregion
    }
}
