package com.micer.easyball.controller;

import com.micer.easyball.ApiVersion;
import com.micer.easyball.pojo.Club;
import com.micer.easyball.pojo.LoginUser;
import com.micer.easyball.pojo.ResponseResult;
import com.micer.easyball.pojo.User;
import com.micer.easyball.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(ApiVersion.V1)
public class UserController {

    @Autowired
    UserService userService;

    // 已移除测试接口 `hello()`，避免泄露内部信息或被滥用。

    /**
     *     切换角色
     */
    @PostMapping("/user/switchRole")
    @PreAuthorize("@ex.canSwitchToRole(#roleName)")
    public ResponseResult switchRole(@RequestParam String roleName)
    {
        return userService.switchRole(roleName);
    }

    /**
     *     获取用户所有角色
     */
    @GetMapping("/user/getRole")
    @PreAuthorize("isAuthenticated()")
    public ResponseResult getRole()
    {
        return userService.getRole();
    }

    /**
     *     获取用户当前角色
     */
    @GetMapping("/user/getCurrentRole")
    public ResponseResult getCurrentRole()
    {
        return userService.getCurrentRole();
    }

    /**
     *     切换俱乐部
     */

    @PostMapping("/user/switchClub")
    @PreAuthorize("isAuthenticated()")
    public ResponseResult switchClub(@RequestParam String clubId)
    {
        return userService.switchClub(clubId);
    }

    /**
     *     获取用户所属俱乐部
     */

    @GetMapping("/user/getClub")
    public ResponseResult getClub()
    {
        return userService.getClub();
    }

    /**
     *     获取用户当前俱乐部
     */

    @GetMapping("/user/getCurrentClub")
    public ResponseResult getCurrentClub()
    {
        return userService.getCurrentClub();
    }

    /**
     *      新增用户
     *      只需要提供用户手机号
     */
    @PostMapping("/user/addUser")
    @PreAuthorize("@ex.hasRole('ROLE_ADMIN')")
    public ResponseResult addUser(@RequestParam String telephone)
    {
        return userService.addUser(telephone);
    }

    /**
     *      新增用户到俱乐部
     *      只需要提供用户手机号
     */

    @PostMapping("/user/addUserToClub")
    @PreAuthorize("@ex.hasRole('ROLE_CLUB_ADMIN')")
    public ResponseResult addUserToClub(@RequestParam String telephone,
                                        @RequestParam String role)
    {
        return userService.addUserToClub(telephone, role);
    }

    /**
     *      从俱乐部中删除用户
     *      只需要提供用户手机号
     */
    @DeleteMapping("/user/deleteUserFromClub")
    @PreAuthorize("@ex.hasRole('ROLE_CLUB_ADMIN')")
    public ResponseResult deleteUserFromClub(@RequestParam String telephone)
    {
        return userService.deleteUserFromClub(telephone);
    }

    /**
     *      更改用户在俱乐部中的角色
     *      role只有ROLE_CLUB_ADMIN和ROLE_CLUB_PLAYER两个选项
     */
    @PutMapping("/user/modifyRole")
    @PreAuthorize("@ex.hasRole('ROLE_CLUB_ADMIN')")
    public ResponseResult modifyRole(@RequestParam String telephone,
                                     @RequestParam String role)
    {
        return userService.modifyRole(telephone, role);
    }

    /**
     *      更改用户密码
     */
    @PutMapping("/user/modifyPassword")
    @PreAuthorize("isAuthenticated()")
    public ResponseResult modifyPassword(@RequestParam String oldPassword,
                                     @RequestParam String newPassword)
    {
        return userService.modifyPassword(oldPassword, newPassword);
    }

    /**
     *      更改用户名
     */
    @PutMapping("/user/modifyUsername")
    public ResponseResult modifyUsername(@RequestParam String newUsername)
    {
        return userService.modifyUsername(newUsername);
    }

    /**
     * 获取登录用户所有信息
     */
    @GetMapping("/user/getLoginUserInfo")
    public ResponseResult getLoginUserInfo()
    {
        return userService.getLoginUserInfo();
    }


}
