package com.jay.monitor.data.server.controllers;//package com.jay.monitor.data.server.controllers;

import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.core.utils.JsonHelper;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.componts.html.DefaultDataRender;
import com.jay.monitor.data.server.controllers.crud.BaseSimpleCrudController;
import com.jay.monitor.data.server.enums.AlertMsgEnums;
import com.jay.monitor.data.server.enums.MatchRuleEnums;
import com.jay.monitor.data.server.models.request.alert.AlertRuleRequest;
import com.jay.monitor.data.server.models.store.AlertPlatformConfig;
import com.jay.monitor.data.server.models.store.AlertRuleConfig;
import com.jay.monitor.data.server.report.alert.AlertDataService;
import com.jay.monitor.data.server.store.IAlertPlatformProcess;
import com.jay.monitor.data.server.store.IAlertRuleProcess;
import com.jay.monitor.data.server.store.ISimpleCrudProcess;
import com.jay.monitor.data.server.utils.HtmlComponentUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//import com.jay.monitor.data.server.services.impl.DefaultBackstageRouteService;

/**
 * 后台路由管理
 *
 * @author liukx
 * @Date 2019-02-28 15:16
 */
@Controller
@RequestMapping(value = "/alert/rule")
public class AlertRuleController extends BaseSimpleCrudController<AlertRuleRequest, AlertRuleConfig> {

    ///////////////////////////////////////////// API ////////////////////////////////////////////
    public final static String PREFIX_URL = "/alert/rule";
    public final static String LIST_URL = PREFIX_URL + "/pageList";
    public final static String ADD_URL = PREFIX_URL + "/add";
    public final static String UPDATE_URL = PREFIX_URL + "/update";
    public final static String DEL_URL = PREFIX_URL + "/del";
    public static final String VIEW_PAGE = "/alert";

    @Autowired
    private IAlertRuleProcess alertRuleProcess;

    @Autowired
    private AlertDataService alertDataService;

    @Autowired
    private IAlertPlatformProcess alertPlatformProcess;

    @Override
    public ISimpleCrudProcess getCrudProcess() {
        return alertRuleProcess;
    }

    @Override
    public String getViewPath() {
        return VIEW_PAGE;
    }

    @Override
    protected String getAddHtmlPath() {
        return "/update";
    }

    @Override
    protected void addInModel(Model model, AlertRuleConfig entity) {
        model.addAttribute("apiService", ADD_URL);
        AlertRuleConfig config = new AlertRuleConfig();
        config.setStatus(1);
        config.setRuleType(MatchRuleEnums.expression);
        super.addInModel(model, config);
    }

    @Override
    protected void updateInModel(Model model, AlertRuleConfig entity) {
        model.addAttribute("apiService", UPDATE_URL);
        super.updateInModel(model, entity);
    }

    @Override
    @RequestMapping(value = "/pageList",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageResponseModel<AlertRuleConfig> pageList(
        @RequestBody
            AlertRuleRequest request) throws Exception {
        AlertRuleConfig config = new AlertRuleConfig();
        BeanUtils.copyProperties(request, config);
        // 将参数对应的全部匹配赋值
        PageResponseModel pageResponseModel =
            alertRuleProcess.searchDataByList(request.getPageNo(), request.getPageSize(), config);
        return pageResponseModel;
    }

    @Override
    protected void addBefore(AlertRuleConfig req) {
        checkExpression(req);
    }

    @Override
    protected void addAfter(AlertRuleConfig req, boolean result) {
        refreshId(req.getId());
    }

    @Override
    protected void updateBefore(AlertRuleConfig req) {
        checkExpression(req);
    }

    @Override
    protected void modelAfter(Model model, AlertRuleConfig entity) {
        AlertPlatformConfig config = new AlertPlatformConfig();
        config.setStatus(1);
        List<AlertPlatformConfig> alertPlatformConfigs = alertPlatformProcess.queryList(config);
        List<DefaultDataRender> dataRenders = HtmlComponentUtils
            .convertDataRender(alertPlatformConfigs, AlertPlatformConfig::getId,
                var -> var.getPlatformType() + " - " + var.getAlertName());
        model.addAttribute("alertPlatformConfigList", JsonHelper.toJsonString(dataRenders));
    }

    @Override
    protected void listInModel(Model model, AlertRuleConfig entity) {
        List<DefaultDataRender> alertPlatformList = HtmlComponentUtils.convertDataRender(AlertMsgEnums.class);
        model.addAttribute("alertPlatformTypeList", JsonHelper.toJsonString(alertPlatformList));
        modelAfter(model, entity);
    }

    @Override
    protected void deleteAfter(AlertRuleConfig req, boolean result) {
        refreshId(req.getId());
    }

    @Override
    protected void updateAfter(AlertRuleConfig req, boolean result) {
        refreshId(req.getId());
    }

    private void refreshId(String id) {
        alertDataService.refreshRuleId(id);
    }

    private void checkExpression(AlertRuleConfig req) {
        ResponseUtils.checkNull(req.getRuleName(), "规则名称不能为空!");
        ResponseUtils.checkNull(req.getProject(), "项目名称不能为空!");
        ResponseUtils.checkNull(req.getAlertPlatformList(), "告警平台组不能为空!");
        ResponseUtils.checkNull(req.getRuleNodes(), "告警规则不能为空!");
        //        CommonDataDTO commonDataDTO = new CommonDataDTO(MsgTypeEnums.MQ);
        //        MatchRuleEnums ruleType = req.getRuleType();
        //        if (MatchRuleEnums.expression == ruleType) {
        //            try {
        //                boolean match = expressionValueCondition.match(commonDataDTO, req.getFieldValue());
        //            } catch (SpelParseException e) {
        //                ResponseUtils.checkNull(null, "expression表达式解析有误!");
        //            }
        //        }
    }
}
