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

namespace OpenAuth.WebApi.Controllers.ServiceControllers;

[Route("api/[controller]/[action]")]
[ApiController]
public class GeoTiffManagerController : ControllerBase
{
    private readonly GeoTiffManagerApp _app;

    public GeoTiffManagerController(GeoTiffManagerApp geoTiffManagerApp)
    {
        _app = geoTiffManagerApp;
    }

    /// <summary>
    /// 更新tiff影像
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<List<string>>> UpdateGeoTiff()
    {
        return await _app.UpdateGeoTiff();
    }

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

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

        return result;
    }

    [AllowAnonymous]
    [HttpPost]
    public Response<string> Test(string storeName, string layerName)
    {
        var result = _app.Test(storeName, layerName);
        return new Response<string> { Result = result };
    }

    /// <summary>
    /// 返回相交影像信息
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet]
    [AllowAnonymous]
    public async Task<Response<PageInfo<List<TifInfo>>>> GetIntersects([FromQuery] GeoTiffManagerReq req)
    {
        return new Response<PageInfo<List<TifInfo>>>
        {
            Result = await _app.GetIntersects(req)
        };
    }


    [HttpPost]
    [AllowAnonymous]
    public async Task<Response<bool>> GroupGeoTiff(string type)
    {
        return await _app.GroupGeoTiff(type);
    }

    /// <summary>
    /// 删除图层存储，级联删除图层
    /// </summary>
    /// <param name="stores"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> DeleteTifStore(string stores)
    {
        return await _app.DeleteTifStore(stores);
    }

    /// <summary>
    /// 生成图层组缩略图
    /// </summary>
    /// <param name="layerGroups"></param>
    /// <param name="bbox"></param>
    /// <param name="num"></param>
    /// <param name="height"></param>
    /// <param name="width"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UpdateLayerGroupThumb(string layerGroups, string bbox, int num, int height = 768,
        int width = 330)
    {
        return await _app.UpdateLayerGroupThumb(layerGroups, bbox, num, height, width);
    }

    [HttpGet]
    [AllowAnonymous]
    public dynamic GetTifCrs(string filePath)
    {
        return _app.GetTifCrs(filePath);
    }
}