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;

namespace Api.SignToken
{
    public class EquipmentService : IEquipmentService
    {
        DiscoveryHttpClientHandler _handler;
        ILogger<EquipmentService> _logger;

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


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

        private HttpClient GetClient()
        {
            var client = new HttpClient(_handler, false);
            return client;
        }

        /// <summary>
        /// 添加保养工单（定时调用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="func"></param>
        /// <param name="cron"></param>
        /// <returns></returns>
        public async Task<string> AddWoMaintainBaseAsync(string proname,string sysname,string equipname,string maintainplan, string expirytime, int advancelong, string content, string type)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"proname", proname},
               {"sysname", sysname},
               {"equipname", equipname},
               {"plan", maintainplan},
               {"expirytime",expirytime },
                {"advancelong",advancelong.ToString() },
                {"maintaincontent",content },
                {"timemaintaintype1",type },
            });
            var response = await client.PostAsync(URL + "WoMaintainBase/add", postContent);
            return await response.Content.ReadAsStringAsync();

        }
        public async Task<string> AddWoMaintainBaseAsync(string planid)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"planid", planid}
            });
            var response = await client.PostAsync(URL + "WoMaintainBase/add", postContent);
            return await response.Content.ReadAsStringAsync();

        }
        /// <summary>
        /// 添加巡检工单（定时调用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="func"></param>
        /// <param name="cron"></param>
        /// <returns></returns>
        public async Task<string> AddWoEquipmentRunningAsync(string projectid, string sysnameid, string equipmentid, string planid, string expirytime, int advancelong)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"projectid", projectid},
               {"sysnameid", sysnameid},
               {"equipmentid", equipmentid},
               {"planid", planid},
               {"expirytime",expirytime },
                {"advancelong",advancelong.ToString() },
                
            });
            var response = await client.PostAsync(URL + "WoEquipmentRunning/add", postContent);
            return await response.Content.ReadAsStringAsync();
        }
        public async Task<string> AddWoEquipmentRunningAsync(string planid)
        {
            _logger.LogError("EquipmentService/AddWoEquipmentRunningAsync  ");
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"planid", planid}
            });
            var response = await client.PostAsync(URL + "WoEquipmentRunning/add", postContent);
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 添加超时工单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="woid"></param>
        /// <param name="wotype"></param>
        /// <returns></returns>
        public async Task<string> SaveTimeOutAsync(int wotype)
        {
            var client = GetClient();
            HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
            {
               {"wotype", wotype.ToString()},

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

        /// <summary>
        /// 超时系统评价工单
        /// </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 + "WoRepairBase/timeoutevaluate", postContent);
            return await response.Content.ReadAsStringAsync();
        }

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