package com.boco.nbd.wios.flow.enums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.boco.nbd.framework.workflow.entity.WorkTaskInfo;
import com.boco.nbd.wios.flow.entity.bo.FlowChartBO;
import com.boco.nbd.wios.flow.entity.qo.FlowChartQo;
import com.boco.nbd.wios.flow.entity.vo.FlowChartVO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author:cao hai
 * @date:2022/8/2 15:32
 * @version:V1.0
 * @description:RgbEnum
 * @modify:
 */
@Slf4j
public enum RgbEnum {


    /**
     * 浅绿色 rgb(0, 176, 80)=>#00B050
     * 黄色 rgb(252, 153, 8)=>#FC9908
     * 红色 rgb(192, 0, 0)=>#C00000
     * 深绿色 rgb(128, 176, 200)=>#80B0C8,
     * 0,100,0=>#006400
     * 灰色 rgb(191,191,191)=>#BFBFC0
     * 黑色 rgb(34, 42, 53)=>#222A35
     *
     * @param key
     */
    LIGHT_GREEN("#00B050"), YELLOW("#FC9908"),

    RED("#C00000"),

    DARK_GREEN("#006400"), GREY("#BFBFC0"),

    BLACK("#222A35");

    RgbEnum(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    /**
     * 忽略节点
     */
    private static final Integer[] IGNORE_NODE = {-1, 0, 199};

    /**
     * 枚举
     */
    private String key;

    /**
     * 生成流程图数据
     *
     * @param taskInfo
     * @param closeNode
     * @param tracks
     * @return
     */
    public static FlowChartVO simpleFlowChart(FlowChartQo taskInfo, Integer closeNode, List<String> tracks) {
        List<FlowChartBO> tab = new ArrayList<>(10);
        FlowChartBO priorNode = null;
        for (String track : tracks) {
            FlowNodeEnum item = FlowNodeEnum.getEnum(Convert.toInt(track));
            if (null == item) {
                log.info("nodeFlag={} is invalid data.", track);
                continue;
            }
            if (Arrays.asList(IGNORE_NODE).contains(item.getKey())) {
                continue;
            }
            RgbEnum rgbEnum = (closeNode != null && closeNode.equals(item.getKey())) ? BLACK : DARK_GREEN;
            FlowChartBO chart = new FlowChartBO(item, 1, rgbEnum.key);
            if (priorNode != null) {
                priorNode.setNext(item.getKey() + "");
                tab.add(priorNode);
            }
            priorNode = chart;
        }
        //添加当前节点
        FlowNodeEnum currentItem = FlowNodeEnum.getEnum(Convert.toInt(taskInfo.getNodeFlag()));
        if (currentItem != null) {
            if (priorNode != null) {
                priorNode.setNext(currentItem.getKey() + "");
                tab.add(priorNode);
            }
            tab.add(new FlowChartBO(currentItem, 0, getOverTimeRgb(taskInfo.getCurrentStartTime(), taskInfo.getDueDate())));
        }
        List<FlowChartBO> cell = FlowChartBO.toCellChart(tab);
        FlowChartVO flowChartVO = new FlowChartVO(tab, cell);
        flowChartVO.setElectricExpand(tracks.contains(FlowNodeEnum.NODE_115.getKey().toString()) ? 1 : 0);
        return flowChartVO;
    }

    /**
     * 获取超时对应rgb
     * 浅绿色:当前工单处于此节点(步骤)且未到超期预警,考核期内前80%的时间
     * 黄色:当前工单处于此节点(步骤),考核期内前20%的时间,渲染为黄色,
     * 红色:当前工单处于此节点(步骤)且当前时间大于等于此节点(步骤)的处理时限,渲染为红色
     * 深绿色:已经完成的节点(步骤)
     * 灰色:  还未开始的节点(步骤)
     * 黑色:  订单在此节点直接关闭(工单未实现闭环,也就是实施过程中被终止)
     *
     * @param startTime
     * @param dueDate
     * @return
     */
    public static String getOverTimeRgb(Date startTime, Date dueDate) {
        if (dueDate == null || startTime == null) {
            return LIGHT_GREEN.key;
        }
        if (DateUtil.compare(dueDate, new Date()) <= 0) {
            return RED.key;
        }
        long space = dueDate.getTime() - startTime.getTime();
        long timeSpace = System.currentTimeMillis() - startTime.getTime();
        double ratio = 0.8;
        if (timeSpace <= space * ratio) {
            return LIGHT_GREEN.key;
        }
        return YELLOW.key;
    }
}