package cn.qinys.platform.upms.api.controller;


import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.security.utils.CurrentUtils;
import cn.qinys.platform.base.vo.LoginUserInfo;
import cn.qinys.platform.upms.api.req.CurrentUserSwitchRoleReq;
import cn.qinys.platform.upms.api.req.CurrentUserUpdatePwdReq;
import cn.qinys.platform.upms.api.req.CurrentUserUpdateSelfReq;
import cn.qinys.platform.upms.api.resp.AdminTokenResp;
import cn.qinys.platform.upms.api.resp.CurrentUserInfoResp;
import cn.qinys.platform.upms.api.resp.LeftMenuResp;
import cn.qinys.platform.upms.api.service.ICurrentUserService;
import cn.qinys.platform.upms.api.service.ILoginService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2024/2/8 15:29
 */

@RestController
@RequestMapping("/upms/current-user")
public class CurrentUserController {

    @Resource
    ILoginService loginService;
    @Resource
    ICurrentUserService currentUserService;

    private static final String DEV_ADMIN = "dev_admin";

    /**
     * 当前登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public Result<CurrentUserInfoResp> getCurrentUserInfo() {
        CurrentUserInfoResp resp = currentUserService.getCurrentUserInfo();
        return new Result<>(resp);
    }

    /**
     * 密码状态校验
     *
     * @return
     */
    @GetMapping("/check-password-status")
    public Result<String> checkPasswordStatus() {
        String passwordStatus = currentUserService.checkPassword();
        String msg = "0".equals(passwordStatus) ? "还没过期" : "已过期";
        return new Result<>(msg, passwordStatus);
    }


    /**
     * 角色切换
     *
     * @return
     */
    @PostMapping("/switch-role")
    public Result<AdminTokenResp> switchRole(@RequestBody @Valid CurrentUserSwitchRoleReq req) {
        AdminTokenResp resp = currentUserService.switchRole(req);
        return new Result<>(resp);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        loginService.logout();
        return new Result<>("success");
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PostMapping("/update-pwd")
    public Result<String> changePassword(@RequestBody @Valid CurrentUserUpdatePwdReq req) {
        loginService.updatePassword(req);
        return new Result<>("success");
    }

    /**
     * 修改个人信息
     *
     * @return
     */
    @PostMapping("/update-self")
    public Result<String> updateSelf(@RequestBody @Valid CurrentUserUpdateSelfReq req) {
        currentUserService.updateSelf(req);
        return new Result<>("success");
    }

    /**
     * 重置token有效期
     *
     * @return
     */
    @PostMapping("/refresh-token")
    public Result<AdminTokenResp> refreshToken() {
        AdminTokenResp resp = loginService.refreshToken();
        return new Result<>(resp);
    }

    /**
     * 获取后台登录菜单
     *
     * @return
     */
    @GetMapping("/left-menus")
    public Result<List<LeftMenuResp>> getLeftMenus() {
        LoginUserInfo currentUserInfo = CurrentUtils.getCurrentUserInfo();
        // 管理员
        if (DEV_ADMIN.equals(currentUserInfo.getUsername()) && "1".equals(currentUserInfo.getTenantId())) {
            return new Result<>(currentUserService.getDevAdminAuthMenus());
        }
        List<LeftMenuResp> tree = currentUserService.getAuthMenuTree();
        return new Result<>(tree);
    }

}
