package com.izouma.jiashanxia.service.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.izouma.jiashanxia.config.Constants;
import com.izouma.jiashanxia.domain.SmsRecord;
import com.izouma.jiashanxia.exception.BusinessException;
import com.izouma.jiashanxia.repo.SmsRecordRepo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Service
@Slf4j
public class AliSmsService implements SmsService {
    @Value("${aliyun.access-key-id}")
    private String        accessKeyId;
    @Value("${aliyun.access-key-secret}")
    private String        accessKeySecret;
    @Autowired
    private SmsRecordRepo smsRecordRepo;

    @Override
    public String sendVerify(String phone) {
        smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now()).ifPresent(record -> {
            if (record.getCreatedAt().plusMinutes(1L).isAfter(LocalDateTime.now())) {
                long sec = record.getCreatedAt().plusMinutes(1L).toInstant(ZoneOffset.UTC).getEpochSecond() - LocalDateTime.now().toInstant(ZoneOffset.UTC).getEpochSecond() + 1;
                throw new BusinessException("请" + sec + "秒后再试");
            }
        });

        String code = RandomStringUtils.randomNumeric(4);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", Constants.SMS_SIGN_NAME);
        request.putQueryParameter("TemplateCode", Constants.SMS_TEMPLATE_CODE_GENERIC);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            if (response.getHttpStatus() != 200) {
                throw new BusinessException("发送失败，请稍后再试", response.getHttpStatus() + "," + response.getData());
            }
            log.info("send sms response {}", response.getData());
            JSONObject jsonObject = new JSONObject(response.getData());
            if (!"ok".equalsIgnoreCase(jsonObject.getString("Code"))) {
                throw new BusinessException("发送失败，请稍后再试", jsonObject.getString("Message"));
            }
            smsRecordRepo.expire(phone);
            String sessionId = RandomStringUtils.randomAlphabetic(10);
            smsRecordRepo.save(SmsRecord.builder()
                                        .sessionId(sessionId)
                                        .phone(phone)
                                        .code(code)
                                        .expiresAt(LocalDateTime.now().plusMinutes(5))
                                        .expired(false)
                                        .build());
            return sessionId;
        } catch (ClientException | JSONException e) {
            e.printStackTrace();
            throw new BusinessException("发送失败，请稍后再试", e.getMessage());
        }
    }

    @Override
    public void verify(String phone, String code) throws SmsVerifyException {
        SmsRecord smsRecord = smsRecordRepo.findLastByPhoneAndExpiresAtAfterAndExpiredFalse(phone, LocalDateTime.now()).orElseThrow(new SmsVerifyException("验证码错误"));
        if (!smsRecord.getCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码错误");
        }
        smsRecord.setExpired(true);
        smsRecordRepo.save(smsRecord);
    }


}
