package cn.iocoder.yudao.module.order.enums;

import cn.iocoder.yudao.framework.i18n.core.I18nMessage;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;


@AllArgsConstructor
@Getter
public enum OrderStatusMsgEnum {

    DRAFT(0, 10101, 0, 0, "草稿", "order.status.draft", "order.status.draft"),
    //1未使用
    ORDER_PLACED(2, 10102, 0, 0, "待入仓", "order.status.placed", "order.status.placed"),
    WAREHOUSING(3, 10103, 201, 0, "入仓中", "order.status.warehousing.progress", "order.status.warehousing.progress"),

    IN_WAREHOUSE(5, 10105, 202, 0, "已入仓", "order.status.warehousing", "order.status.warehousing"),
    IN_WAREHOUSE_UPDATE(5, 10106, 203, 0, "已入仓，修改中", "order.status.warehousing.update", "order.status.warehousing"),
    ROLLBACK_APPLYING(5, 10107, 204, 0, "退仓中", "order.status.withdrawal", "order.status.withdrawal"),
    WITHDRAWN_EXPENSES_NOT_CLEARED(6, 10108, 205, 0, "退仓通过，费用待清缴", "order.status.withdrawn.expense.not.cleared", "order.status.withdrawn.expense.not.cleared"),
    rollback_finished(7, 10109, 206, 0, "已退仓", "admin.order.status.returned", "customer.order.status.returned"),
    order_split_applying(5, 10110, 207, 0, "拆单中", "admin.order.status.split", "order.status.warehousing"),
    order_merge_applying(5, 10111, 209, 0, "合单中", "order.status.combination", "order.status.combination"),
    adjust_applying(5, 10112, 212, 0, "调仓中", "order.status.adjust", "order.status.adjust"),

    ADJUST_WAREHOUSE(5, 10113, 215, 0, "调仓已到仓", "order.status.adjust.arrived.warehouse", "order.status.adjust.arrived.warehouse"),
    EXIT_WAREHOUSE(5, 10114, 216, 0, "报关退场已入仓", "order.status.adjust.demobilized.warehoused", "order.status.warehousing"),
    PREINSTALL_EXAMINE(5, 10115, 0, 305, "预装审核中", "order.status.pre.install.review", "order.status.pre.install.review"),


    WITHDRAWN(7, 0, 0, 0, "已退仓", "Withdrawn", "order.status.draft"),
    adjust_wait_out(8, 10202, 213, 0, "调仓待出仓", "order.status.adjust.wait.out.warehouse", "order.status.adjust"),
    WAREHOUSE_ADJUSTMENT(8, 10203, 214, 0, "调仓待入仓", "order.status.adjust.wait.in.warehouse", "order.status.adjust"),
    MERGED_ORDER(9, 10204, 210, 0, "已合单", "order.status.closed", "order.status.closed"),
    MERGED_ORDER_FINISH(9, 10205, 211, 0, "已合单归档", "order.status.closed", "order.status.closed"),
    SPLIT_ORDER(10, 10206, 208, 0, "已拆单", "order.status.split.succeeded", "order.status.warehousing"),


    PRE_INSTALLED(11, 10207, 0, 307, "待装柜", "order.status.preinstalled", "order.status.preinstalled"),
    SORTINGED(11, 102071, 0, 407, "待理货", "order.status.sortinged", "order.status.sortinged"),
    DISPATCHED_WAIT_CABINETS(11, 10208, 0, 309, "已派车，待提柜", "order.status.dispatched.car.wait.pick.counter", "order.status.preinstalled"),
    LIFTED_WAIT_RETURN(11, 10209, 0, 310, "已提柜，待还柜", "order.status.dispatched.car.wait.returned", "order.status.preinstalled"),
    CABINETS_RETURNED(11, 10210, 0, 311, "已还柜", "order.status.returned", "order.status.preinstalled"),
    IN_THE_CABINETS(11, 10211, 0, 313, "装柜中", "order.status.preinstalled", "order.status.preinstalled"),


