package com.szxgl.nft.controller;

import com.aliyuncs.exceptions.ClientException;
import com.szxgl.nft.entity.RedisKeyName;
import com.szxgl.nft.entity.UserNftPhoto;
import com.szxgl.nft.service.NftPhotoService;
import com.szxgl.nft.utils.AliyunSmsUtils;
import com.szxgl.nft.utils.CookieUtil;
import com.szxgl.nft.utils.R;
import com.szxgl.nft.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/code")
@Slf4j
public class VerificationCodeController {

    @Autowired
    private RedisUtil redisUtil;

    private static final String openidCode = RedisKeyName.getOpenidCode();

    @Autowired
    private NftPhotoService nftPhotoService;

    /**
     * 获取验证码
     * @param seed 手机号
     * @return
     */
    @PostMapping("/getCode")
    public R getCode(String seed, HttpServletRequest request){
        String openid = CookieUtil.getCookie(request.getCookies());
        if(StringUtils.isBlank(openid)){
            log.error("openid为空："+openid);
            return R.error("openid为空");
        }
        //判断手机号是否为空
        if(StringUtils.isBlank(seed)){
            log.error("手机号为空："+seed);
            return R.error("手机号为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //活动开始时间
            String activityStartDate = "2022-3-6 10:00:00";
            long StartDate = sdf.parse(activityStartDate).getTime();
            //活动结束时间
            String activityEndDate = "2022-3-7 12:00:00";
            long EndDate = sdf.parse(activityEndDate).getTime();
            //当前时间
            long currentDate = new Date().getTime();
//            String activityCurrentDate = "2022-3-6 9:00:00";
//            long currentDate = sdf.parse(activityCurrentDate).getTime();
            //在活动时间内

            if(currentDate < StartDate){
                return R.error("活动时间未开始");
            }
            if(currentDate >= StartDate && currentDate <= EndDate){
                //香港手机号格式化：去掉区号
                if(seed.contains(" ")|| seed.contains("-")){
                    seed = seed.replaceAll(" ", "");
                    seed = seed.replaceAll("-", "");
                    seed = seed.substring(4);
                }
                //判断是否为定向用户
                UserNftPhoto nftPhoto = nftPhotoService.getNftPhoto(seed);
                if(nftPhoto == null){
                    log.error("不是定向用户！");
                    return R.error("不是定向用户");
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取验证码
        String code = Integer.toString(AliyunSmsUtils.getNewcode());
        log.info("发送的验证码为："+code);
        try {
            //使用阿里云发送短信
            AliyunSmsUtils.sendSms(seed,code);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //把验证码存入redis中
        redisUtil.set(openidCode + openid, code, 300);
        return R.ok(code);
    }


}
