package com.persagy.adm.diagram.controller;

import com.persagy.adm.diagram.core.model.template.DiagramTemplate;
import com.persagy.adm.diagram.frame.EditRequest;
import com.persagy.adm.diagram.manage.Folder;
import com.persagy.adm.diagram.manage.TemplateManager;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@CrossOrigin
@RequestMapping("template")
public class TemplateController {

    @Autowired
    private TemplateManager templateManager;

    @ApiOperation("查询模板列表")
    @GetMapping("getTemplates")
    public CommonResult<List<Folder>> getTemplates(){
        return ResultHelper.single(templateManager.getTemplates());
    }

    @ApiOperation("按系统图类型查询模板列表")
    @GetMapping("getTemplatesByType")
    public CommonResult<List<DiagramTemplate>> getTemplatesByType(@RequestParam String diagramType){
        return ResultHelper.single(templateManager.getTemplates(diagramType));
    }

    @PostMapping("newTemplate")
    public CommonResult<DiagramTemplate> newTemplate(@RequestBody Map<String, String> params){
        return ResultHelper.single(templateManager.createTemplate(params.get("name"), params.get("type")));
    }

    @PostMapping("delTemplate")
    public CommonResult<Boolean> delTemplate(@RequestBody Map<String, String> params){
        return ResultHelper.single(templateManager.deleteTemplate(params.get("id")));
    }

    @PostMapping("editTemplateInfo")
    public CommonResult<Boolean> editTemplateInfo(@RequestBody Map<String, String> params){
        return ResultHelper.single(templateManager.editTemplateInfo(params.remove("id"), params));
    }

    @PostMapping("addCon")
    public CommonResult<DiagramTemplate> addCon(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.addCon(params.getNewCompId(), params.getCurrentCompId(), params.getTemplateId()));
    }

    @PostMapping("delCon")
    public CommonResult<DiagramTemplate> delCon(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.delCon(params.getCurrentCompId(), params.getTemplateId()));
    }

    @PostMapping("moveCon")
    public CommonResult<DiagramTemplate> moveCon(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.moveCon(params.getCurrentCompId(), params.getTemplateId(), params.getMoveOffset()));
    }

    @PostMapping("addMainPipe")
    public CommonResult<DiagramTemplate> addMainPipe(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.addMainPipe(params.getLines(), params.getTemplateId()));
    }

    @PostMapping("delLine")
    public CommonResult<DiagramTemplate> delLine(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.delMainPipe(params.getCurrentCompId(), params.getTemplateId()));
    }

    @PostMapping("modifyCon")
    public CommonResult<DiagramTemplate> modifyCon(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.modifyCon(params.getTemplatePropsData(), params.getCurrentCompId(), params.getTemplateId()));
    }

    @PostMapping("modifyMainPipe")
    public CommonResult<DiagramTemplate> modifyMainPipe(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.modifyMainPipe(params.getTemplatePropsData(), params.getCurrentCompId(), params.getTemplateId()));
    }

    /**
     * 编辑设备过滤条件
     */
    @PostMapping("modifyFilter")
    public CommonResult<DiagramTemplate> modifyFilter(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.modifyFilter(params.getDataFilter(), params.getCurrentCompId(), params.getTemplateId()));
    }

    /**
     * 编辑设备打包配置
     */
    @PostMapping("modifyPack")
    public CommonResult<DiagramTemplate> modifyPack(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.modifyPack(params.getEquipPack(), params.getCurrentCompId(), params.getTemplateId()));
    }

    /**
     * 编辑容器的动态组配置
     */
    @PostMapping("modifyDynGroup")
    public CommonResult<DiagramTemplate> modifyDynGroup(@RequestBody EditRequest params){
        return ResultHelper.single(templateManager.modifyDynGroup(params.getDynGroup(), params.getCurrentCompId(), params.getTemplateId()));
    }

}