package com.xmomen.module.core.web.controller;

import java.util.Set;

import com.xmomen.module.account.service.UserService;
import com.xmomen.module.core.web.controller.dto.AccountSettingDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class AccountController {

    // 注入UserService
    @Autowired
    UserService userService;

    // 获取账户设置
    @RequestMapping(value = "/account/setting", method = RequestMethod.GET)
    public AccountSettingDto accountSetting(){
        // 创建AccountSettingDto对象
        AccountSettingDto accountSettingDto = new AccountSettingDto();
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 如果当前用户为空，则返回null
        if(subject == null){
            return null;
        }
        // 获取当前用户的用户名
        String username = subject.getPrincipal().toString();
        // 将用户名设置到AccountSettingDto对象中
        accountSettingDto.setUsername(username);
        // 获取当前用户的角色
        Set<String> roles = userService.findRoles(username);
        // 返回AccountSettingDto对象
        return accountSettingDto;
    }

    // 重置密码
    @RequestMapping(value = "/account/resetPassword", method = RequestMethod.POST)
    public void resetPassword(@RequestParam(value = "current_password") String currentPassword,
                              @RequestParam(value = "password") String password){
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 获取当前用户的用户名
        String username = subject.getPrincipal().toString();
        // 调用UserService的changePassword方法，修改密码
        userService.changePassword(username, currentPassword, password);
    }


}
