package cn.iocoder.yudao.module.system.controller.admin.positionmap;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.dto.PositionMapDTO;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapDO;
import cn.iocoder.yudao.module.system.service.positionmap.PositionMapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 仓库点位地图")
@RestController
@RequestMapping("/system/position-map")
@Validated
public class PositionMapController {

    @Resource
    private PositionMapService positionMapService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库点位地图")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:position-map:delete')")
    public CommonResult<Boolean> deletePositionMap(@RequestParam("id") Long id) {
        positionMapService.deletePositionMap(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库点位地图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:position-map:query')")
    public CommonResult<PositionMapRespVO> getPositionMap(@RequestParam("id") Long id) {
        PositionMapDO positionMap = positionMapService.getPositionMap(id);
        return success(BeanUtils.toBean(positionMap, PositionMapRespVO.class));
    }


    @GetMapping("/emergencyStopOrRecovery")
    @Operation(summary = "一键急停or一键恢复地图上所有AGV")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @Parameter(name = "type", description = "类型(1:停止， 0:恢复)", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:position-map:emergencyStopOrRecovery')")
    public CommonResult<Boolean> emergencyStopOrRecovery(@RequestParam("id") Long id, @RequestParam("type") Integer type) {
        positionMapService.emergencyStopOrRecovery(id,type,false);
        return success(true);
    }

    @GetMapping("/getMap")
    @Operation(summary = "获得仓库点位地图Map")
    @PreAuthorize("@ss.hasPermission('system:position-map:query')")
    public CommonResult<Map<Integer, List<PositionMapDTO>>> getPositionMapList() {
        Map<Integer, List<PositionMapDTO>> result = positionMapService.getPositionMapList();
        /*List<PositionMapDO> list = positionMapService.list();
        Map<Integer, List<PositionMapDO>> map = list.stream().collect(Collectors.groupingBy(PositionMapDO::getFloor));*/
        return success(result);
    }

    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    @PermitAll
    public CommonResult<Boolean> uploadFiles(@RequestParam("files") MultipartFile[] files) {
        // todo aiKai - 这里需要和设备定义好 楼层和区域到底是通过文件名称给 - 还是通过yml内容中给 目前按照以yml给的来写
        positionMapService.uploadFiles(files);
        return CommonResult.success(true);
    }

    @GetMapping("/downloadPngBase64")
    @Operation(summary = "下载png文件")
    @PermitAll
    public CommonResult<String> downloadPngBase64(@RequestParam Integer floor, @RequestParam String area) {
        String base64 = positionMapService.downloadPngBase64(floor, area);
        return CommonResult.success(base64);
    }

    @GetMapping("/download")
    @Operation(summary = "下载文件")
    @PermitAll
    public void download(@RequestParam Integer floor, @RequestParam String area, HttpServletResponse response) throws IOException {
        positionMapService.downloadFile(floor, area, response);
    }

    @GetMapping("/agvDownload")
    @Operation(summary = "AGV下载文件")
    @PermitAll
    public void downloadFile(HttpServletResponse response) throws IOException {
        positionMapService.downloadFile(response);
    }

}
