package com.ywt.mg.domain.models.enums;

/**
 * nutrimeal 项目的 meal_order 表里面status字段
 */
public enum MealOrderStatusEnum {
    /**
     * 订单状态:0-待支付；1-已支付；2-已接单；4-配送中；8-待自取；16-已完成；32-已取消；64-已退款
     */
    WaitForPayment("待支付", 0),
    HasPayment("已支付", 1),
    HasAccess("已接单", 2),
    Dispatching("配送中", 4),
    WaitClaim("待自取", 8),
    Finished("已完成", 16),
    HasCancel("已取消", 32),
    HaveARefund("已退款", 64);

    private final String displayName;

    private final int value;

    MealOrderStatusEnum(String displayName, int value) {
        this.displayName = displayName;
        this.value = value;
    }

    public String getDisplayName() {
        return displayName;
    }

    public int getValue() {
        return value;
    }


    // TODO (如果是已退款状态，依旧显示"已取消") 注意第一个 if 判断的返回
    public static String getDisplayName(int value) {
        /** 方式一：
         for (MealOrderStatusEnum statusEnum : MealOrderStatusEnum.values()) {
         if ((statusEnum.value & value) == statusEnum.value && value < 2 * statusEnum.value) {
         return statusEnum.displayName;
         }
         }
         return "";
         */

        /**
         * 方式二：
         */
        if ((HaveARefund.value & value) == HaveARefund.value && (HasCancel.value & value) == HasCancel.value) {
            return HasCancel.displayName;
        }
        if ((HasCancel.value & value) == HasCancel.value) {
            return HasCancel.displayName;
        }
        if ((Finished.value & value) == Finished.value) {
            return Finished.displayName;
        }
        if ((WaitClaim.value & value) == WaitClaim.value) {
            return WaitClaim.displayName;
        }
        if ((Dispatching.value & value) == Dispatching.value) {
            return Dispatching.displayName;
        }
        if ((HasAccess.value & value) == HasAccess.value) {
            return HasAccess.displayName;
        }
        if ((HasPayment.value & value) == HasPayment.value) {
            return HasPayment.displayName;
        }
        if ((WaitForPayment.value & value) == WaitForPayment.value) {
            return WaitForPayment.displayName;
        }
        return "";
    }

    public static int getEnumValue(int value) {
        if ((HaveARefund.value & value) == HaveARefund.value && (HasCancel.value & value) == HasCancel.value) {
            return HaveARefund.value;
        }
        if ((HasCancel.value & value) == HasCancel.value) {
            return HasCancel.value;
        }
        if ((Finished.value & value) == Finished.value) {
            return Finished.value;
        }
        if ((WaitClaim.value & value) == WaitClaim.value) {
            return WaitClaim.value;
        }
        if ((Dispatching.value & value) == Dispatching.value) {
            return Dispatching.value;
        }
        if ((HasAccess.value & value) == HasAccess.value) {
            return HasAccess.value;
        }
        if ((HasPayment.value & value) == HasPayment.value) {
            return HasPayment.value;
        }
        if ((WaitForPayment.value & value) == WaitForPayment.value) {
            return WaitForPayment.value;
        }
        return WaitForPayment.value;
    }
}
