package io.controller;

import cn.hutool.crypto.digest.DigestUtil;
import io.annotation.Login;
import io.annotation.LoginUser;
import io.common.utils.Result;
import io.common.validator.ValidatorUtils;
import io.dto.LoginDTO;
import io.dto.RegisterDTO;
import io.entity.UserEntity;
import io.modules.item.dto.AccountDTO;
import io.modules.item.service.AccountService;
import io.service.TokenService;
import io.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "登录接口")
@AllArgsConstructor
@CrossOrigin
public class UserController {

    private final UserService userService;
    private final AccountService accountService;
    private final TokenService tokenService;
    @PostMapping("register")
    @Operation(summary = "注册")
    public Result register(@RequestBody RegisterDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            return new Result().error("两次密码输入不一致~");
        }
       if (userService.getByUsername(dto.getUsername()) != null) {
           return new Result().error("用户名已经存在~");
       }
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        user.setUsername(dto.getUsername());
        user.setNickName(dto.getNickName());
        user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        user.setCreateDate(new Date());
        userService.insert(user);
        return new Result();
    }


    @PostMapping("login")
    @Operation(summary = "登录")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO dto) {
        //表单校验
        ValidatorUtils.validateEntity(dto);
        //用户登录
        Map<String, Object> map = userService.login(dto);
        return new Result().ok(map);
    }

    @PutMapping("update")
    @Operation(summary = "修改信息")
    public Result update(@RequestBody RegisterDTO dto) {
        if (!dto.getPassword().equals(dto.getConfirmPassword())){
            return new Result().error("密码不一致");
        }
        //表单校验
        ValidatorUtils.validateEntity(dto);
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(dto,user);
        if (dto.getPassword() !=null){
            user.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        }
        userService.updateById(user);
        return new Result();
    }

    //充值
    @Login
    @PostMapping("recharge")
    @Operation(summary = "充值")
    public Result recharge(@RequestBody AccountDTO dto, @Parameter(hidden = true) @LoginUser UserEntity user) {
        //添加账户金额
        BigDecimal total = user.getBalance().add(dto.getBalance());
        user.setBalance(total);
        userService.updateById(user);
        //添加记录
        AccountDTO account = new AccountDTO();

        account.setUserId(user.getId());
        account.setNote("充值");
        account.setBalance(total);
        accountService.save(account);
        return new Result();
    }


    //提现
//    @Login
//    @PostMapping("withdraw")
//    @Operation(summary = "提现")
//    public Result withdraw(@RequestBody AccountDTO dto, @Parameter(hidden = true) @LoginUser UserEntity user) {
//
//        //添加账户金额
//        BigDecimal total = user.getBalance().add(dto.getBalance());
//        user.setBalance(total);
//        userService.updateById(user);
//        //添加记录
//        AccountDTO account = new AccountDTO();
//        account.setUserId(user.getId());
//        account.setDescribe("充值");
//        account.setBalance(total);
//        accountService.save(account);
//        return new Result();
//    }



    @Login
    @PostMapping("logout")
    @Operation(summary = "退出")
    public Result logout(@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        tokenService.expireToken(userId);
        return new Result();
    }

    @Login
    @GetMapping("userInfo")
    @Operation(summary = "获取用户信息")
    public Result<UserEntity> userInfo(@Parameter(hidden = true) @LoginUser UserEntity user) {
        return new Result<UserEntity>().ok(user);
    }

}
