package com.yonge.cooleshow.teacher.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.service.SmsCodeService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.cooleshow.common.security.SecurityConstants;

@RestController
@RequestMapping("code")
@Api(tags = "验证码服务")
public class SmsCodeController extends BaseController {

    @Autowired
    private SmsCodeService smsCodeService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "发送登录短信验证码")
    @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String")
    @PostMapping(value = "/sendSmsCode")
    public Object sendLoginVerifyCode(String mobile) throws Exception {
        smsCodeService.sendValidCode(mobile);
        return succeed();
    }

    @ApiOperation(value = "校验短信验证码")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, dataType = "String") })
    @PostMapping(value = "/verifySmsCode")
    public Object verifySmsCode(String phone,String code) {
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        if(smsCodeService.verifyValidCode(phone,code)){
            return succeed();
        }
        return failed("验证码校验失败");
    }

    @PostMapping(value = "/verifyImageCode")
    @ApiOperation("校验登录图形验证码")
    @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String") })
    public Object verifyImageCode(String phone,String code){
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        String redisKey = MessageTypeEnum.KAPTCHA_SESSION_KEY + phone;
        if(redisTemplate.hasKey(redisKey)){
            if(StringUtils.equalsIgnoreCase(redisTemplate.opsForValue().get(redisKey),code)){
                return succeed();
            }
        }
        return failed(SecurityConstants.VERIFY_FAILURE);
    }

    @RequestMapping("/getImageCode")
    @ApiOperation("获取登录图片验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String")
    public void getLoginImage(HttpServletRequest request, HttpServletResponse response,String phone) throws Exception {
        if(StringUtils.isEmpty(phone)){
            throw new BizException("请输入手机号");
        }
        SpecCaptcha specCaptcha = new SpecCaptcha(125, 45, 4);
        redisTemplate.opsForValue().set(MessageTypeEnum.KAPTCHA_SESSION_KEY + phone,specCaptcha.text(),3, TimeUnit.MINUTES);
        CaptchaUtil.out(specCaptcha, request, response);
    }
}
