package com.gxwebsoft.payment.enums;

/**
 * 支付状态枚举
 * 定义支付过程中的各种状态
 *
 * @author 科技小王子
 * @since 2025-01-26
 */
public enum PaymentStatus {
    
    /** 待支付 */
    PENDING(0, "待支付", "PENDING"),
    
    /** 支付中 */
    PROCESSING(1, "支付中", "PROCESSING"),
    
    /** 支付成功 */
    SUCCESS(2, "支付成功", "SUCCESS"),
    
    /** 支付失败 */
    FAILED(3, "支付失败", "FAILED"),
    
    /** 支付取消 */
    CANCELLED(4, "支付取消", "CANCELLED"),
    
    /** 支付超时 */
    TIMEOUT(5, "支付超时", "TIMEOUT"),
    
    /** 退款中 */
    REFUNDING(6, "退款中", "REFUNDING"),
    
    /** 退款成功 */
    REFUNDED(7, "退款成功", "REFUNDED"),
    
    /** 退款失败 */
    REFUND_FAILED(8, "退款失败", "REFUND_FAILED"),
    
    /** 部分退款 */
    PARTIAL_REFUNDED(9, "部分退款", "PARTIAL_REFUNDED");

    private final Integer code;
    private final String name;
    private final String status;

    PaymentStatus(Integer code, String name, String status) {
        this.code = code;
        this.name = name;
        this.status = status;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getStatus() {
        return status;
    }

    /**
     * 根据代码获取支付状态
     */
    public static PaymentStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (PaymentStatus status : values()) {
            if (status.code.equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 根据状态字符串获取支付状态
     */
    public static PaymentStatus getByStatus(String status) {
        if (status == null) {
            return null;
        }
        for (PaymentStatus paymentStatus : values()) {
            if (paymentStatus.status.equals(status)) {
                return paymentStatus;
            }
        }
        return null;
    }

    /**
     * 是否为最终状态（不会再变化）
     */
    public boolean isFinalStatus() {
        return this == SUCCESS || this == FAILED || this == CANCELLED || 
               this == TIMEOUT || this == REFUNDED || this == REFUND_FAILED;
    }

    /**
     * 是否为成功状态
     */
    public boolean isSuccessStatus() {
        return this == SUCCESS;
    }

    /**
     * 是否为失败状态
     */
    public boolean isFailedStatus() {
        return this == FAILED || this == CANCELLED || this == TIMEOUT || this == REFUND_FAILED;
    }

    /**
     * 是否为退款相关状态
     */
    public boolean isRefundStatus() {
        return this == REFUNDING || this == REFUNDED || this == REFUND_FAILED || this == PARTIAL_REFUNDED;
    }

    /**
     * 是否可以退款
     */
    public boolean canRefund() {
        return this == SUCCESS || this == PARTIAL_REFUNDED;
    }

    /**
     * 是否可以取消
     */
    public boolean canCancel() {
        return this == PENDING || this == PROCESSING;
    }

    @Override
    public String toString() {
        return String.format("PaymentStatus{code=%d, name='%s', status='%s'}", code, name, status);
    }
}
