package org.sky.resource.rule.oss;

import org.sky.core.literule.annotation.LiteRuleComponent;
import org.sky.core.literule.core.RuleSwitchComponent;
import org.sky.core.log.exception.ServiceException;
import org.sky.core.oss.enums.OssEnum;
import org.sky.resource.pojo.entity.Oss;
import org.sky.resource.rule.context.OssContext;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.sky.resource.rule.constant.OssRuleConstant.OSS_BUILD_RULE;

/**
 * Oss构建判断
 *
 */
@LiteRuleComponent(id = OSS_BUILD_RULE, name = "OSS构建条件判断")
public class OssBuildRule extends RuleSwitchComponent {
	private static final Map<Integer, String> CATEGORY_RULE_MAP = new HashMap<>();

	static {
		CATEGORY_RULE_MAP.put(OssEnum.MINIO.getCategory(), "minioRule");
		CATEGORY_RULE_MAP.put(OssEnum.QINIU.getCategory(), "qiniuOssRule");
		CATEGORY_RULE_MAP.put(OssEnum.ALI.getCategory(), "aliOssRule");
		CATEGORY_RULE_MAP.put(OssEnum.TENCENT.getCategory(), "tencentCosRule");
		CATEGORY_RULE_MAP.put(OssEnum.HUAWEI.getCategory(), "huaweiObsRule");
		CATEGORY_RULE_MAP.put(OssEnum.AMAZONS3.getCategory(), "amazonS3Rule");
		CATEGORY_RULE_MAP.put(OssEnum.LOCAL.getCategory(), "localFileRule");
	}

	@Override
	public List<String> process() {
		OssContext contextBean = this.getContextBean(OssContext.class);
		Oss oss = contextBean.getOss();

		String ruleName = CATEGORY_RULE_MAP.get(oss.getCategory());
		if (ruleName != null) {
			return Collections.singletonList(ruleName);
		}
		throw new ServiceException("未找到OSS配置");
	}
}
