package org.sky.resource.rule.sms;

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.sms.enums.SmsEnum;
import org.sky.resource.pojo.entity.Sms;
import org.sky.resource.rule.constant.SmsRuleConstant;
import org.sky.resource.rule.context.SmsContext;

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

/**
 * Sms构建判断
 *
 */
@LiteRuleComponent(id = SmsRuleConstant.SMS_BUILD_RULE, name = "SMS构建条件判断")
public class SmsBuildRule extends RuleSwitchComponent {

	private static final Map<Integer, String> CATEGORY_RULE_MAP = new HashMap<>();

	static {
		CATEGORY_RULE_MAP.put(SmsEnum.YUNPIAN.getCategory(), "yunpianSmsRule");
		CATEGORY_RULE_MAP.put(SmsEnum.QINIU.getCategory(), "qiniuSmsRule");
		CATEGORY_RULE_MAP.put(SmsEnum.ALI.getCategory(), "aliSmsRule");
		CATEGORY_RULE_MAP.put(SmsEnum.TENCENT.getCategory(), "tencentSmsRule");
	}

	@Override
	public List<String> process() {
		SmsContext contextBean = this.getContextBean(SmsContext.class);
		if (contextBean.getIsCached()) {
			return Collections.singletonList("cacheSmsRule");
		}
		Sms sms = contextBean.getSms();
		String ruleName = CATEGORY_RULE_MAP.get(sms.getCategory());
		if (ruleName != null) {
			return Collections.singletonList(ruleName);
		}
		throw new ServiceException("未找到SMS配置");
	}
}
