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

import java.util.Date;
import com.ruoyi.web.modules.farming.domain.BizTechResource;

/**
 * 列表/详情展示用发布三态。
 */
public final class TechResourcePublishStatusResolver
{
    private TechResourcePublishStatusResolver()
    {
    }

    public static void applyDisplayStatus(BizTechResource row)
    {
        if (row == null)
        {
            return;
        }
        int display = resolveDisplayCode(row.getPublishStatus(), row.getOfflineTime());
        row.setDisplayPublishStatus(display);
        row.setDisplayPublishStatusName(resolveDisplayName(display));
        row.setCanEdit(TechResourceRules.canEdit(row.getPublishStatus()));
        row.setCanOffline(TechResourceRules.canOffline(row.getPublishStatus()));
        row.setCanPublish(TechResourceRules.canPublish(row.getPublishStatus()));
        row.setCanDelete(TechResourceRules.canDelete(row.getPublishStatus()));
        row.setCanAssignAccount(canAssignAccount(row));
    }

    public static int resolveDisplayCode(Integer publishStatus, Date offlineTime)
    {
        if (TechResourceRules.isPublished(publishStatus))
        {
            return TechResourceRules.QUERY_PUBLISH_PUBLISHED;
        }
        if (offlineTime != null)
        {
            return TechResourceRules.QUERY_PUBLISH_OFFLINE;
        }
        return TechResourceRules.QUERY_PUBLISH_UNPUBLISHED;
    }

    public static String resolveDisplayName(int displayCode)
    {
        switch (displayCode)
        {
            case TechResourceRules.QUERY_PUBLISH_PUBLISHED:
                return "已发布";
            case TechResourceRules.QUERY_PUBLISH_OFFLINE:
                return "已下架";
            default:
                return "未发布";
        }
    }

    private static boolean canAssignAccount(BizTechResource row)
    {
        return TechResourceRules.canAssignAccount(row.getResourceType())
                && row.getAccountAssigned() != null
                && row.getAccountAssigned() == TechResourceRules.ACCOUNT_ASSIGNED_NO
                && TechResourceRules.canEdit(row.getPublishStatus());
    }
}
