package com.njcn.msgpush.module.push.client.factory.impl;

import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.client.factory.ProviderFactorySupport;
import com.njcn.msgpush.module.push.client.sender.AppPushSender;
import com.njcn.msgpush.module.push.client.sender.EmailSender;
import com.njcn.msgpush.module.push.client.sender.SendOutcome;
import com.njcn.msgpush.module.push.client.sender.Sender;
import com.njcn.msgpush.module.push.client.sender.SmsSender;
import com.njcn.msgpush.module.push.client.sender.fallback.FixedResultAppPushSender;
import com.njcn.msgpush.module.push.client.sender.fallback.FixedResultEmailSender;
import com.njcn.msgpush.module.push.client.sender.fallback.FixedResultSmsSender;
import com.njcn.msgpush.module.push.client.sender.impl.email.AliyunEmailSender;
import com.njcn.msgpush.module.push.client.sender.impl.sms.AliyunSmsSender;
import com.njcn.msgpush.module.push.client.setting.impl.AliYunMailSetting;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;

public class AliyunProviderFactory implements MessageProviderFactory {

    @Override
    public SmsSender createSmsSender(ChannelProviderConfigDO config, Sender sender) {
        // 配置缺失时返回固定结果 sender，由主流程统一落库为配置异常。
        if (ProviderFactorySupport.hasBlank(config.getAppKey(), config.getAppSecret())) {
            return new FixedResultSmsSender(SendOutcome.CONFIG_INVALID, "当前服务商短信渠道配置不完整");
        }
        AliYunMailSetting aliYunSmsSetting = AliYunMailSetting.builder()
                .accessKeyId(config.getAppKey())
                .accessKeySecret(config.getAppSecret())
                .regionId("cn-hangzhou")
                .endpoint("dysmsapi.aliyuncs.com")
                .build();
        return new AliyunSmsSender(aliYunSmsSetting, sender);
    }

    @Override
    public EmailSender createEmailSender(ChannelProviderConfigDO config, Sender sender) {
        // 配置缺失时返回固定结果 sender，由主流程统一落库为配置异常。
        if (ProviderFactorySupport.hasBlank(config.getAppKey(), config.getAppSecret())) {
            return new FixedResultEmailSender(SendOutcome.CONFIG_INVALID, "当前服务商邮件渠道配置不完整");
        }
        AliYunMailSetting aliYunMailSetting = AliYunMailSetting.builder()
                .accessKeyId(config.getAppKey())
                .accessKeySecret(config.getAppSecret())
                .regionId("cn-hangzhou")
                .endpoint("dm.aliyuncs.com")
                .build();
        return new AliyunEmailSender(aliYunMailSetting, sender);
    }

    @Override
    public AppPushSender createAppPushSender(ChannelProviderConfigDO config, Sender sender) {
        // 当前服务商没有该能力时，返回固定结果 sender，避免主流程处理 null。
        return new FixedResultAppPushSender(SendOutcome.UNSUPPORTED_CHANNEL, "当前服务商不支持 APP 推送");
    }
}