    INSTALLED_WAIT_SEALED(12, 10212, 0, 314, "已装柜，待封柜", "order.status.cabinet.installed", "order.status.cabinet.installed"),
    SEAL_EXAMINE(12, 10213, 0, 315, "封柜审核中", "order.status.closed.cabinet.review", "order.status.closed.cabinet.review"),
    WAIT_SEALED(12, 10214, 0, 317, "封柜审核通过,待封柜", "order.status.closed.cabinet.progress", "order.status.closed.cabinet.progress"),
    SEALED(12, 10214, 0, 318, "已封柜", "order.status.closed.cabinet", "order.status.closed.cabinet"),
    CUSTOMSING(12, 10215, 0, 320, "报关中", "order.status.in.customs.declaration", "order.status.in.customs.declaration"),
    CUSTOMS_PART_EXIT(12, 10216, 0, 322, "报关-查验失败-部分退场", "order.status.under.inspection", "order.status.in.customs.declaration"),
    CUSTOMS_FULL_EXIT(12, 10217, 0, 323, "报关-查验失败-全部退场", "order.status.under.inspection", "order.status.in.customs.declaration"),
    CUSTOMS_PASS(12, 10218, 0, 324, "报关-查验后放行", "order.status.release.after.inspection", "order.status.declared"),
    CUSTOMS_FINISH(12, 10224, 0, 325, "已放行", "order.status.declared", "order.status.declared"),

    CUSTOMSING_AIR(12, 10315, 0, 413, "报关中", "order.status.in.customs.declaration", "order.status.in.customs.declaration"),
    CUSTOMS_PART_EXIT_AIR(12, 10316, 0, 414, "报关-查验失败-部分退场", "order.status.under.inspection", "order.status.in.customs.declaration"),
    CUSTOMS_FULL_EXIT_AIR(12, 10317, 0, 415, "报关-查验失败-全部退场", "order.status.under.inspection", "order.status.in.customs.declaration"),
    CUSTOMS_PASS_AIR(12, 10318, 0, 416, "报关-查验后放行", "order.status.release.after.inspection", "order.status.declared"),
    CUSTOMS_FINISH_AIR(12, 10324, 0, 417, "已放行", "order.status.declared", "order.status.declared"),
    CUSTOMS_ABNORMAL_EXAM(12, 10325, 0, 418, "空运报关-删单退场审核中", "order.status.customs.abnormal.exam", "order.status.customs.abnormal.exam"),
    CUSTOMS_ABNORMAL_WEIGHT(12, 10326, 0, 419, "空运报关-过机重量异常", "order.status.customs.abnormal.weight", "order.status.customs.abnormal.weight"),
    CUSTOMS_ABNORMAL_EXAM_PASS(12, 10327, 0, 420, "空运报关-删单退场审核通过", "order.status.customs.abnormal.exam.pass", "order.status.customs.abnormal.exam.pass"),
    CUSTOMS_COMPLETE(12, 10328, 0, 421, "已报关", "order.status.customs.finish", "order.status.customs.finish"),

    IN_SHIPPING(13, 10219, 0, 326, "起运-航运中", "order.status.in.shipping", "order.status.in.shipping"),
    IN_FLYING(13, 13001, 0, 426, "已起飞", "order.status.in.flying", "order.status.in.flying"),

    ARRIVAL_WAIT_DISCHARGE(14, 10220, 0, 328, "已到目的港，待卸港", "order.status.arrived", "order.status.arrived"),
    DISCHARGE_WAIT_CLEARANCE(14, 10221, 0, 329, "已卸港，待清关", "order.status.arrived", "order.status.arrived"),

    CLEARANCE_WAIT_UNLOAD(15, 10222, 0, 330, "已清关，待卸柜", "order.status.cleared", "order.status.cleared"),
    CLEARED_UNLOAD_EXAMINE(15, 10223, 0, 332, "卸柜审核中    ", "order.status.cleared", "order.status.cleared"),
    CLEARED_UNLOAD_EXAMINE_AIR(15, 11223, 0, 432, "到仓审核中    ", "order.status.warehouse.review", "order.status.warehouse.review"),

