using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.BaseApp.Shape;
using OpenAuth.App.BaseApp.Shape.Request;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.ShpGeo.Request;
using OpenAuth.App.ServiceApp.ShpGeo;
using OpenAuth.App.Shape.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.BaseControllers;

[Route("api/[controller]/[action]")]
[ApiController]
public class ShpLayerSourceController : ControllerBase
{
    private readonly ShpLayerSourceApp _shpLayerSourceApp;

    public ShpLayerSourceController(ShpLayerSourceApp shpLayerSourceApp)
    {
        this._shpLayerSourceApp = shpLayerSourceApp;
    }

    // 添加图层
    [Obsolete("废弃")]
    [HttpPost]
    public async Task<Response<bool>> Add(ShpLayerSource obj)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.Add(obj);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    [HttpGet]
    public async Task<Response<PageInfo<List<ShpLayerSource>>>> LoadPage([FromQuery] ShpLayerSourceReq req)
    {
        return await _shpLayerSourceApp.LoadPage(req);
    }

    [HttpGet]
    public Response<ShpLayerSource> Get(string id)
    {
        var result = new Response<ShpLayerSource>();
        try
        {
            result.Result = _shpLayerSourceApp.Get(id);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 解析shp文件
    /// </summary>
    /// <param name="zipFilePath">zip文件相对路径</param>
    /// <param name="serviceName">服务名称</param>
    /// <param name="tableName">表名</param>
    /// <param name="srid">空间参考</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<ShpInfo>> ParseShpInfo(string zipFilePath, string serviceName, string tableName,
        string srid)
    {
        var result = new Response<ShpInfo>();
        if (string.IsNullOrEmpty(zipFilePath))
        {
            result.Code = 500;
            result.Message = "未传文件路径";
            return result;
        }

        try
        {
            result = await _shpLayerSourceApp.ParseShpInfo(zipFilePath, serviceName, tableName, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    [HttpPost]
    public async Task<Response<bool>> CreateDataTable(ShpInfo shpInfo)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.CreateDataTable(shpInfo);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="filePath">shp文件路径</param>
    /// <param name="tableName">表名</param>
    /// <param name="srid">空间参考</param>
    /// <returns></returns>
    // POST: api/<ValuesController>
    [Obsolete("不再使用，替换为ParseShpInfo")]
    [HttpPost]
    public Task<string> CreateGISLayer(string filePath, string tableName, string srid, string serverName)
    {
        if (string.IsNullOrEmpty(tableName))
        {
            throw new ArgumentNullException("未传表名");
        }

        return _shpLayerSourceApp.CreateGISLayer(filePath, tableName, srid, serverName);
    }


    /// <summary>
    /// 取得矢量切片
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    // GET api/<ValuesController>/5
    [HttpGet]
    public IActionResult GetMapbox([FromQuery] QueryMapboxReq req)
    {
        // 根据serviceId取得服务信息，得到图斑表，查询
        // TODO: 图层相关接口  1. 图层列表；2. 新建图层
        // 上传shp， 根据前端参数，新建指定空间参数的表，
        // 上传图层步骤，上传shp建的表，如何与之关联？在服务资源对应的表中，存储表名？

        // 传serviceId ,还是tableName ? 
        var result = _shpLayerSourceApp.QueryMapbox(req);
        return File(result, "application/octet-stream");
    }

    [HttpGet]
    [Obsolete("不再使用")]
    public Task<string> test()
    {
        return _shpLayerSourceApp.TestReadShpFeature("20240528\\2024052816004685980256.zip");
    }

    /// <summary>
    /// 发布图层
    /// </summary>
    /// <param name="publishLayerReq"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    [HttpPost]
    public async Task<Response<bool>> PublishLayer(PublishLayerReq publishLayerReq)
    {
        if (publishLayerReq.Name.IsNullOrEmpty())
        {
            throw new ArgumentNullException("未传图层名称");
        }

        if (publishLayerReq.Description.IsNullOrEmpty())
        {
            throw new ArgumentNullException("未传图层描述");
        }

        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.PublishLayer(publishLayerReq);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 更新图层
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateLayer(UpdateLayerReq updateLayerReq)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateLayer(updateLayerReq);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 删除图层
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task<Response<bool>> DeleteLayer(string id)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.DeleteLayer(id);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    ///查询图斑数据
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<PageInfo<List<NoEntityGeom>>>> GetShapeData([FromBody] ShapePageReq req)
    {
        var result = new Response<PageInfo<List<NoEntityGeom>>>();
        try
        {
            result = await _shpLayerSourceApp.GetShapeData(req);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 违法处理图斑查询(扩展到已有专题)
    /// </summary>
    /// <param name="gid"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<List<dynamic>>> GetIllegalShapeData(string caseid, string category)
    {
        var result = new Response<List<dynamic>>();
        try
        {
            result = await _shpLayerSourceApp.GetIllegalShapeData(caseid,category);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 保存图斑数据
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<List<string>>> SaveShapeData([FromBody] ShapeDataForm req)
    {
        var result = new Response<List<string>>();
        try
        {
            result = await _shpLayerSourceApp.SaveShapeData(req);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    #region 地图配置

    /// <summary>
    /// 查询配置
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<FrontendSettingConfig>> LoadSettingConfig(string code)
    {
        var result = new Response<FrontendSettingConfig>();
        try
        {
            result = await _shpLayerSourceApp.LoadSettingConfig(code);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 添加配置
    /// </summary>
    [HttpPost]
    public async Task<Response<bool>> AddSettingConfig(FrontendSettingConfig obj)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.AddSettingConfig(obj);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 修改配置
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateSettingConfig(FrontendSettingConfig obj)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateSettingConfig(obj);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 删除配置
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> DeleteSettingConfig(string code)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.DeleteSettingConfig(code);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    #endregion

    #region shp导入

    /// <summary>
    /// 违法用地
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpData(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpData(zipFilePath,srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 非粮化
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataFLH(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataFLH(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 重点问题1
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataZDWT1(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataZDWT1(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 重点问题2
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataZDWT2(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataZDWT2(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 巡查审计
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataXCSJ(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataXCSJ(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 卫片下发
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataWPXF(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataWPXF(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    /// <summary>
    /// 矿产导入
    /// </summary>
    /// <param name="updateLayerReq"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateCaseInfoShpDataMinerals(string zipFilePath, string srid)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpLayerSourceApp.UpdateCaseInfoShpDataMinerals(zipFilePath, srid);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

    #endregion
}