using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.DroneCaseInfo;
using OpenAuth.App.ServiceApp.InsTaskHallManager;
using OpenAuth.App.ServiceApp.Request;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DroneCaseinfoController : ControllerBase
    {
        private readonly DroneCaseinfoApp _app;
        public DroneCaseinfoController(DroneCaseinfoApp app)
        {
            _app = app;
        }


        /// <summary>
        /// 无人机添加案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> AddDroneCaseByDrone([FromBody] AddDroneCaseByDroneReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.AddDroneCaseByDrone(req);
            }
            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<AddOrUpdateDroneCaseInfoDataBaseReqExt> GetCaseInfo(string id)
        {
            Response<AddOrUpdateDroneCaseInfoDataBaseReqExt> response = new Response<AddOrUpdateDroneCaseInfoDataBaseReqExt>();
            try
            {
                response.Result = _app.GetCaseInfo(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 获取GeoJson
        /// PC获取图层的GeoJson（判读页面用）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<JToken> GetDroneGeoJson(string id)
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                response.Result = _app.GetDroneGeoJson(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件判读
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> UpdateDroneCaseInfoIntact([FromBody] AddOrUpdateDroneCaseInfoDataBaseReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.UpdateDroneCaseInfoIntact(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 上报案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> AddDroneCaseInfo([FromBody] AddOrUpdateDroneCaseInfoReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                if (string.IsNullOrEmpty(req.info.Id))
                {
                    //新增
                    response.Result = _app.AddDroneCaseInfo(req);
                }
                else
                {
                    //修改
                    response.Result = _app.UpdateDroneCaseInfo(req);
                }
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 删除文件 - 通过路径删除
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> DeleteDroneFilesByPath(string[] paths)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.DeleteDroneFilesByPath(paths);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 关闭案件（判读用）
        /// </summary>
        /// <param name="id">案件id</param>
        /// <returns></returns>
        [HttpGet]
        public Response<string> CloseDroneCaseInfo(string id)
        {
            Response<string> response = new Response<string>();
            try
            {
                response.Result = _app.CloseDroneCaseInfo(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 添加违建图层
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> AddIllegalBuildeLayer([FromBody] DroneShpData model)
        {
            var res = new Response<string>();
            try
            {
                res.Result = _app.AddIllegalBuildeLayer(model);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 删除图斑
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<string> DeleteDroneLayers(int gid)
        {
            var res = new Response<string>();
            try
            {
                res.Result = _app.DeleteDroneLayers(gid);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 根据经纬度获取组织机构
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetOrgAreaByPoint(decimal lng, decimal lat)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                response.Result = _app.GetOrgAreaByPoint(lng, lat);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 查询处理案件列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpGet]
        public TableData LoadDealCaseInfoList([FromQuery] QueryDealCaseInfoListReq obj)
        {
            TableData response = new TableData();
            try
            {
                response = _app.LoadDealCaseInfoList(obj);
            }
            catch (Exception ex)
            {
                response.code = 500;
                response.msg = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        #region 复提案件
        /// <summary>
        /// 复提案件
        /// </summary>
        /// <param name="id">案件id</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Response<bool> ReSubmitCaseInfo([FromBody] ReSubmitInfo submitInfo)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response.Result = _app.ReSubmitCaseInfo(submitInfo);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region 关闭案件
        /// <summary>
        /// 关闭案件
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public Response<bool> CloseCaseInfo([FromBody] CloseCaseInfo submitInfo)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response.Result = _app.CloseCaseInfo(submitInfo);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
