using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.DroneCloudQuery;
using OpenAuth.App.ServiceApp.DroneCloudQuery.Request;
using OpenAuth.App.ServiceApp.DroneCloudQueryManage.Request;
using OpenAuth.Repository.Domain;

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

        public DroneCloudQueryContentController(DroneCloudQueryContentApp app)
        {
            _app = app;
        }

        // todo 内网可以访问外网
        // todo 配置云查询内容 列表，添加、修改

        /// <summary>
        /// 列表页
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCloudQueryContent>>>> LoadPage(
            [FromQuery] DroneCloudQueryContentReq req)
        {
            return await _app.LoadPage(req);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<DroneCloudQueryContent> Get(string id)
        {
            var result = new Response<DroneCloudQueryContent>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        [HttpPost]
        public Response Add(AddOrUpdateCloudQueryContentReq obj)
        {
            var result = new Response();
            try
            {
                _app.Add(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }


        [HttpPost]
        public Response Update(AddOrUpdateCloudQueryContentReq obj)
        {
            var result = new Response();
            try
            {
                _app.Update(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Response Delete(string id)
        {
            var result = new Response();
            try
            {
                _app.Delete(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}