    UNLOADED_CABINET(16, 10116, 218, 335, "已卸柜、目的港到仓合并", "order.status.unloaded.cabinet", "order.status.unloaded.cabinet"),
    UNLOADED_CABINET_AIR(16, 11116, 218, 435, "已到仓", "order.status.warehoused", "order.status.warehoused"),

    UNLOADED_CABINET_ADJUST(16, 10117, 212, 0, "调仓审核中", "order.status.unloaded.cabinet", "order.status.unloaded.cabinet"),
    UNLOADED_CABINET_WAIT_OUT(16, 10118, 213, 0, "调仓待出仓", "order.status.unloaded.cabinet", "order.status.unloaded.cabinet"),
    UNLOADED_CABINET_WAIT_IN(16, 10119, 214, 0, "调仓待入仓", "order.status.unloaded.cabinet", "order.status.unloaded.cabinet"),
    UNLOADED_CABINET_IN(16, 10120, 215, 0, "调仓已到仓", "order.status.unloaded.cabinet", "order.status.unloaded.cabinet"),
    UNCOLLECTED(17, 10121, 0, 391, "二程起飞", "order.status.two.way.takeoff", "order.status.two.way.takeoff"),
    TWP_WAY_ARRIVAL(18, 10122, 0, 392, "二程到目的港", "order.status.two.way.arrival", "order.status.two.way.arrival"),
    AIR_ARRIVAL(18, 118428, 0, 428, "空运到港", "order.status.arrived", "order.status.arrived"),
    TWP_WAY_CLEARED_UNLOAD_EXAMINE(18, 10126, 0, 332, "卸柜审核中", "order.status.cleared", "order.status.cleared"),
    PICKED_UP(20, 10124, 0, 0, "已提货或已送达", "order.status.picked.up", "order.status.picked.up"),
    PART_PICKED_UP(21, 10125, 0, 0, "部分提货", "order.status.part.picked.up", "order.status.part.picked.up"),

    //....预留定义
    TALLY_COMPLETE(32, 132409, 0, 409, "完成理货", "order.status.tally.complete", "order.status.tally.complete"),
    //    WAIT_OUT_SHIPMENT(32, 10127, 0, 410, "待出货", "order.status.wait.out.shipment", "order.status.wait.out.shipment"),
    OUT_SHIPMENT(32, 132411, 0, 411, "已出货", "order.status.out.shipment", "order.status.out.shipment"),
    WAREHOUSED(32, 132412, 0, 412, "已出仓", "order.status.out.warehouse", "order.status.out.warehouse"),

    ORDER_UPDATE(50, 55555, 0, 0, "订单修改中", "order.status.update.progress", "order.status.update.progress"),
    CANCEL(88, 88888, 0, 0, "取消订单", "order.status.cancel", "order.status.cancel"),

    // 以下为空运已入仓订单转义状态文案
    AIR_TO_BE_RELEASED(501, 10105, 202, 0, "空运待出", "air.to.be.released", "air.to.be.released"),
    AIR_TO_BE_PREPARED(502, 10105, 202, 0, "仓库待备货", "air.to.be.prepared", "air.to.be.prepared"),
    AIR_IN_STOCK(503, 10105, 202, 0, "仓库备货中", "air.in.stock", "air.in.stock"),
    AIR_READY_STOCK_AND_WAITING_TO_BE_ARRANGED(504, 10105, 202, 0, "待排单", "air.ready.stock.and.waiting.to.be.arranged", "air.ready.stock.and.waiting.to.be.arranged"),

    ;


    @ApiModelProperty(value = "订单状态详情见字典：order_status")
    private final Integer status;

    @ApiModelProperty(value = "状态key值,前端对应")
    private final Integer statusKey;

    /**
     * 入仓状态 {@link OrderWarehouseInStatusEnum}
     */
    private final Integer inWarehouseState;

    /**
     * 订单出货状态 {@link BoxStatusEnum}
     */
    private final Integer shipmentState;
    /**
     * 类型说明
     */
    private final String name;

    /**
     * 管理端类型中英文源数据
     */
    private final String adminSourceName;

    /**
     * 客户端端类型中英文源数据
     */
    private final String customerSourceName;


