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

/**
 * 设备实施作业标准状态机与附件白名单（与养殖标准两维模型一致）。
 * <p>用例对照：doc/设备实施作业标准/设备实施作业标准管理测试用例.md 一、SS-BZ-UT-001～007、011～012</p>
 */
public final class EquipmentWorkStandardRules
{
    private EquipmentWorkStandardRules()
    {
    }

    /** 合法稳态 (publish_status, kb_sync_status)：(0,0)、(0,1)、(1,1)。 */
    public static boolean isValidStateCombo(int publish, int kb)
    {
        return (publish == 0 && kb == 0)
                || (publish == 0 && kb == 1)
                || (publish == 1 && kb == 1);
    }

    public static boolean canEdit(Integer publishStatus, Integer kbSyncStatus)
    {
        return publishStatus != null && publishStatus == 0
                && kbSyncStatus != null && kbSyncStatus == 0;
    }

    public static boolean canSyncKb(Integer publishStatus, Integer kbSyncStatus)
    {
        return publishStatus != null && publishStatus == 0
                && kbSyncStatus != null && kbSyncStatus == 0;
    }

    public static boolean canRemoveKb(Integer publishStatus, Integer kbSyncStatus)
    {
        return publishStatus != null && publishStatus == 0
                && kbSyncStatus != null && kbSyncStatus == 1;
    }

    public static boolean canPublish(Integer publishStatus, Integer kbSyncStatus)
    {
        return publishStatus != null && publishStatus == 0
                && kbSyncStatus != null && kbSyncStatus == 1;
    }

    public static boolean canOffline(Integer publishStatus)
    {
        return publishStatus != null && publishStatus == 1;
    }

    public static boolean canDelete(Integer publishStatus, Integer kbSyncStatus)
    {
        if (publishStatus == null)
        {
            return true;
        }
        return publishStatus == 0;
    }

    public static boolean isContentExtensionAllowed(String filenameOrPath)
    {
        String ext = extensionOf(filenameOrPath);
        return ext.equals("pdf") || ext.equals("md") || ext.equals("doc") || ext.equals("docx")
                || ext.equals("xls") || ext.equals("xlsx") || ext.equals("pptx");
    }

    public static boolean isCoverExtensionAllowed(String filenameOrPath)
    {
        String ext = extensionOf(filenameOrPath);
        return ext.equals("jpg") || ext.equals("jpeg") || ext.equals("png");
    }

    public static String extensionOf(String name)
    {
        if (name == null)
        {
            return "";
        }
        int i = name.lastIndexOf('.');
        if (i < 0 || i == name.length() - 1)
        {
            return "";
        }
        return name.substring(i + 1).toLowerCase();
    }
}
