package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxCenterWorkingBoardService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
public class JxCenterWorkingBoardProxyWorkingStatusService implements IJxCenterWorkingBoardService {

    private IJxCenterWorkingBoardService proxyService;

    public JxCenterWorkingBoardProxyWorkingStatusService() {}

    public JxCenterWorkingBoardProxyWorkingStatusService(IJxCenterWorkingBoardService proxyService) {
        this.proxyService = proxyService;
    }

    @Override
    public Map<String, Object> queryCenterWorkingBoardData(Map<String, Object> dataMap) {

        if (null != proxyService) proxyService.queryCenterWorkingBoardData(dataMap);

        String workCenterCode = (String) dataMap.get(MesExtConstWords.WORK_CENTER_CODE);
        String workTime = (String) dataMap.get(MesExtConstWords.WORK_TIME);
        String shiftCode = (String) dataMap.get(MesExtConstWords.SHIFT_CODE);
        String curTime = (String) dataMap.get("curTime");
        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = (List<MesShiftProdCenterRecord>) dataMap.get(MesShiftProdCenterRecord.class.getSimpleName());

        List<MesShiftProdCenterRecord> filterList = (CollectionUtils.isEmpty(shiftProdCenterRecordList) || StringUtils.isEmpty(workTime) || StringUtils.isEmpty(shiftCode)) ? null :
                shiftProdCenterRecordList.stream().filter(o -> (null != o && o.getWorkCenterCode().equals(workCenterCode) && o.getWorkTime().equals(workTime) && o.getShiftCode().equals(shiftCode) &&
                        MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_PROD.getValue() >= o.getProdType() && o.getStartTime().compareTo(curTime) <= 0 && o.getEndTime().compareTo(curTime) >= 0)).collect(Collectors.toList());
        MesShiftProdCenterRecord filterData = CollectionUtils.isEmpty(filterList) ? null :
                filterList.stream().filter(o -> null != o).sorted(Comparator.comparing(MesShiftProdCenterRecord::getModifyDatetime).reversed()).collect(Collectors.toList()).get(0);

        SHIFT_PROD_TYPE_COLOR enumItem;
        if (null == filterData) enumItem = SHIFT_PROD_TYPE_COLOR.NO_WORKING;
        else enumItem = SHIFT_PROD_TYPE_COLOR.getByValue(filterData.getProdType());

        dataMap.put(MesExtConstWords.STATUS, new StringJoiner(MesExtConstWords.COMMA).add(enumItem.workingStatusName).add(enumItem.color).toString());

        return dataMap;
    }

    enum SHIFT_PROD_TYPE_COLOR {
        //System.out.println(String.format("#%02x%02x%02x", Color.white.getRed(), Color.white.getGreen(), Color.white.getBlue()));
        NO_WORKING(0, "无生产", "无生产", "#808080", "gray"),
        NORMAL_STOP_CENTER(10, "常规停机", "常规停机", "#808080", "gray"),
        UNNORMAL_STOP_CENTER(20, "非常规停机", "异常停机", "#ff0000", "red"),
        NORMAL_PROD(30, "常规生产", "正常生产", "#00ff00", "green"),
        UNNORMAL_PROD(40, "非常规生产", "加班生产", "#ff0000", "red");

        private int value;
        private String description;
        private String workingStatusName;
        private String color;
        private String colorName;

        SHIFT_PROD_TYPE_COLOR(int value, String description, String workingStatusName, String color, String colorName) {
            this.value = value;
            this.description = description;
            this.workingStatusName = workingStatusName;
            this.color = color;
            this.colorName = colorName;
        }

        public int getValue() {
            return value;
        }

        public static SHIFT_PROD_TYPE_COLOR getByValue(int value) {
            for (SHIFT_PROD_TYPE_COLOR matchType : values()) {
                if (matchType.getValue() == value) {
                    return matchType;
                }
            }
            return null;
        }
    }

}
