package com.pqs9200.controller.advanced;

import com.njcn.enums.ResultStatusEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.sagDomain.DipZoneModel;
import com.pqs9200.pojo.data.*;
import com.pqs9200.service.advanced.SvgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年12月06日 13:41
 */
@RestController
@RequestMapping("/svg")
public class SvgController {

    private static final Logger logger = LoggerFactory.getLogger(SvgController.class);

    @Autowired
    private SvgService svgService;


    /**
     * 保存svg信息
     * @param fileName .
     * @param xml .
     * @param svg .
     * @return .
     */
    @PostMapping("/save")
    public HttpResult save(String fileName, String xml, String svg) {
        HttpResult result;
        try {
            String saveResult = svgService.saveSvg(fileName, xml, svg);
            if (saveResult.equalsIgnoreCase(ResultStatusEnum.SUCCESS.getMessage())) {
                result = PubUtils.assignmentResult(null,200,ResultStatusEnum.SUCCESS.getMessage());
            } else {
                result = PubUtils.assignmentResult(null,500,ResultStatusEnum.SUCCESS.getMessage());
            }
        } catch (Exception e) {
            logger.error("保存svg信息失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }


    /**
     * 获取svg数据
     * @param index 所选svg的uuid
     * @param type svg后缀
     */
    @PostMapping("/loadSvg")
    public HttpResult loadSvg(String index,String type) {
        HttpResult result;
        try {
            String svgXml = svgService.loadSvg(index,type);
            if (svgXml.equalsIgnoreCase(ResultStatusEnum.NO_SVG_FILE.getMessage())) {
                result = PubUtils.assignmentResult(null,500,ResultStatusEnum.FAIL.getMessage());
            } else {
                result =PubUtils.assignmentResult(svgXml,200, ResultStatusEnum.SUCCESS.getMessage());
            }
        } catch (Exception e) {
            logger.error("获取svg信息失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,ResultStatusEnum.FAIL.getMessage());
        }
        return result;
    }


    /**
     * create by: xu yang
     * description: 存储图元信息
     * create time: 2021/12/13 10:59
     */
    @PostMapping("/saveCell")
    public HttpResult saveCell(@RequestBody SvgCells svgCells) {
        HttpResult result;
        try {
            svgService.saveCell(svgCells);
            result = PubUtils.assignmentResult("成功",200,ResultStatusEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            logger.error("保存图元信息进数据库失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }

    /**
     * create by: xu yang
     * description: 查询图元信息
     * create time: 2021/12/13 10:59
     */
    @PostMapping("/getCell")
    public HttpResult getCell(Integer cellIndex) {
        HttpResult result;
        SvgCells svgCells = svgService.getCell(cellIndex);
        result = PubUtils.assignmentResult(svgCells,200,ResultStatusEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * create by: xu yang
     * description: 存储所有图元信息
     * create time: 2021/12/14 9:00
     */
    @PostMapping("/saveAllCells")
    public HttpResult saveAllCells(@RequestBody SvgAllCells svgAllCells) {
        HttpResult result;
        try {
            svgService.saveAllCells(svgAllCells);
            result = PubUtils.assignmentResult("成功",200,ResultStatusEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            logger.error("存储所有图元信息失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }

    /**
     * create by: xu yang
     * description: 查询所有图元信息
     * create time: 2021/12/20 10:22
     */
    @PostMapping("/getAllCells")
    public HttpResult getAllCells() {
        HttpResult result;
        List<SvgCells> list = svgService.getAllCells();
        result = PubUtils.assignmentResult(list,200,ResultStatusEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * create by: yexb
     * description: 暂降域算法
     * create time: 2021/12/20 10:22
     */
    @PostMapping("/getSagDomain")
    public HttpResult getSagDomain(@RequestBody SvgSagDomain svgSagDomain) {
        System.out.println(svgSagDomain.getLines_substation());
        HttpResult result;
        DipZoneModel[] Usag_area = svgService.getSagDomain(svgSagDomain);
        result = PubUtils.assignmentResult(Usag_area,200,ResultStatusEnum.SUCCESS.getMessage());
        return result;
    }


    /**
     * 功能描述: 保存暂降敏感点发生事件
     * TODO
     *
     * @param svgSensitivePoint
     * @return com.njcn.pojo.commons.HttpResult
     * @author xy
     * @date 2021/12/22 21:01
     */
    @PostMapping("/saveSensitivePoint")
    public HttpResult saveSensitivePoint(@RequestBody SvgSensitivePoint svgSensitivePoint) {
        HttpResult result;
        try {
            svgService.saveSensitivePoint(svgSensitivePoint);
            result = PubUtils.assignmentResult("成功",200,ResultStatusEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            logger.error("保存暂降敏感点发生事件失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }


    /**
     * 功能描述:
     * TODO
     *
     * @param
     * @return com.njcn.pojo.commons.HttpResult
     * @author xy
     * @date 2021/12/23 14:05
     */
    @PostMapping("/getAllCalucateInfo")
    public HttpResult getAllCalucateInfo() {
        HttpResult result;
        try {
            List<SvgCalucateInfo> list = svgService.getAllCalucateInfo();
            result = PubUtils.assignmentResult(list,200,ResultStatusEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            logger.error("获取所有暂降敏感点事件失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }



    @PostMapping("/deleteCalucateInfo")
    public HttpResult deleteCalucateInfo(String index) {
        HttpResult result;
        try {
            svgService.deleteCalucateInfo(index);
            result = PubUtils.assignmentResult("成功",200,ResultStatusEnum.SUCCESS.getMessage());
        } catch (Exception e) {
            logger.error("删除暂降敏感点事件失败，异常为：{}",e.getMessage());
            result = PubUtils.assignmentResult(null,500,"失败");
        }
        return result;
    }

}
