package com.ghy.common.enums;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单状态
 *
 * @author clunt
 */
public enum OrderStatus implements IEnumType {

    GOING_CANCEL(-2, "待上门时申请取消"),
    SERVER_CANCEL(-3, "服务中申请取消"),
    FINISH_CHECK_CANCEL(-4, "待确认时申请取消"),

    RECEIVE(0, "待接单"),
    PLAIN(1, "待排期"),
    GOING(2, "待上门"),
    SERVER(3, "服务中"),
    FINISH_CHECK(4, "待确认"),
    FINISH(5, "已完成"),
    CANCEL(6, "已取消"),
    Pending(7,"待提现");

    private final Integer code;
    private final String desc;

    private final static Map<Integer, OrderStatus> BY_CODE_MAP
            = Arrays.stream(OrderStatus.values()).collect(Collectors.toMap(OrderStatus::code, code -> code));

    private final static Map<String, OrderStatus> BY_NAME_MAP
            = Arrays.stream(OrderStatus.values()).collect(Collectors.toMap(code -> code.name().toLowerCase(), code -> code));


    /**
     * @param code 代码
     * @param desc 描述
     */
    OrderStatus(final int code, final String desc) {
        this.code = code;
        this.desc = desc;
    }

    @Override
    public int code() {
        return this.code;
    }

    @Override
    public String desc() {
        return this.desc;
    }

    /**
     * @param code 代码
     * @return 转换出来的状态码
     */
    public static OrderStatus parse(Integer code) {
        return BY_CODE_MAP.get(code);
    }

    public static OrderStatus parse(Integer code, OrderStatus defaultState) {
        return BY_CODE_MAP.getOrDefault(code, defaultState);
    }

    /**
     * @param name 名字
     * @return 转换出来的状态码
     */
    public static OrderStatus parse(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return BY_NAME_MAP.get(name.trim().toLowerCase());
    }

    public static OrderStatus parse(String name, OrderStatus defaultState) {
        if (StringUtils.isBlank(name)) {
            return defaultState;
        }
        return BY_NAME_MAP.getOrDefault(name.trim().toLowerCase(), defaultState);
    }
}
