using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz.Util;
using SqlSugar;
using System.ComponentModel;
using System.Reflection;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseController;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.BasicInfo;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;

namespace WIDESEAWCS_Server.Controllers.BasicInfo
{
    [Route("api/Router")]
    [ApiController]
    public class RouterController : ApiBaseController<IRouterService, Dt_Router>
    {
        private readonly IDeviceInfoRepository _deviceInfoRepository;
        private readonly IDeviceProtocolRepository _deviceProtocolRepository;
        public RouterController(IRouterService service, IDeviceInfoRepository deviceInfoRepository, IDeviceProtocolRepository deviceProtocolRepository) : base(service)
        {
            _deviceInfoRepository = deviceInfoRepository;
            _deviceProtocolRepository = deviceProtocolRepository;
        }

        [HttpPost, Route("QueryRoutes"), AllowAnonymous]
        public List<Dt_Router> QueryRoutes(string startPosi, string endPosi)
        {
            return Service.QueryNextRoutes(startPosi, endPosi);
        }

        [HttpPost, Route("QueryAllPositions"), AllowAnonymous]
        public List<string> QueryAllPositions(string deviceCode)
        {
            return Service.QueryAllPositions(deviceCode);
        }

        [HttpPost, Route("GetAllWholeRouters"), AllowAnonymous]
        public WebResponseContent GetAllWholeRouters()
        {
            WebResponseContent content = new();
            try
            {
                List<object> dynamics = Service.GetAllWholeRouters();

                content = WebResponseContent.Instance.OK(data: dynamics);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;        }

        [HttpPost, Route("GetBaseRouterInfo"), AllowAnonymous]
        public WebResponseContent GetBaseRouterInfo()
        {
            try
            {
                #region 获取路由类型
                List<object> routerTypes = new List<object>();
                Type routerType = typeof(RouterInOutType);
                List<int> routerIndexs = Enum.GetValues(typeof(RouterInOutType)).Cast<int>().ToList();
                int routerIndex = 0;
                foreach (var item in routerIndexs)
                {
                    FieldInfo? fieldInfo = routerType.GetField(((RouterInOutType)item).ToString());
                    DescriptionAttribute? description = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
                    if (description != null)
                    {
                        routerTypes.Add(new { key = item.ToString(), value = description.Description });
                    }
                    else
                    {
                        routerTypes.Add(new { key = item.ToString(), value = item.ToString() });
                    }
                    routerIndex++;
                }
                #endregion

                #region 获取设备编号
                object deviceCodes = _deviceProtocolRepository.QueryData(x => true).GroupBy(x => x.DeviceChildCode).Select(x => new { key = x.Key, value = x.Key }).ToList();

                object areaInfos = Enum.GetNames(typeof(AreaInfo)).Select(x => new { key = x, value = x }).ToList();


                #endregion

                return WebResponseContent.Instance.OK(data: new { routerTypes, deviceCodes, areaInfos });
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }

        }

        [HttpPost, Route("AddRouters"), AllowAnonymous]
        public WebResponseContent AddRouters([FromBody] List<RoutersAddDTO> routersAddDTOs, int routerType)
        {
            return Service.AddRouters(routersAddDTOs, routerType);
        }
    }
}