package com.xydl.cac.controller;

import com.xydl.cac.comparator.Comparator;
import com.xydl.cac.comparator.FloatCompare;
import com.xydl.cac.comparator.IntCompare;
import com.xydl.cac.comparator.MissCompare;
import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.WarnRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;


@RestController
@Api(tags = {"告警规则相关接口"})
@RequestMapping("rule")
@Slf4j
public class WarnRuleController extends BasicController {
    FloatCompare floatCompare = new FloatCompare();
    IntCompare intCompare = new IntCompare();
    MissCompare missCompare = new MissCompare();

    @Resource
    WarnRuleService service;

    @GetMapping("listComparator")
    @ApiOperation("查询比较器")
    public Response<List<ColumnModel>> listComparator() throws Exception {
        List<ColumnModel> result = new ArrayList<>();
        ColumnModel item = new ColumnModel();
        item.setType(Constants.Float);
        item.setComment("浮点型数据比较器");
        result.add(item);
        item = new ColumnModel();
        item.setType(Constants.Int);
        item.setComment("整型数据比较器");
        result.add(item);
        item = new ColumnModel();
        item.setType(Constants.Miss);
        item.setComment("数据缺失比较器");
        result.add(item);
        return Response.success(result);
    }

    @GetMapping("listOperator")
    @ApiOperation("查询比较符")
    public Response<Map<String, String>> listOperator(String name) throws Exception {
        Comparator comparator = this.getComparator(name);
        return Response.success(comparator.supportedOperator());
    }

    private Comparator getComparator(String name) throws BusinessException {
        if (Constants.Float.equalsIgnoreCase(name)) {
            return floatCompare;
        } else if (Constants.Int.equalsIgnoreCase(name)) {
            return intCompare;
        } else if (Constants.Miss.equalsIgnoreCase(name)) {
            return missCompare;
        } else {
            throw new BusinessException("未找到该比较器" + name);
        }
    }

    @GetMapping("listAll")
    @ApiOperation("查询全部列表")
    public Response<List<WarnRule>> listAll(Integer sensorId) throws Exception {
        List<WarnRule> result = service.listAll(sensorId);
        for (WarnRule item : result) {
            if (Constants.Float.equalsIgnoreCase(item.getComparator())) {
                item.setComparatorDesc("浮点型数据比较器");
                String name = floatCompare.supportedOperator().get(item.getOperator());
                item.setOperatorDesc(name);
            } else if (Constants.Int.equalsIgnoreCase(item.getComparator())) {
                item.setComparatorDesc("整型数据比较器");
                String name = intCompare.supportedOperator().get(item.getOperator());
                item.setOperatorDesc(name);
            } else if (Constants.Miss.equalsIgnoreCase(item.getComparator())) {
                item.setComparatorDesc("数据缺失比较器");
                String name = missCompare.supportedOperator().get(item.getOperator());
                item.setOperatorDesc(name);
            }
        }
        return Response.success(result);
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public Response<WarnRule> add(@Validated @RequestBody WarnRule item) throws Exception {
        Comparator comparator = this.getComparator(item.getComparator());
        comparator.valid(item.getOperator(), item.getThreshold());
        WarnRule result = service.add(item);
        return Response.success(result);
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public Response<String> update(@Validated @RequestBody WarnRule item) throws Exception {
        if (item.getId() == null) {
            throw new BusinessException("ID不能为空!");
        }
        Comparator comparator = this.getComparator(item.getComparator());
        comparator.valid(item.getOperator(), item.getThreshold());
        service.update(item);
        return Response.success("OK");
    }

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

}
