package com.xydl.cac.controller;

import com.xydl.cac.entity.IcdConfigTypeInst;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.entity.IcdTransform;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.iec.IecServerService;
import com.xydl.cac.model.Response;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.service.IcdTransformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@Api(tags = {"Iec服务端接口"})
@RequestMapping("iecserver")
@Slf4j
public class IecServerController extends BasicController {

    @Resource
    IcdFileConfigService configService;
    @Resource
    IecServerService iecServerService;
    @Resource
    IcdTransformService transformService;

    @PostMapping("upload")
    @ApiOperation("上传服务端icd文件")
    public Response<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file != null && file.getInputStream() != null) {
            String filename = file.getOriginalFilename();
            String content = IOUtils.toString(file.getInputStream(), Charset.forName("UTF-8"));
            configService.upload(content, filename, 1);
            return Response.success("OK");
        } else {
            return Response.fail("缺少上传文件");
        }
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public Response<String> delete(Integer fileId) throws Exception {
        if (fileId == null) {
            throw new BusinessException("fileId不能为空!");
        }
        configService.deleteSrv(fileId);
        return Response.success("OK");
    }

    @GetMapping("listFile")
    @ApiOperation("查询服务端icd文件列表")
    public Response<List<IcdFile>> listFile() throws Exception {
        List<IcdFile> result = configService.listFile(Constants.Server);
        for (IcdFile file : result) {
            file.setXml(null);
        }
        return Response.success(result);
    }

    @PostMapping("start")
    @ApiOperation("启动IEC服务端")
    public Response<String> start(Integer fileId, Integer port) throws Exception {
        if (fileId == null) {
            throw new BusinessException("fileId不能为空!");
        }
        if (port == null) {
            throw new BusinessException("port不能为空!");
        }
        iecServerService.startServer(fileId, port);
        return Response.success("OK");
    }

    @PostMapping("stop")
    @ApiOperation("停止IEC服务端")
    public Response<String> stop(Integer fileId) throws Exception {
        if (fileId == null) {
            throw new BusinessException("fileId不能为空!");
        }
        iecServerService.stopServer(fileId);
        return Response.success("OK");
    }

    @GetMapping("listParamindex")
    @ApiOperation("查询该IED下的paramindex数据")
    public Response<List<IcdConfigTypeInst>> listParamindex(@Validated @NotNull(message = "fileId不能为空!") Integer fileId) throws Exception {
        List<IcdConfigTypeInst> result = iecServerService.listParamindex(fileId);
        return Response.success(result);
    }

    @PostMapping("addTransform")
    @ApiOperation("addTransform")
    public Response<IcdTransform> addTransform(@Validated @RequestBody IcdTransform icdTransform) throws Exception {
        IcdTransform result = transformService.addTransform(icdTransform);
        return Response.success(result);
    }

    @PostMapping("delTransform")
    @ApiOperation("delTransform")
    public Response<String> delTransform(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
        if (id == null) {
            throw new BusinessException("id不能为空!");
        }
        transformService.deleteTransform(id);
        return Response.success("OK");
    }

    @PostMapping("clearTransform")
    @ApiOperation("清空")
    public Response<String> clearTransform() {
        transformService.clearTransform();
        return Response.success("OK");
    }

    @GetMapping("listTransform")
    @ApiOperation("listTransform")
    public Response<List<IcdTransform>> listTransform() throws Exception {
        List<IcdTransform> result = transformService.listTransform();
        for (IcdTransform item : result) {
            if (StaticVariable.rptFromActiveMap.containsKey(item.getRptFrom())) {
                item.setFromActive(true);
            } else {
                item.setFromActive(false);
            }
            if (StaticVariable.rptToActiveMap.containsKey(item.getRptTo())) {
                item.setToActive(true);
            } else {
                item.setToActive(false);
            }
        }
        return Response.success(result);
    }

}
