using IceCoffee.Common.Templates;
using langguanApi.Common.Proxy;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using langguanApi.Model.Dto;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Writers;
namespace langguanApi.Service
{
    /// 
    /// 环境治理页面Service
    /// 
    [ServiceInjection(InjectionType.Transient)]
    public class EnvGovService
    {
        private HttpProxy _httpProxy;
        private readonly IConfiguration _configuration;
        public EnvGovService(HttpProxy httpProxy, IConfiguration configuration)
        {
            _httpProxy = httpProxy;
            _configuration = configuration;
        }
        /// 
        /// 首页数据
        /// 
        /// 
        public async Task IndexView()
        {
            var url = _configuration.GetValue("Apis:DeviceList");
            var deviceResp = await _httpProxy.Get>(null, url);
            var deviceList = deviceResp.data.List.GroupBy(g => g.GroupID)
                .Select(s => new
                {
                    group = s.Key,
                    items = new
                    {
                        devices = s.ToList().GroupBy(m => m.DeviceType)
                        .Select(o => new
                        {
                            devicetype = ConvertDeviceType(o.Key),
                            items = o.ToList()
                             .Select(d => new
                             {
                                 d.DeviceID,
                                 d.DeviceName,
                                 d.Lon,
                                 d.Lat,
                                 d.CurrentAngel,
                                 d.RunLog
                             })
                        })
                    }
                });
            return new ApiResult
            {
                code = deviceResp.code,
                msg = deviceResp.msg,
                data = deviceList
            };
        }
        private string ConvertDeviceType(int deviceType)
        {
            switch (deviceType)
            {
                case 1:
                    return "雾炮";
                case 2:
                    return "雾桩";
                case 3:
                    return "干雾";
            }
            return null;
        }
        /// 
        /// 获取设备状态
        /// 
        /// 
        /// 
        public async Task DeviceStatu(string deviceId)
        {
            var url = _configuration.GetValue("Apis:DeviceStatu");
            var deviceStatuResp = await _httpProxy.Get>>(null, url);
            var result = deviceStatuResp.data.Where(s => s.DeviceID == deviceId).FirstOrDefault();
            return new ApiResult
            {
                //接口返回错误,暂时注释
                //  code = deviceStatuResp.code,
                code = 0,
                msg = deviceStatuResp.msg,
                data = result
            };
        }
    }
}