package com.chinaweal.aiccs.outside.service;

import com.chinaweal.aiccs.outside.api.MosSMSApi;
import com.esms.MessageData;
import com.esms.PostMsg;
import com.esms.common.entity.Account;
import com.esms.common.entity.GsmsResponse;
import com.esms.common.entity.MTPack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * 短信服务
 */
@Service
@Slf4j
public class SMSServiceImpl {

    @Resource
    private MosSMSApi mosSMSApi;

    @Value("${sms.username}")
    private String username;
    @Value("${sms.ip}")
    private String ip;
    @Value("${sms.downPort}")
    private String downPort;
    @Value("${sms.upPort}")
    private String upPort;
    @Value("${sms.password}")
    private String password;


    /**
     * 发送短信
     *
     * @param text      内容
     * @param toPhone   电话
     * @param batchName 该批次的描述
     */
    public boolean sendSms(String text, String batchName, String toPhone) throws Exception {
        Set<String> set = new HashSet<>();
        set.add(toPhone);
        return sendSms(text, batchName, set);
    }


    /**
     * 发送短信
     *
     * @param text      内容
     * @param toPhones  电话集合
     * @param batchName 该批次的描述
     */
    public boolean sendSms(String text, String batchName, Set<String> toPhones) throws Exception {
        GsmsResponse gsmsResponse = basalticSdkSendSms(text, batchName, toPhones);
        if (gsmsResponse.getResult() == 0) {
            return true;
        } else {
            log.error("短信发送失败，{}", gsmsResponse);
            return false;
        }
    }

    public static void main(String[] args) {
        String s = DigestUtils.md5DigestAsHex("j35bEzJ5".trim().getBytes());
        System.out.println(s);
    }

    /**
     * 使用玄武的sdk发送短信
     */
    public GsmsResponse basalticSdkSendSms(String text, String batchName, Set<String> toPhones) throws Exception {
        final PostMsg pm = new PostMsg(false, 5000, 1);
        pm.setInterceptor((waitConnTime, loginTime, pack) -> {
            //加上自定义的日志输出
            log.debug("MT before send [" + pack.getBatchID()
                    + "] wait:" + waitConnTime
                    + ", login:" + loginTime
                    + ", now:" + System.currentTimeMillis());
        });
        pm.getCmHost().setHost(ip, Integer.parseInt(downPort));//设置网关的IP和port，用于发送信息
        pm.getWsHost().setHost(ip, Integer.parseInt(upPort));//设置网关的 IP和port，用于获取账号信息、上行、状态报告等等
        Account ac = new Account(username, password);
        MTPack pack = new MTPack();
        pack.setBatchID(UUID.randomUUID());
        pack.setBatchName(batchName);
        pack.setMsgType(MTPack.MsgType.SMS);
        pack.setBizType(2);
        pack.setDistinctFlag(false);
        ArrayList<MessageData> msgs = new ArrayList<>();

        pack.setSendType(MTPack.SendType.GROUP);
        for (String toPhone : toPhones) {
            msgs.add(new MessageData(toPhone, text));
        }
        pack.setMsgs(msgs);

        GsmsResponse resp = pm.post(ac, pack);
        return resp;
    }
}
