package com.qmth.boot.core.fss.condition;

import com.qmth.boot.core.constant.CoreConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class FssConditionExecutor implements Condition {

    private static String PROPERTY_CONFIG = CoreConstant.CONFIG_PREFIX + ".fss.config";

    private static String PROPERTY_SERVER = CoreConstant.CONFIG_PREFIX + ".fss.server";

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Boolean single = (Boolean) metadata.getAnnotationAttributes(FssCondition.class.getName()).get("single");
        if (single) {
            return StringUtils.isNotBlank(context.getEnvironment().getProperty(PROPERTY_CONFIG)) && StringUtils
                    .isNotBlank(context.getEnvironment().getProperty(PROPERTY_SERVER));
        } else {
            return StringUtils.isBlank(context.getEnvironment().getProperty(PROPERTY_CONFIG)) && StringUtils
                    .isBlank(context.getEnvironment().getProperty(PROPERTY_SERVER));
        }
    }
}
