using DocumentFormat.OpenXml.Wordprocessing;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.LayerManagerApp;
using OpenAuth.App.ServiceApp.LayerManagerApp.Request;
using Org.BouncyCastle.Ocsp;

namespace OpenAuth.WebApi.Controllers.ServiceControllers.DataMaintenance;

/// <summary>
///  图层
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class LayerController : ControllerBase
{
    private readonly LayerApp _app;

    public LayerController(LayerApp layerApp)
    {
        _app = layerApp;
    }

    /// <summary>
    ///  excel 上传
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> UploadExcel(LayerReq req)
    {
        return await _app.UploadExcel(req);
    }

    /// <summary>
    /// shape文件上传
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    [RequestSizeLimit(214748364800)]
    public async Task<Response<bool>> UploadShape(string zipFilePath, string tableName)
    {
        return await _app.UploadShape3( zipFilePath, tableName);
    }

    [HttpGet]
    [AllowAnonymous]
    public Response<Dictionary<string, object>> TableDataByTableName(string tablename,string type,string keyword,string keyvalue,int page=1,int limit = 10) { 
        return  _app.TableDataByTableName(tablename,type, keyword,keyvalue, page,limit); 
    }

    [HttpGet]
    [AllowAnonymous]
    public IActionResult TempeleteByTableName(string tablename,int type)
    {
        var res = new Response();
        if (type ==0)
        {
            var excelRes = _app.TempeleteByTableName(tablename,type,"","");
            if (excelRes.Code == 200)
            {
                return File(excelRes.Result.ToArray(),
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            }
            else
            {
                res.Code = excelRes.Code;
                res.Message = "导出失败";
            }
        }
        if (type==1)
        {
            try
            {
                string shpFilePath = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.shp");
                string shpFilePathzip = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
                _app.TempeleteByTableName(tablename,type, shpFilePath, shpFilePathzip);
                byte[] fileBytes = System.IO.File.ReadAllBytes(shpFilePathzip);
                return File(fileBytes, "application/octet-stream", $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }
      

        return Ok(res);
    }


    [HttpPost]
    [AllowAnonymous]
    [RequestSizeLimit(214748364800)]
    public async Task<Response<bool>> UploadExcelAll(string tableName, IFormFile file)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            response.Result = await _app.UploadExcel1(tableName, file);
            return response;
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
            return response;
        }
      
    }


    [HttpPost]
    [AllowAnonymous]
    [RequestSizeLimit(214748364800)]
    public async Task<Response<Dictionary<string, object>>> UploadExcelInsert(string tableName, IFormFile file)
    {
     
       
            return await _app.UploadExcel2(tableName, file);
       
    }



    [HttpPost]
    [AllowAnonymous]
    public Response<bool> UpdateTableData(UpdateTableReq req)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            response.Result = _app.UpdateTableData(req);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    [HttpPost]
    [AllowAnonymous]
    public Response<bool> AddTableData(UpdateTableReq req)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            response.Result = _app.AddTableData(req);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    [HttpPost]
    [AllowAnonymous]
    [RequestSizeLimit(214748364800)]
    public async Task<Response<Dictionary<string, object>>> AddUploadExcel(string tableName, IFormFile file)
    {
        UploadExcelReq req = new UploadExcelReq { tableName = tableName, file = file };


        return await _app.AddUploadExcel(req);


    }

    [HttpGet]
    [AllowAnonymous]
    public async Task<Response<dynamic>> GetSldFilePath(string tablename) {
        Response<dynamic> response = new Response<dynamic>();
  
        try
        {
            var result = await _app.GetSldFilePath( tablename);
            return result;
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
            response.Result = null;
        }
        return response;
    }

    [HttpGet]
    [AllowAnonymous]
    public async Task<Response<bool>> UploadSldStyle(string tablename,string styleName, string filepath,string id)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            var result = await _app.uploadSldStyle(styleName,tablename, filepath,id);
            return result;
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
            response.Result = false;
        }
        return response;
    }
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    [RequestSizeLimit(214748364800)]
    public async  Task<Response<Dictionary<string, object>>> UploadShape1(string zipFilePath, string tableName)
    {
        return await _app.UploadShape1(zipFilePath,tableName);
    }
    [HttpPost]
    [AllowAnonymous]
    public async Task<Response<Dictionary<string, object>>> UploadShape2(string zipFilePath, string tableName)
    {
        return await _app.UploadShape2(zipFilePath, tableName);
    }
    [HttpPost]
    [AllowAnonymous]
    public Response<bool> UpdateTableOriginalData(UpdateTableReq req)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            response.Result = _app.UpdateTableData1(req);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

}