package com.amms.controller;

import com.amms.domain.SysUser;
import com.amms.domain.dto.LoginParam;
import com.amms.domain.dto.UpdatePwdParam;
import com.amms.domain.vo.Result;
import com.amms.security.SecurityUtils;
import com.amms.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 登录方法
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginParam loginParam) {
        return sysUserService.login(loginParam);
    }


    @GetMapping("/list")
    public PageInfo list(SysUser sysUser, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers = sysUserService.selectSysUserList(sysUser);
        return new PageInfo(sysUsers);
    }

    @GetMapping("/listAll")
    public List<SysUser> listAll() {
        return sysUserService.selectSysUserList(null);
    }

    @GetMapping("/info/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(sysUserService.selectById(id));
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysUser sysUser) {
        return sysUserService.insertSysUser(sysUser) > 0 ? Result.success() : Result.error("添加失败");
    }

    @PostMapping("/register")
    public Result register(@RequestBody SysUser sysUser) {
        return sysUserService.registerTourist(sysUser) > 0 ? Result.success("注册成功") : Result.error("注册失败");
    }

    @PutMapping
    public Result edit(@RequestBody SysUser sysUser) {
        return sysUserService.updateSysUser(sysUser) > 0 ? Result.success() : Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id) {
        return sysUserService.deleteSysUserById(id) > 0 ? Result.success() : Result.error("删除失败");
    }

    /**
     * 获取当前用户
     * @return
     */
    @GetMapping("/getCurrentSysUser")
    public Result getCurrentSysUser() {
        SysUser currentUser = SecurityUtils.getCurrentUser();  // 当前登录用户
        SysUser sysUser = sysUserService.selectById(currentUser.getId());
        sysUser.setPassword(null);
        return Result.success(sysUser);
    }

    /**
     * 修改密码
     * @param updatePwdParam
     * @return
     */
    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdParam updatePwdParam) {
        SysUser currentUser = SecurityUtils.getCurrentUser();  // 当前登录用户

        if(currentUser == null) {
            return Result.error("用户不存在");
        }
        // 进行校验
        if (StringUtils.hasText(updatePwdParam.getOldPwd())
            && StringUtils.hasText(updatePwdParam.getNewPwd())
            && StringUtils.hasText(updatePwdParam.getConfirmPwd())) {
            boolean matches = passwordEncoder.matches(updatePwdParam.getOldPwd(), currentUser.getPassword());  // 旧密码对比
            if (!matches) {
                return Result.error("旧密码不正确！");
            }
            if (updatePwdParam.getNewPwd().equals(updatePwdParam.getConfirmPwd())) {  // 新密码和确认密码一致
                String newPwdEncode = passwordEncoder.encode(updatePwdParam.getNewPwd());
                currentUser.setPassword(newPwdEncode);
                int i = sysUserService.updateSysUser(currentUser);
                if (i > 0) {
                    return Result.success("密码更新成功！");
                } else {
                    return Result.error("密码更新失败！");
                }
            } else {
                return Result.error("两次密码输入不一致");
            }
        } else {
            return Result.error("不能为空");
        }
    }

}
