package org.wcs.constant.enums.database;

import lombok.Getter;
import org.wcs.constant.enums.business.SimpleTaskEnum;

import java.util.List;

/**
 * 单项任务类型枚举
 */
@Getter
public enum StockSingleTaskTypeEnum {

    STACKER_AUTO(-1, "堆垛机自动任务"),

    UNKNOWN(0, "未知任务"),

    STACKER_IN(1, "堆垛机入库"),
    STACKER_OUT(2, "堆垛机出库"),
    STACKER_MOVE(9, "堆垛机移库"),
    CONVEY_IN(4, "输送入库"),
    CONVEY_OUT(5, "输送出库"),
    CONVEY_MOVE(6, "输送移动"),
    CONVEY_PICK(7, "输送线捡选"),
    CONVEY_CHECK(8, "输送线复核"),

    AGV(11, "AGV任务"),
    SHUTTLE(10, "穿梭车任务"),

    BINDING_VEHICLE_NO(12, "绑定载具号任务"); // 这个一版时载具号搬运出来后将载具号绑定到输送机上



    private final Integer code;
    private final String desc;

    StockSingleTaskTypeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static StockSingleTaskTypeEnum getByCode(Integer code) {
        if (code == null) {
            return UNKNOWN;
        }
        for (StockSingleTaskTypeEnum value : values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return UNKNOWN;
    }

    /**
     * 获取所有堆垛机任务类型
     * @return 堆垛机任务类型
     */
    public static List<Integer> getAllStackerTaskType() {
        return List.of(STACKER_IN.getCode(), STACKER_OUT.getCode(), STACKER_MOVE.getCode());
    }

    /**
     * 获取所有输送任务类型
     * @return 输送任务类型
     */
    public static List<Integer> getAllConveyTaskType() {
        return List.of(CONVEY_IN.getCode(), CONVEY_OUT.getCode(), CONVEY_MOVE.getCode());
    }

    /**
     * 根据简单任务类型获取堆垛机任务类型
     * @param simpleTaskType 简单任务类型
     * @return 堆垛机任务类型
     */
    public static StockSingleTaskTypeEnum getBySimpleTaskType(SimpleTaskEnum simpleTaskType) {
        return switch (simpleTaskType) {
            case STACKER -> STACKER_AUTO;
            case CONVEY, TRAY_CONVEY -> CONVEY_MOVE;
            case AGV -> AGV;
            case SHUTTLE -> SHUTTLE;
            default -> UNKNOWN;
        };
    }

    public static StockSingleTaskTypeEnum getByComposeTaskTypeAndExecuteMachine(StockComposeTaskTypeEnum composeTaskType, StockExecuteEquipmentEnum executeMachine) {
        if(executeMachine == StockExecuteEquipmentEnum.STACKER && composeTaskType == StockComposeTaskTypeEnum.IN) {
            return StockSingleTaskTypeEnum.STACKER_IN; // 入库
        }
        if(executeMachine == StockExecuteEquipmentEnum.STACKER && composeTaskType == StockComposeTaskTypeEnum.OUT) {
            return StockSingleTaskTypeEnum.STACKER_OUT; // 出库
        }
        if(executeMachine == StockExecuteEquipmentEnum.STACKER && composeTaskType == StockComposeTaskTypeEnum.MOVE) {
            return StockSingleTaskTypeEnum.STACKER_MOVE; // 移库
        }
        if(executeMachine == StockExecuteEquipmentEnum.TRAY && composeTaskType == StockComposeTaskTypeEnum.IN) {
            return StockSingleTaskTypeEnum.CONVEY_IN;
        }
        if(executeMachine == StockExecuteEquipmentEnum.TRAY && composeTaskType == StockComposeTaskTypeEnum.OUT) {
            return StockSingleTaskTypeEnum.CONVEY_OUT;
        }
        if(executeMachine == StockExecuteEquipmentEnum.TRAY && composeTaskType == StockComposeTaskTypeEnum.MOVE) {
            return StockSingleTaskTypeEnum.CONVEY_MOVE;
        }
        return UNKNOWN;
    }

    /**
     * 输送任务类型转换成PLC任务类型
     * @param taskType 输送任务类型
     * @return PLC任务类型
     */
    public static short conveyTaskTypeToPlc(Integer taskType) {
        StockSingleTaskTypeEnum taskTypeEnum = getByCode(taskType);
        return switch (taskTypeEnum) {
            case CONVEY_IN -> 1;
            case CONVEY_OUT -> 2;
            case CONVEY_MOVE -> 3;
            default -> 0;
        };

    }

}
