package com.vctgo.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.vctgo.auth.form.LoginBody;
import com.vctgo.auth.form.LoginRequest;
import com.vctgo.auth.form.RegisterBody;
import com.vctgo.auth.form.SmsResponse;
import com.vctgo.auth.service.SmsService;
import com.vctgo.auth.service.SysLoginService;
import com.vctgo.common.core.exception.CaptchaException;
import com.vctgo.common.core.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.vctgo.common.core.domain.R;
import com.vctgo.common.core.utils.JwtUtils;
import com.vctgo.common.security.auth.AuthUtil;
import com.vctgo.common.security.service.TokenService;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.model.LoginUser;

import java.util.Map;

/**
 * token 控制
 *
 * @author vctgo
 */
@RestController
public class TokenController {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private SmsService smsService;

    /**
     * 平台端登录
     *
     * @param form
     * @return
     */
    @PostMapping("/platform/login")
    public R<?> platformLogin(@RequestBody LoginBody form) {
        //验证码校验
        sysLoginService.verification(form.getCode());
        // 用户登录
        LoginUser userInfo = sysLoginService.login("1", form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 幼儿园端
     *
     * @param form
     * @return
     */
    @PostMapping("/kindergarten/login")
    public R<?> kindergartenLogin(@RequestBody LoginBody form) {
        //验证码校验
        sysLoginService.verification(form.getCode());
        // 用户登录
        LoginUser userInfo = sysLoginService.login("2", form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 小程序登录
     *
     * @param form
     * @return
     */
    @PostMapping("/mini-program/login")
    public R<?> miniProgram(@RequestBody LoginBody form) {
        // 用户登录
        LoginUser userInfo = sysLoginService.login("2", form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 专家登录
     *
     * @param form
     * @return
     */
    @PostMapping("/expert/login")
    public R<?> expertLogin(@RequestBody LoginBody form) {
        //验证码校验
        sysLoginService.verification(form.getCode());
        // 用户登录
        LoginUser userInfo = sysLoginService.login("3", form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StrUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody) {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }


    @PostMapping("/program/smsCode")
    public R<?> smsCode(@RequestBody LoginRequest form) {
        //验证码发送
        Map<String, Object> map = smsService.sendSms(form.getAccount());
        return R.ok(map);
    }


    /**
     * 评选小程序登录 微信一键授权 和 短信验证码登录
     *
     * @param form
     * @return
     */
    @PostMapping("/program/login")
    public R<?> program(@RequestBody LoginRequest form) {
        // 用户登录
        LoginUser userInfo = sysLoginService.programLogin(form);
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 小程序退出登录
     */
    @DeleteMapping("AppLogout")
    public R<?> AppLogout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    /**
     * 幼儿园端
     *
     * @param form
     * @return
     */
    @PostMapping("/kindergarten/voteLogin")
    public R<?> voteLogin(@RequestBody LoginBody form) {
        // 用户登录
        LoginUser userInfo = sysLoginService.voteLogin("2", form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

}
