using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NPOI.HPSF;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App;
using System.Text;
using OpenAuth.App.DroneScreenDisplay;
using OpenAuth.App.DroneScreenDisplay.Response;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.App.ServiceApp.DroneScreenDisplay.Response;
using NPOI.SS.Formula.Functions;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 无人机执法 大屏展示
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DroneScreenDisplayController : ControllerBase
    {
        IConfiguration _configuration;
        DroneScreenDisplayApp _app;
        IAuth _auth;
        ILogger<DroneScreenDisplayController> _logger;

        public DroneScreenDisplayController(IConfiguration configuration, DroneScreenDisplayApp app, IAuth auth, ILogger<DroneScreenDisplayController> logger)
        {
            _configuration = configuration;
            _app = app;
            _auth = auth;
            _logger = logger;
        }

        /// <summary>
        /// 正式案件记录、最新上报案件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<List<dynamic>> LoadDroneCase(string streetid)
        {
            Response<List<dynamic>> response = new Response<List<dynamic>>();
            try
            {
                response.Result = _app.LoadDroneCase(streetid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<ScreenDroneCaseInfoReq>> GetCaseInfo(string id)
        {
            Response<ScreenDroneCaseInfoReq> response = new Response<ScreenDroneCaseInfoReq>();
            try
            {
                response = await _app.GetCaseInfo(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 处理详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<AddOrUpdateDroneCaseDealReq> GetDroneCaseDeal(string id, string caseid)
        {
            Response<AddOrUpdateDroneCaseDealReq> response = new Response<AddOrUpdateDroneCaseDealReq>();
            try
            {
                response.Result = _app.GetDroneCaseDeal(id, caseid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 最新案件处理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<object> LoadDroneCaseDeal(int? today, string areaid)
        {
            Response<object> response = new Response<object>();
            try
            {
                response.Result = _app.LoadDroneCaseDeal(today, areaid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 上报和处理案件数量
        /// </summary>
        /// <param name="countyid">县区id</param>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetTodayReportAndDealCount(string streetid)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetTodayReportAndDealCount(streetid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件数量统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetDroneCaseCount()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetDroneCaseCount();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 违建分类统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<object> GetDroneTypeCount(string areaid)
        {
            Response<object> response = new Response<object>();
            try
            {
                response.Result = _app.GetDroneTypeCount(areaid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 按乡镇统计
        /// </summary>
        /// <param name="areaid">行政区划id</param>
        /// <returns></returns>
        [HttpGet]
        public Response<JArray> GetDroneOrgCount(string areaid)
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                response.Result = _app.GetDroneOrgCount(areaid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件线索统计-费城街道工作区
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JArray> GetDroneCityOrgCount()
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                response.Result = _app.GetDroneCityOrgCount();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 数据统计，按天统计图表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JArray> GetDroneCaseDateCount(string type, string streetId)
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                response.Result = _app.GetDroneCaseDateCount(type, streetId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 查询案件的GeoJson、地图展示上报案件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JToken> GetDroneGeoJson()
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                response.Result = _app.GetDroneGeoJson();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取无人机的位置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public object GetDroneLocationPoints()
        {
            Response<object> response = new Response<object>();
            try
            {
                response.Result = _app.GetDroneLocationPoints();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取无人机的历史轨迹
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="userId">人员Id</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public object GetDroneLocationPointsHistory(DateTime startTime, DateTime endTime, string userId)
        {
            Response<object> response = new Response<object>();
            try
            {
                response.Result = _app.GetDroneLocationPointsHistory(startTime, endTime, userId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 无人机飞行 假数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<object> GetDroneLocationPointsFalse()
        {
            Response<object> response = new Response<object>();
            try
            {
                response.Result = _app.GetDroneLocationPointsFalse();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取无人机的在线率
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetDroneOnlinePercent()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetDroneOnlinePercent();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 统计图表已完成未完成数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetDroneCaseCompleteCount(string areaid)
        {
            Response<JObject> res = new Response<JObject>();
            try
            {
                res.Result = _app.GetDroneCaseCompleteCount(areaid);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }

        ///// <summary>
        ///// 切片
        ///// </summary>
        ///// <param name="x"></param>
        ///// <param name="y"></param>
        ///// <param name="z"></param>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpGet]
        //public IActionResult getVectorTile(int x, int y, int z)
        //{
        //    List<double> lons = Util.getLon(x, z);
        //    List<double> lats = Util.getLat(y, z);
        //    double lonmin = lons[0];
        //    double lonmax = lons[1];
        //    double latmin = lats[1];
        //    double latmax = lats[0];

        //    StringBuilder sql = new StringBuilder();
        //    sql.AppendFormat($" SELECT ST_AsMVT(tile, 'lines', 4096, 'geom') tile FROM (SELECT 'shp_drone_community', ST_AsMVTGeom(geom, ST_Transform(ST_MakeEnvelope({lonmin},{latmin},{lonmax},{latmax}, 4326),4326),4096, 256, true) AS geom FROM public.shp_drone_community ) AS tile");

        //    using (var db = SqlSugarOper.GetInstance(_configuration))
        //    {
        //        var dataTable = db.Ado.GetDataTable(sql.ToString());
        //        byte[] result = (Byte[])dataTable.Rows[0]["tile"];

        //        return File(result, "application/octet-stream");
        //    }
        //}

        /// <summary>
        /// 是否是费城街道
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<int> IsFeiChengStreet()
        {
            var res = new Response<int>();
            try
            {
                res.Result = _app.IsFeiChengStreet();
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        #region 新版大屏
        /// <summary>
        /// 总数与面积统计
        /// </summary>
        /// <param name="countyid">地区id</param>
        /// <param name="type">案件类型</param>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetTotalAndArea(string countyid, string type)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetTotalAndArea(countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 右上角统计
        /// </summary>
        /// <param name="countyid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetTotalAndAreaByType(string countyid, string type)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetTotalAndAreaByType(countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 图斑汇总统计
        /// </summary>
        /// <param name="countyid">县区</param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<SummaryResp>> GetTuBanSummary(string countyid, string type)
        {
            Response<List<SummaryResp>> response = new Response<List<SummaryResp>>();
            try
            {
                response = _app.GetTuBanSummary(countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取图斑图例 舍弃
        /// </summary>
        /// <param name="countyid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [NonAction]
        [HttpGet]
        public Response<Dictionary<string, List<DroneShpDataResp>>> GetTuBanInfoByType(string countyid, string type)
        {
            Response<Dictionary<string, List<DroneShpDataResp>>> response = new Response<Dictionary<string, List<DroneShpDataResp>>>();
            try
            {
                response = _app.GetTuBanInfoByType(countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 趋势统计
        /// </summary>
        /// <param name="datatype">时间类型（0按天统计，1按月统计）</param>
        /// <param name="begindate">开始时间（不传统计10天或者10个月）</param>
        /// <param name="enddate">结束时间（可以不传）</param>
        /// <param name="countyid">县区id</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<dynamic>> GetTrendData(int datatype, string begindate, string enddate, string countyid, string type)
        {
            Response<List<dynamic>> response = new Response<List<dynamic>>();
            try
            {
                response = _app.GetTrendData(datatype, begindate, enddate, countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region 新版大屏 2.0
        /// <summary>
        /// 新版大屏 右上角柱状图
        /// </summary>
        /// <param name="type">类型1-下发，2-核实，3-整改</param>
        /// <param name="month">全部-0</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<OffenceEChartResp>> CaseOffenceEcharts(int type, DateTime month)
        {
            Response<List<OffenceEChartResp>> response = new Response<List<OffenceEChartResp>>();
            try
            {
                response = _app.CaseOffenceEcharts(type, month);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 新版右上角趋势图
        /// </summary>
        /// <param name="datatype">时间类型（0按天统计，1按月统计）</param>
        /// <param name="begindate">开始时间（不传统计10天或者10个月）</param>
        /// <param name="enddate">结束时间（可以不传）</param>
        /// <param name="countyid">县区id</param>
        /// <param name="type">类型1-下发，2-核实，3-整改</param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<dynamic>> GetTrendEchartsData(int datatype, string begindate, string enddate, string countyid, int type)
        {
            Response<List<dynamic>> response = new Response<List<dynamic>>();
            try
            {
                response = _app.GetTrendEchartsData(datatype, begindate, enddate, countyid, type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 数据分析
        /// </summary>
        /// <param name="countyid">县区id，全部不传</param>
        /// <param name="month">月份，必传</param>
        /// <returns></returns>
        [HttpGet]
        public Response<dynamic> GetAnalysisData(string countyid, DateTime month)
        {
            Response<dynamic> response = new Response<dynamic>();
            try
            {
                response = _app.GetAnalysisData(countyid, month);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        /// <summary>
        /// 下发图斑
        /// </summary>
        /// <param name="month">月份，必传</param>
        /// <returns></returns>
        [HttpGet]
        public Response<dynamic> CaseOffenceXiaFa(DateTime month)
        {
            Response<dynamic> response = new Response<dynamic>();
            try
            {
                response = _app.CaseOffenceXiaFa( month);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 核实新增
        /// </summary>
        /// <param name="month">月份，必传</param>
        /// <returns></returns>
        [HttpGet]
        public Response<dynamic> CaseOffenceCheckAdd(DateTime month)
        {
            Response<dynamic> response = new Response<dynamic>();
            try
            {
                response = _app.CaseOffenceCheckAdd( month);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 整改剩余
        /// </summary>
        /// <param name="month">月份，必传</param>
        /// <returns></returns>
        [HttpGet]
        public Response<dynamic> CaseOffenceModifyRemain(DateTime month)
        {
            Response<dynamic> response = new Response<dynamic>();
            try
            {
                response = _app.CaseOffenceModifyRemain( month);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
    }
}
