package com.brilliance.isc.common.oit.bussiness;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.annotation.OitAno;
import com.brilliance.isc.bo.Oit;
import com.brilliance.isc.common.cache.pty.PtyCacheService;
import com.brilliance.isc.common.oit.vo.NoticeVo;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.isc.vo.WarnVo;
import com.brilliance.isc.warn.WarnService;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class AbstractOit {

    @Autowired
    private WarnService warnService;
    public static final Logger logger = LoggerFactory.getLogger(AbstractOit.class);

    public void entyOit(NoticeVo noticeVo) {

        for (Method method : this.getClass().getDeclaredMethods()) {
            OitAno oitAno = method.getAnnotation(OitAno.class);
            if (oitAno != null && oitAno.objtype().equals(noticeVo.getObjtyp())) {

                try {
                    method.invoke(this, new Object[]{noticeVo});
                } catch (IllegalAccessException e) {
                    logger.error("oit注册类不合法{}: {}", this.getClass().getSimpleName(), e.getCause().getMessage());
                } catch (InvocationTargetException e) {
                    logger.error("注册类内部方法执行异常{}:{}", this.getClass().getSimpleName(), e.getCause().getMessage());
                }
                //设置提示warning颜色级别
                buildWarning(noticeVo);
            }
        }
    }

    void buildWarning(NoticeVo noticeVo) {
        if(StringUtils.isEmpty(noticeVo.getLev())){
            warnService.buildwarning(WarnVo.LEVEL_TIPS,noticeVo.getPath(), "", WarnVo.SOURCE_BACK, "");
            return;
        }
        switch (noticeVo.getLev()) {
            case "!":
                //绿色
                warnService.buildwarning(WarnVo.LEVEL_TIPS,noticeVo.getPath(), "", WarnVo.SOURCE_BACK, noticeVo.getMessage());
                break;
            case "$":
            case "0":
                warnService.buildwarning(WarnVo.LEVEL_GENERAL,noticeVo.getPath(), "", WarnVo.SOURCE_BACK, noticeVo.getMessage());
                break;
            case "1":
            case "2":
            case "3":
            case "9":
                //红色
                warnService.buildwarning(WarnVo.LEVEL_IMPORTANT,noticeVo.getPath(), "", WarnVo.SOURCE_BACK, noticeVo.getMessage());
                break;
            default:
                logger.info("oit未知消息{}", noticeVo.getMessage());
                break;
        }

    }

    public void selectHeighLevOit(List<Oit> oits, NoticeVo noticeVo) {
        for (Oit oit : oits) {
            oit.setVer(swichLev(oit.getInflev()));
        }
        oits.sort((a, b) -> a.getVer().compareTo(b.getVer()));
        Oit oit = oits.get(oits.size() - 1);
        noticeVo.setMessage(String.format("消息及停用提示:%s,提示级别:%s,提示主体:%s", oit.getInftxt(),getTextByCode(oit.getInflev()),noticeVo.getExtkey()));
        noticeVo.setLev(oit.getInflev());

    }

    String swichLev(String lev) {
        switch (lev) {
            case "!":
                return "1";
            case "$":
                return "2";
            case "0":
                return "3";
            case "1":
                return "4";
            case "2":
                return "5";
            case "3":
                return "6";
            case "9":
                return "7";

        }
        return "0";
    }
    String getTextByCode(String code){
        switch (code) {
            case "!":
                return "不展示";
            case "$":
                return "展示";
            case "0":
                return "警告";
            case "1":
                return "新风险停止";
            case "2":
                return "支付停止";
            case "3":
                return "交易停止";
            case "9":
                return "处理停止";

        }
        return "未知提示";
    }
}