    public static String getMsg(Integer status, Integer inWarehouseState, Integer shipmentState, Integer airShipment,
                                Integer transportId, Integer userType, Integer lang) {
        for (OrderStatusMsgEnum value : OrderStatusMsgEnum.values()) {
            String message = userType == 2 ? value.adminSourceName : value.customerSourceName;
            if (Objects.equals(value.status, status) && shipmentState != 0 && Objects.equals(value.shipmentState, shipmentState)) {
                if (inWarehouseState != 0){
                    if (Objects.equals(value.inWarehouseState, inWarehouseState)){
                        // 返回定义出货状态
                        return lang == 0 ? I18nMessage.getZhMessage(message) : I18nMessage.getEnMessage(message);
                    }
                }else {
                    // 返回定义出货状态
                    return lang == 0 ? I18nMessage.getZhMessage(message) : I18nMessage.getEnMessage(message);
                }
            } else if (Objects.equals(value.status, status) && inWarehouseState != 0 && Objects.equals(value.inWarehouseState, inWarehouseState)) {
                if (shipmentState != 0){
                    if (Objects.equals(value.shipmentState, shipmentState)){
                        // 返回定义出货状态
                        return lang == 0 ? I18nMessage.getZhMessage(message) : I18nMessage.getEnMessage(message);
                    }
                }else {
                    // 空运订单，且不是入仓中订单，需要判断订单备货状态
                    if ((Objects.equals(TransportTypeShortEnum.AIR_FREIGHT_LINE.getValue(), transportId)
                            || Objects.equals(TransportTypeShortEnum.AIR_SEA_COMBINED_TRANSPORT.getValue(), transportId))
                            && status != 3 && inWarehouseState != 201) {
                        // 空运已入仓订单需要判断备货状态文案
                        if (Objects.equals(status, OrderStatusEnum.IN_WAREHOUSE.getValue()) && airShipment > 0) {
                            switch (airShipment) {
                                case 1:
                                    message = userType == 2 ? AIR_TO_BE_RELEASED.adminSourceName : AIR_TO_BE_RELEASED.customerSourceName;
                                    break;
                                case 2:
                                    message = userType == 2 ? AIR_TO_BE_PREPARED.adminSourceName : AIR_TO_BE_PREPARED.customerSourceName;
                                    break;
                                case 3:
                                    message = userType == 2 ? AIR_IN_STOCK.adminSourceName : AIR_IN_STOCK.customerSourceName;
                                    break;
                                case 4:
                                    message = userType == 2 ? AIR_READY_STOCK_AND_WAITING_TO_BE_ARRANGED.adminSourceName : AIR_READY_STOCK_AND_WAITING_TO_BE_ARRANGED.customerSourceName;
                                    break;
                                default:
                                    message = lang == 0 ? "状态错误" : "STATUS_ERROR";
                            }
                        }

                    }
                    // 返回定义的入仓状态
                    return lang == 0 ? I18nMessage.getZhMessage(message) : I18nMessage.getEnMessage(message);
                }
            } else if (Objects.equals(value.status, status) && (status == 0 || status == 2 || status == 4 || status == 20 || status == 21 || status == 50 || status == 88)) {
                // 这里是没有定义子状态的主状态值
                // 返回定义的主状态
                return lang == 0 ? I18nMessage.getZhMessage(message) : I18nMessage.getEnMessage(message);
            }
        }

        return lang == 0 ? "状态错误" : "STATUS_ERROR";
    }


    public static Integer getStatusKey(Integer status, Integer inWarehouseState, Integer shipmentState) {

        for (OrderStatusMsgEnum value : OrderStatusMsgEnum.values()) {
            if (Objects.equals(value.status, status) && shipmentState != 0 && Objects.equals(value.shipmentState, shipmentState)) {
                // 返回定义出货状态key
                return value.statusKey;
            } else if (Objects.equals(value.status, status) && inWarehouseState != 0 && Objects.equals(value.inWarehouseState, inWarehouseState)) {
                // 返回定义的入仓状态key
                return value.statusKey;
            } else if (Objects.equals(value.status, status)) {
                // 返回定义的主状态key
                return value.statusKey;
            }
        }

        return 99999;
    }


}
