package com.inkasso.factoring.system.user.controller;

import com.inkasso.adapter.response.ResponseData;
import com.inkasso.adapter.web.LoginUserHolder;
import com.inkasso.factoring.system.user.entity.UserDO;
import com.inkasso.factoring.system.user.model.dto.UserDTO;
import com.inkasso.factoring.system.user.model.request.*;
import com.inkasso.factoring.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 张超
 * @date 2023-08-15
 */
@RestController
@RequestMapping(path = "user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userDO
     * @return
     */
    @PostMapping("register")
    public ResponseData register(@RequestBody UserDO userDO) {
        userService.register(userDO);
        return ResponseData.success();
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @GetMapping("currentUser")
    public ResponseData currentUser() {
        UserDTO userDTO = userService.getUserDtoById(LoginUserHolder.get().getId());
        return ResponseData.success(userDTO);
    }


    @PostMapping("phoneCode")
    public ResponseData sendPhoneCode(@Validated @RequestBody  SendPhoneCodeRequest sendPhoneCodeRequest) {
        userService.sendCode(sendPhoneCodeRequest.getPhone(), sendPhoneCodeRequest.getType());
        return ResponseData.success();
    }


    /**
     * 发送手机短信验证码
     * @param phoneNumber
     * @param type
     * @return
     */
    @RequestMapping("phoneCode")
    public ResponseData sendPhoneCode(@Validated @RequestParam("phoneNumber") String phoneNumber,@RequestParam("type") String type) {
        userService.sendCode(phoneNumber, type);
        return ResponseData.success();
    }

    /**
     * 发送 email 验证码
     *
     * @param email
     * @return
     */
    @RequestMapping("emailCode")
    public ResponseData sendEmailCode(@Validated @RequestParam("mail") String email) {
        userService.sendEmailCode(email);
        return ResponseData.success();
    }

    /**
     * 手机号绑定 后  返回最新的用户信息
     *
     * @param bindPhoneRequest
     * @return
     */
    @PostMapping("phoneBind")
    public ResponseData phoneBind(@Validated @RequestBody BindPhoneRequest bindPhoneRequest,@RequestHeader("Token")String token) {
        userService.bindPhone(bindPhoneRequest,token);
        UserDTO userDtoById = userService.getUserDtoById(LoginUserHolder.get().getId());
        return ResponseData.success(userDtoById);
    }

    /**
     * 修改密码
     *
     * @param editPasswordRequest
     * @return
     */
    @PutMapping("password")
    public ResponseData passwordEdit(@Validated @RequestBody EditPasswordRequest editPasswordRequest) {
        userService.editPassword(editPasswordRequest);
        return ResponseData.success();
    }


    /**
     *  绑定邮件
     *
     * @param bindEmailRequest
     * @return
     */
    @PostMapping("emailBind")
    public ResponseData emailBind(@Validated @RequestBody BindEmailRequest bindEmailRequest) {
        userService.bindEmail(bindEmailRequest);
        return ResponseData.success();
    }


    /**
     *  修改头像
     *
     * @param fileId
     * @return
     */
    @PutMapping("userHeadImg/{fileId}")
    public ResponseData userHeadImg(@PathVariable("fileId") Long fileId) {
        userService.updateHeadImg(fileId);
        return ResponseData.success();
    }

    /**
     * 重置密码
     * @param reSetPasswordRequest
     * @return
     */
    @PostMapping("reSetPassword")
    public  ResponseData  reSetPassword(@Validated @RequestBody ReSetPasswordRequest reSetPasswordRequest){
        userService.reSetPassword(reSetPasswordRequest);
        return ResponseData.success();
    }
    /**
     * 修改用户名
     * @param username
     * @return
     */
    @PutMapping("username/{username}")
    public ResponseData userHeadImg(@PathVariable("username")String username,@RequestHeader("token") String token) {
        UserDO userDO = userService.updateUsername(username,token);
        userDO.setPassword(null);

        return ResponseData.success(username);
    }




//    @PostMapping(path = "user")
//    @PermissionAndCheck(name="添加用户",code="UserController:add")
//    public ResponseData add(@Validated @RequestBody AddUserRequest addUserRequest) {
//
//        userService.addUser(addUserRequest);
//        return ResponseData.success();
//
//    }
//
//    @PutMapping(path = "user")
//    @PermissionAndCheck(name="更新用户",code="UserController:update")
//    public ResponseData edit(@Validated @RequestBody EditUserRequest editUserRequest) {
//        userService.editUser(editUserRequest);
//        return ResponseData.success();
//    }
//
//    @GetMapping(path = "user/{id}")
//    @PermissionAndCheck(name = "查询用户详细信息", code="UserController:detail")
//    public ResponseData getById(@PathVariable("id") Long id){
//        if(id==null){
//            throw new ServiceException(400, KafubaoExceptionEnum.SYSTEM_DATE_ERROR);
//        }
//        UserDTO userDTO = userService.getById(id);
//        return ResponseData.success(userDTO);
//
//    }
//
//
//    @DeleteMapping(path = "user/{id}")
//    @PermissionAndCheck(name = "删除用户", code="UserController:delete")
//    public ResponseData deleteById(@PathVariable("id") Long id){
//        if(id==null){
//            throw new ServiceException(400, KafubaoExceptionEnum.SYSTEM_DATE_ERROR);
//        }
//        userService.deleteUser(id);
//        return ResponseData.success();
//    }

}
