using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.DroneCloudQuery;
using OpenAuth.App.ServiceApp.DroneCloudQueryManage.Request;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;
using Microsoft.AspNetCore.Authorization;
using OpenAuth.App.ServiceApp.Response;

namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
    /// <summary>
    /// 云查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public partial class DroneCloudQueryController : ControllerBase
    {
        private DroneCloudQueryApp _app;

        private DroneCloudLandQueryApp _land;

        public DroneCloudQueryController(DroneCloudQueryApp app, DroneCloudLandQueryApp land)
        {
            _app = app;
            _land = land;
        }

        /// <summary>
        ///添加云查询任务 
        /// </summary>
        /// <param name="geomid">图斑编号</param>
        /// <param name="caseno">案件编号</param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddDroneTask(string geomid, string caseno, string countyName)
        {
            var response = new Response<bool>();
            if (string.IsNullOrEmpty(geomid))
                throw new Exception("图斑不能为空");
            try
            {
                response = _app.AddDroneTask(geomid, caseno, countyName);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }

        /// <summary>
        /// 获取需要查询的任务列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<DroneCloudTaskResp>> LoadDroneTask()
        {
            return await _app.LoadDroneTask();
        }

        /// <summary>
        /// 添加查询结果
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateCloudQuery(CloudQueryReq req)
        {
            var response = new Response<bool>();
            try
            {
                response = await _app.UpdateCloudQuery(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }

        /// <summary>
        /// 根据云查询id获取查询结果
        /// </summary>
        /// <param name="id">结果id，消息推送的msg</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<DroneCloudQuery>> LoadCloudQueryById(string id)
        {
            return await _app.LoadCloudQueryById(id);
        }

        /// <summary>
        /// 根据geomid和caseno获取状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<DroneCloudTask>> LoadCloudQueryByCaseNo(string geomId, string caseno)
        {
            return await _app.LoadCloudQueryByCaseNo(geomId, caseno);
        }


        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCloudTask>>>> LoadCloudQueryAll(string geomId, string caseno,
            int page, int limit)
        {
            return await _app.LoadCloudQueryAll(geomId, caseno, page, limit);
        }


        /// <summary>
        ///获取土地分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<dynamic>>> getLandType()
        {
            return await _app.getLandType();
        }

        /// <summary>
        /// 保存土地分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddDroneLandType(AddOrUpdateDroneLandType req)
        {
            return _land.AddOrUpdateDroneLandType(req);
        }

        /// <summary>
        ///添加云查询任务 
        /// </summary>
        /// <param name="geomid">图斑编号</param>
        /// <param name="caseno">案件编号</param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddDroneLandTask(string geomData, string caseid, string code, string name)
        {
            var response = new Response<bool>();
            if (string.IsNullOrEmpty(geomData))
                throw new Exception("图斑数据不能为空");
            try
            {
                response = _land.AddDroneTask(geomData, caseid, code, name);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }

        /// <summary>
        /// 获取需要查询的任务列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<DroneCloudLandTask>>> LoadDroneLandTask()
        {
            return await _land.DroneCloudLandTask();
        }

        /// <summary>
        /// 添加查询结果
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateCloudLandQuery(CloudQueryReq req)
        {
            var response = new Response<bool>();
            try
            {
                response = await _land.UpdateCloudLandQuery(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }

        /// <summary>
        /// 根据云查询id获取查询结果
        /// </summary>
        /// <param name="id">结果id，消息推送的msg</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<DroneCloudLandQuery>> LoadCloudLandQueryById(string id)
        {
            return await _land.LoadCloudQueryById(id);
        }

        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<bool>> IsPublic()
        {
            return await _land.IsPublic();
        }
    }
}