using ce.autofac.extension;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.ShpGeo;
using OpenAuth.App.ServiceApp.ShpGeo.Request;
using OpenAuth.App.Shape.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceControllers;

[Route("api/[controller]/[action]")]
[ApiController]
public class ShpGeoLayerController : ControllerBase
{
    private readonly ShpGeoLayerApp _shpGeoLayerApp;

    public ShpGeoLayerController(ShpGeoLayerApp shpGeoLayerApp)
    {
        this._shpGeoLayerApp = shpGeoLayerApp;
    }

    /// <summary>
    ///  geo服务添加 / 云查询图层管理
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> Add(ShpGeoLayerShpReq obj)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpGeoLayerApp.Add(obj);
        }
        catch (Exception ex)
        {
            result.Code = 500;
            result.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return result;
    }

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

    [HttpGet]
    public Response<ShpGeoLayer> Get(string id)
    {
        var result = new Response<ShpGeoLayer>();
        try
        {
            result.Result = _shpGeoLayerApp.Get(id);
        }
        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(UpdateGeoLayerShpReq updateLayerReq)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpGeoLayerApp.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>
    [HttpPost("{id}")]
    public async Task<Response<bool>> Delete(string id)
    {
        var result = new Response<bool>();
        try
        {
            result = await _shpGeoLayerApp.Delete(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="tableName">表名</param>
    /// <param name="srid">空间参考</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<ShpInfo>> ParseShpInfo(string zipFilePath, string tableName,
        string srid)
    {
        var result = new Response<ShpInfo>();
        if (string.IsNullOrEmpty(zipFilePath))
        {
            result.Code = 500;
            result.Message = "未传文件路径";
            return result;
        }

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

        return result;
    }

    /// <summary>
    /// 获取shp数据表中心坐标
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<string>> GetGeomData(string tableName)
    {
        return await _shpGeoLayerApp.GetGeomData(tableName);
    }

    [HttpPost]
    public async Task<Response<bool>> Test(string tableName, string serverName, string srid, string shpPath)
    {
        return await _shpGeoLayerApp.Test1(tableName, serverName, srid, shpPath);
       //return await _shpGeoLayerApp.Test( tableName,  serverName,  srid,  shpPath);
    }
    
    [HttpGet]
    [AllowAnonymous]
    public IActionResult QueryVectorTileByTable([FromQuery] QueryVectorTileByTableReq req)
    {
        
        var result = _shpGeoLayerApp.QueryVectorTileByTable(req);
        return File(result, "application/octet-stream");
    }

    [HttpGet]
    public dynamic GetDataTableRecord(string dataTable, string id)
    {
        return _shpGeoLayerApp.GetDataTableRecord(dataTable,id);
    }

}