package ink.wgink.module.sms.service.sms.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.module.sms.dao.sms.ISmsDao;
import ink.wgink.module.sms.factory.sms.SmsSendFactory;
import ink.wgink.module.sms.manager.VerifyCodeManager;
import ink.wgink.module.sms.pojo.dtos.sms.SmsDTO;
import ink.wgink.module.sms.pojo.vos.sms.SmsVO;
import ink.wgink.module.sms.service.sms.ISmsService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.properties.sms.SmsProperties;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: SmsServiceImpl
 * @Description: 短信
 * @Author: wanggeng
 * @Date: 2021/5/1 3:07 下午
 * @Version: 1.0
 */
@Service
public class SmsServiceImpl extends DefaultBaseService implements ISmsService {

    @Autowired
    private SmsProperties smsProperties;
    @Autowired
    private ISmsDao smsDao;

    @Override
    public void save(SmsVO smsVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(smsVO);
        params.put("smsId", UUIDUtil.getUUID());
        setSaveInfoByUserId(params, "1");
        smsDao.save(params);
    }

    @Override
    public void remove(List<String> ids) {
        Map<String, Object> params = getHashMap(6);
        params.put("smsIds", ids);
        setUpdateInfo(params);
        smsDao.remove(params);
    }

    @Override
    public SmsDTO get(Map<String, Object> params) {
        return smsDao.get(params);
    }

    @Override
    public SmsDTO get(String smsId) {
        Map<String, Object> params = getHashMap(2);
        params.put("smsId", smsId);
        return get(params);
    }

    @Override
    public List<SmsDTO> list(Map<String, Object> params) {
        return smsDao.list(params);
    }

    @Override
    public SuccessResultList<List<SmsDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<SmsDTO> smsDTOs = list(page.getParams());
        PageInfo<SmsDTO> pageInfo = new PageInfo<>(smsDTOs);
        return new SuccessResultList<>(smsDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public void sendContent(String phone, String content) {
        SmsVO smsVO = new SmsVO();
        smsVO.setPhone(phone);
        smsVO.setContent(content);
        try {
            LOG.info(">>>>> 向手机号：{}，发送内容：{}", phone, content);
            SmsSendFactory.getSendSms(smsProperties).content(phone, content);
            smsVO.setSendStatus(1);
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            smsVO.setSendStatus(0);
            smsVO.setErrorMessage(errorMessage);
            throw new SystemException("短信发送失败");
        } finally {
            save(smsVO);
        }
    }

    @Override
    public void sendVerifyCode(String phone) {
        VerifyCodeManager verifyCodeManager = VerifyCodeManager.getInstance();
        long timeRemaining = verifyCodeManager.effectiveVerificationCodeTimeRemaining(phone);
        if (timeRemaining > 0) {
            throw new ParamsException(String.format("验证码已发送，若没有收到，请在%d秒后重试", (timeRemaining / 1000)));
        }

        String currentTimeStr = String.valueOf(System.currentTimeMillis());
        String code = currentTimeStr.substring(currentTimeStr.length() - 6);
        LOG.info(">>>>> 向手机号：{}，发送验证码：{}", phone, code);

        SmsVO smsVO = new SmsVO();
        smsVO.setPhone(phone);
        smsVO.setContent(code);
        try {
            SmsSendFactory.getSendSms(smsProperties).code(phone, code);
            verifyCodeManager.setVerificationCode(phone, code);
            smsVO.setSendStatus(1);
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            smsVO.setSendStatus(0);
            smsVO.setErrorMessage(errorMessage);
            throw new SystemException("短信发送失败");
        } finally {
            save(smsVO);
        }
    }

    @Override
    public String getVerifyCode(String phone) {
        return VerifyCodeManager.getInstance().getVerifyCode(phone);
    }
}
