package com.multictrl.modules.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.AssertUtils;
import com.multictrl.modules.business.dto.GeoPhotoDTO;
import com.multictrl.modules.business.service.GeoPhotoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 地理照片模型
 *
 * @author Sdy
 * @since 1.0.0 2026-06-05
 */
@RestController
@RequestMapping("business/geophoto")
@Tag(name = "地理模型")
@ApiOrder(13)
@RequiredArgsConstructor
public class GeoPhotoController {
    private final GeoPhotoService geoPhotoService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "type", description = "类型 1正射 2倾斜")
    })
    @RequiresPermissions("bus:geophoto:page")
    public Result<PageData<GeoPhotoDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<GeoPhotoDTO> page = geoPhotoService.pageList(params);

        return new Result<PageData<GeoPhotoDTO>>().ok(page);
    }

    @PostMapping
    @Operation(summary = "上传地理资源")
    @LogOperation("上传地理资源")
    @RequiresPermissions("bus:geophoto:upload")
    public Result<Object> upload(@Parameter(description = "模型类型{1:正射，2:倾斜}")
                                 @RequestParam("type") String type,
                                 @RequestParam("file") MultipartFile file) {
        String suffix = FileUtil.getSuffix(file.getOriginalFilename());
        Map<String, String> typeSuffixMap = Map.of("1", "tif", "2", "zip");
        String expectedSuffix = typeSuffixMap.get(type);
        if (expectedSuffix == null) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR, "模型类型");
        }
        if (!expectedSuffix.equals(suffix)) {
            return new Result<>().error(ErrorCode.FILE_FORMAT_ERROR, expectedSuffix);
        }

        geoPhotoService.upload(type, file);
        return new Result<>();
    }

    @GetMapping
    @Operation(summary = "获取地理资源")
    public Result<List<GeoPhotoDTO>> get(@Parameter(description = "类型{1:正射，2:倾斜}")
                                         @RequestParam("type") String type) {
        List<GeoPhotoDTO> layerInfo = geoPhotoService.getGeoList(type);

        return new Result<List<GeoPhotoDTO>>().ok(layerInfo);
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("bus:geophoto:delete")
    public Result<Object> delete(@RequestParam Long id) {
        geoPhotoService.remove(id);

        return new Result<>();
    }

    @GetMapping("/getDiskModel")
    @Operation(summary = "获取磁盘模型")
    @LogOperation("获取磁盘模型")
    @RequiresPermissions("bus:geophoto:getDiskModel")
    public Result<List<GeoPhotoDTO>> getDiskModel() {
        List<GeoPhotoDTO> diskData = geoPhotoService.getDiskMap();

        return new Result<List<GeoPhotoDTO>>().ok(diskData);
    }

    @PostMapping("/uploadModelDisk")
    @Operation(summary = "磁盘上传模型")
    @LogOperation("磁盘上传模型")
    @RequiresPermissions("bus:geophoto:uploadModelDisk")
    public Result<List<GeoPhotoDTO>> uploadModelDisk(@RequestBody List<String> names) {
        AssertUtils.isListEmpty(names, "模型名称");
        geoPhotoService.uploadModelDisk(names);

        return new Result<>();
    }
}
