package com.ho.business.controller;

import com.ho.business.entity.DynamicConfigTitle;
import com.ho.business.entity.PointPolysemyConfig;
import com.ho.business.service.DynamicConfigService;
import com.ho.business.vo.req.dynamicConfig.*;
import com.ho.business.vo.resp.DeviceRespVO;
import com.ho.business.vo.resp.dynamicConfig.CurveConfigQueryResp;
import com.ho.business.vo.resp.dynamicConfig.PointConfigDataResp;
import com.ho.business.vo.resp.dynamicConfig.PointConfigResultResp;
import com.ho.common.tools.annotation.HzPermission;
import com.ho.common.tools.annotation.NotSportEnOperation;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.PermissionConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(ContextConstant.BUSINESS + "dynamicConfig")
@Api(tags = "动态配置")
public class DynamicConfigController {

    @Autowired
    DynamicConfigService dynamicConfigService;

    @PostMapping("addCurveList")
    @ApiOperation(value = "批量增加曲线配置信息")
    @HzPermission(PermissionConstant.CURVE_BATCH_CONFIGURATION)
    @NotSportEnOperation()
    public DataResult addCurve(@RequestBody List<CurveConfigAdd> vo) {
        if (vo.isEmpty()) {
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        //辅助线不需要绑定点过滤
        List<CurveConfigAdd> otherList = vo.stream().filter(i -> !(i.getCurveType() == null || CommonConstant.ONE.equals(i.getCurveType()))).collect(Collectors.toList());
        for (CurveConfigAdd c : otherList) {
            if (c.getList() == null || c.getList().isEmpty()) {
                throw new BusinessException(BaseResponseCode.POINT_NOT_NULL);
            }
            //普通线
            if (CommonConstant.ZERO.equals(c.getCurveType()) && c.getList().size() != 1) {
                throw new BusinessException(BaseResponseCode.CURVE_DATA_ERROR);
            }
        }
        int result = dynamicConfigService.addCurveList(vo);
        return DataResult.success(result);
    }

    @PostMapping("queryCurveConfig")
    @ApiOperation(value = "查询曲线配置信息")
    public DataResult<List<CurveConfigQueryResp>> queryCurveConfig(@RequestBody DynamicConfigQuery vo, HttpServletRequest request) {
        vo.setLang(request.getHeader(CommonConstant.LANG));
        List<CurveConfigQueryResp> result = dynamicConfigService.queryCurveConfig(vo);
        return DataResult.success(result);
    }

    @PostMapping("addPointList")
    @ApiOperation(value = "增加点配置信息")
    @HzPermission(PermissionConstant.ADDPOINTLIST)
    @NotSportEnOperation()
    public DataResult addPointList(@RequestBody List<PointConfigAdd> list) {
        if (list.isEmpty()) {
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        //除了自算尖峰平谷（4）不需要绑定点、自定义名称（10），其余都要校验绑定点位
        List<PointConfigAdd> sevenList = list.stream().filter(i -> !(i.getPointType() == null || CommonConstant.FOUR.equals(i.getPointType()) || CommonConstant.TEN.equals(i.getPointType()))).collect(Collectors.toList());
        for (PointConfigAdd p : sevenList) {
            if (p.getList() == null || p.getList().isEmpty()) {
                throw new BusinessException(BaseResponseCode.POINT_NOT_NULL);
            }
            if (p.getList().size() != 1) {
                if (CommonConstant.ZERO.equals(p.getPointType()) || CommonConstant.TWO.equals(p.getPointType()) || CommonConstant.THREE.equals(p.getPointType())) {
                    throw new BusinessException(BaseResponseCode.COMMON_POINT_DATA_ERROR);
                }
            }
            if (CommonConstant.THREE.equals(p.getPointType()) && (p.getDisassemble() == null || p.getDisassemble().getValue() == null)) {
                throw new BusinessException(BaseResponseCode.POINT_DATA_ERROR);
            }
            if (CommonConstant.TWO.equals(p.getPointType())) {
                List<PointPolysemyConfig> polysemyList = p.getPolysemyList();
                if (polysemyList == null || polysemyList.isEmpty()) {
                    throw new BusinessException(BaseResponseCode.POINT_DATA_ERROR_MORE);
                } else {
                    for (PointPolysemyConfig m : polysemyList) {
                        if (m.getName() == null || m.getValue() == null) {
                            throw new BusinessException(BaseResponseCode.POINT_DATA_ERROR_MORE);
                        }
                    }
                }
            }
            if (CommonConstant.ELEVEN.equals(p.getPointType())) {
                List<PointPolysemyConfig> polysemyList = p.getPolysemyList();
                if (polysemyList == null || polysemyList.isEmpty()) {
                    throw new BusinessException(BaseResponseCode.POINT_DATA_ERROR_MORE);
                } else {
                    for (PointPolysemyConfig m : polysemyList) {
                        if (m.getName() == null || m.getValue() == null || m.getSymbol() == null) {
                            throw new BusinessException(BaseResponseCode.POINT_DATA_ERROR_MORE);
                        }
                    }
                }
            }
        }
        int result = dynamicConfigService.addPointList(list);
        return DataResult.success(result);
    }

    @PostMapping("pointList")
    @ApiOperation(value = "查询配置信息")
    public DataResult<List<PointConfigResultResp>> pointList(@RequestBody PointConfigQuery vo, HttpServletRequest request) {
        vo.setLang(request.getHeader(CommonConstant.LANG));
        List<PointConfigResultResp> resultRespList = dynamicConfigService.pointList(vo);
        return DataResult.success(resultRespList);
    }

    @PostMapping("pointListData")
    @ApiOperation(value = "配置点数据集合")
    public DataResult<List<PointConfigDataResp>> pointListData(@RequestBody PointConfigQuery vo, HttpServletRequest request) {
        vo.setLang(request.getHeader(CommonConstant.LANG));
        List<PointConfigDataResp> pointConfigData = dynamicConfigService.getPointConfigData(vo);
        return DataResult.success(pointConfigData);
    }

    @PostMapping("syncDynamicConfig")
    @ApiOperation(value = "同步动态配置")
    @HzPermission(PermissionConstant.SYNCDYNAMICCONFIG)
    public DataResult syncDynamicConfig(@RequestBody SyncDynamicConfigParam vo) {
        dynamicConfigService.syncDynamicConfig(vo);
        return DataResult.success();
    }

    @PostMapping("dynamicConfigDevice")
    @ApiOperation(value = "查询配置页面设备")
    public DataResult<List<DeviceRespVO>> dynamicConfigDevice(@RequestBody SyncDynamicConfigDeviceQuery vo) {
        List<DeviceRespVO> deviceRespVOList = dynamicConfigService.dynamicConfigDevice(vo);
        return DataResult.success(deviceRespVOList);
    }

    @PostMapping("addDynamicConfigTitle")
    @ApiOperation(value = "动态配置标题")
    public DataResult addDynamicConfigTitle(@RequestBody DynamicConfigTitle vo) {
        String name = vo.getName();
        if (name != null && name.length() > CommonConstant.FIFTY) {
            throw new BusinessException(BaseResponseCode.PARAM_EXCEED_LENGTH.getCode(), BaseResponseCode.PARAM_EXCEED_LENGTH.getMsg() + CommonConstant.FIFTY);
        }
        int count = dynamicConfigService.addDynamicConfigTitle(vo);
        return DataResult.success(count);
    }

    @PostMapping("queryDynamicConfigTitle")
    @ApiOperation(value = "查询动态配置标题")
    public DataResult<List<DynamicConfigTitle>> queryDynamicConfigTitle(@RequestBody DynamicConfigTitle vo) {
        List<DynamicConfigTitle> dynamicConfigTitles = dynamicConfigService.queryDynamicConfigTitle(vo);
        return DataResult.success(dynamicConfigTitles);
    }

    @PostMapping("delDynamicConfigTitle")
    @ApiOperation(value = "删除动态配置标题")
    @HzPermission(PermissionConstant.DELDYNAMICCONFIGTITLE)
    public DataResult delDynamicConfigTitle(@RequestBody DynamicConfigTitle vo) {
        int count = dynamicConfigService.delDynamicConfigTitle(vo);
        return DataResult.success(count);
    }
}
