package com.izouma.nineth.web;

import com.izouma.nineth.domain.User;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.exception.AuthenticationException;
import com.izouma.nineth.security.JwtTokenUtil;
import com.izouma.nineth.security.JwtUserFactory;
import com.izouma.nineth.service.CaptchaService;
import com.izouma.nineth.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthenticationController {
    private final AuthenticationManager authenticationManager;
    private final JwtTokenUtil          jwtTokenUtil;
    private final UserService           userService;
    private final CaptchaService        captchaService;

    @PostMapping("/login")
    public String loginByUserPwd(String username, String password, Integer expiration) {
        User user = userService.loginByUsernamePwd(username, password);
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/loginAdmin")
    public String loginByUserPwdAdmin(String username, String password, Integer expiration) {
        User user = userService.loginByUsernamePwd(username, password);
        if (user.getAuthorities() == null ||
                user.getAuthorities().stream().anyMatch(au -> au.getName().equals(AuthorityName.ROLE_ADMIN.name()))) {
            throw new AuthenticationException("禁止登录", null);
        }
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/phoneLogin")
    @ApiOperation(value = "手机号验证码登录")
    public String phoneLogin(String phone, String code) {
        User user = userService.loginByPhone(phone, code);
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/phonePwdLogin")
    @ApiOperation(value = "手机号密码登录")
    public String phonePwdLogin(String phone, String password) {
        User user = userService.loginByPhonePwd(phone, password);
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/phoneRegister")
    @ApiOperation(value = "手机号密码注册")
    public String phonePwdLogin(String phone, String code, String password, String inviteCode, Long invitor,
                                Long collectionId, String captcha, String captchaKey) {
//        boolean verify = captchaService.verify(captchaKey, captcha);
//        if (!verify) {
//            throw new BusinessException("验证码错误");
//        }
        User user = userService.phoneRegister(phone, code, password, inviteCode, invitor, collectionId);
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/mqPhoneRegister")
    @ApiOperation(value = "手机号密码注册")
    public String mqPhoneRegister(String phone, String code, String password, String inviteCode, Long invitor,
                                  Long collectionId, String captcha, String captchaKey) {
//        boolean verify = captchaService.verify(captchaKey, captcha);
//        if (!verify) {
//            throw new BusinessException("验证码错误");
//        }
        return userService.mqRegister(phone, code, password, inviteCode, invitor, collectionId);
    }

    @GetMapping("/registerResult")
    public Object registerResult(String phone) {
        Object res = userService.getRegisterResult(phone);
        Map<String, Object> map = new HashMap<>();
        if (res == null) {
            map.put("status", "pending");
            return map;
        } else {
            return res;
        }
    }

    @PostMapping("/mpLogin")
    @ApiOperation(value = "公众号登录")
    public String mpLogin(String code) {
        try {
            User user = userService.loginMp(code);
            return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
        } catch (Exception e) {
            log.error("loginByCode", e);
            throw new AuthenticationException("登陆错误", e);
        }
    }

    @PostMapping("/maLogin")
    @ApiOperation(value = "小程序登录")
    public String maLogin(String code) {
        try {
            User user = userService.loginMa(code);
            return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
        } catch (Exception e) {
            log.error("loginByCode", e);
            throw new AuthenticationException("登陆错误", e);
        }
    }

    @GetMapping("/getTime")
    public Long getTime() {
        return System.currentTimeMillis();
    }


}
