package com.ruoyi.web.modules.merchant.support;

import com.ruoyi.web.modules.merchant.constant.MerchantConstants;
import com.ruoyi.web.modules.merchant.vo.AllowedCertStatusVO;

/**
 * 认证状态机（§9.1）
 */
public final class CertStatusTransition
{
    private CertStatusTransition()
    {
    }

    public static boolean canTransition(String from, String to)
    {
        if (from == null || to == null || from.equals(to))
        {
            return false;
        }
        if (MerchantConstants.CERT_CANCELLED.equals(from) && MerchantConstants.CERT_FROZEN.equals(to))
        {
            return false;
        }
        if (MerchantConstants.CERT_NORMAL.equals(from))
        {
            return MerchantConstants.CERT_FROZEN.equals(to) || MerchantConstants.CERT_CANCELLED.equals(to);
        }
        if (MerchantConstants.CERT_FROZEN.equals(from))
        {
            return MerchantConstants.CERT_NORMAL.equals(to) || MerchantConstants.CERT_CANCELLED.equals(to);
        }
        if (MerchantConstants.CERT_CANCELLED.equals(from))
        {
            return MerchantConstants.CERT_NORMAL.equals(to);
        }
        return false;
    }

    public static AllowedCertStatusVO buildOption(String from, String to, String label)
    {
        AllowedCertStatusVO vo = new AllowedCertStatusVO();
        vo.setValue(to);
        vo.setLabel(label);
        boolean allowed = canTransition(from, to);
        vo.setNeedConfirm(allowed);
        vo.setConfirmMessage(allowed ? confirmMessage(from, to) : null);
        return vo;
    }

    public static String confirmMessage(String from, String to)
    {
        if (MerchantConstants.CERT_NORMAL.equals(from) && MerchantConstants.CERT_FROZEN.equals(to))
        {
            return "冻结后不可新开店铺，是否继续？";
        }
        if (MerchantConstants.CERT_NORMAL.equals(from) && MerchantConstants.CERT_CANCELLED.equals(to))
        {
            return "注销后资料不可编辑，是否继续？";
        }
        if (MerchantConstants.CERT_FROZEN.equals(from) && MerchantConstants.CERT_NORMAL.equals(to))
        {
            return "确认恢复为正常？";
        }
        if (MerchantConstants.CERT_FROZEN.equals(from) && MerchantConstants.CERT_CANCELLED.equals(to))
        {
            return "注销后资料不可编辑，是否继续？";
        }
        if (MerchantConstants.CERT_CANCELLED.equals(from) && MerchantConstants.CERT_NORMAL.equals(to))
        {
            return "恢复后可编辑、可开店（须经营信息完整），是否继续？";
        }
        return "确认修改认证状态？";
    }

    public static String statusLabel(String status)
    {
        if (MerchantConstants.CERT_NORMAL.equals(status))
        {
            return "正常";
        }
        if (MerchantConstants.CERT_FROZEN.equals(status))
        {
            return "已冻结";
        }
        if (MerchantConstants.CERT_CANCELLED.equals(status))
        {
            return "已注销";
        }
        return status;
    }
}
