package com.njcn.msgpush.module.push.service.message;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.constant.MessageStatusConstant;
import com.njcn.msgpush.module.push.constant.MsgPushConstant;
import com.njcn.msgpush.module.push.controller.admin.message.vo.MessageRecordSendReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.dal.dataobject.message.MessageRecordDO;
import com.njcn.msgpush.module.push.dal.mysql.message.MessageRecordMapper;
import com.njcn.msgpush.module.push.service.channel.ChannelProviderConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecordDO> implements MessageRecordService {

    @Autowired
    @Qualifier("messageProviderFactoryMap")
    private Map<String, MessageProviderFactory> messageProviderFactoryMap;

    @Autowired
    private ChannelProviderConfigService channelProviderConfigService;

    @Override
    public Boolean send(MessageRecordSendReqVO messageRecordSendReqVO) {
        MessageRecordDO messageRecordDO = BeanUtil.copyProperties(messageRecordSendReqVO, MessageRecordDO.class);
        messageRecordDO.setStatus(MessageStatusConstant.PENDING);
        this.save(messageRecordDO);

        ChannelProviderConfigDO channelProviderConfigDO = channelProviderConfigService.getByTypeAndChannel(messageRecordDO.getProviderType(), messageRecordDO.getChannel());
        //channelProviderConfigDO.setAppKey("LTAI4FxsR76x2dq3w9c5puUe");
        //channelProviderConfigDO.setAppSecret("GxkTR8fsrvHtixTlD9UPmOGli35tZs");
        MessageProviderFactory messageProviderFactory = messageProviderFactoryMap.get(messageRecordDO.getProviderType());
        boolean sendResult = switch (messageRecordDO.getChannel()) {
            case MsgPushConstant.CHANNEL_SMS -> messageProviderFactory.createSmsSender(channelProviderConfigDO).sendSms(messageRecordDO);
            //case MsgPushConstant.CHANNEL_EMAIL ->
            //messageProviderFactory.createEmailSender(channelProviderConfigDO).sendEmail(messageRecordDO.getTemplateParams(), messageRecordDO.getReceiver());
            case MsgPushConstant.CHANNEL_APP_PUSH -> messageProviderFactory.createAppPushSender(channelProviderConfigDO).appPush(messageRecordDO);
            default -> throw new RuntimeException("暂不支持该渠道：" + messageRecordDO.getChannel());
        };

        return sendResult;
    }

    @Override
    public Boolean add(MessageRecordSendReqVO messageRecordSendReqVO) {
        return null;
    }
}
