package cn.newfeifan.mall.module.member.controller.app.user;

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.member.controller.app.user.vo.*;
import cn.newfeifan.mall.module.member.convert.user.MemberUserConvert;
import cn.newfeifan.mall.module.member.dal.dataobject.level.MemberLevelDO;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.service.level.MemberLevelService;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;

    @GetMapping("/verifyUsername")
    @Operation(summary = "校验用户名是否重复")
    public CommonResult<Boolean> verifyUsername(@NotEmpty(message = "用户名不能为空") @RequestParam(value = "username") String username) {
        return success(userService.selectCountByUsername(username) == 0);
    }


    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    @PreAuthenticated
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        AppMemberUserInfoRespVO convert = MemberUserConvert.INSTANCE.convert(user, level);
//        convert.setSubscribe(userService.getUserIsSubscribe(user.getId()));
        convert.setHavePayPassword(user.getPayPassword() != null);
        return success(convert);
    }

    @PutMapping("/update")
    @Operation(summary = "修改基本信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/addRegisterId")
    @Operation(summary = "添加安卓的极光注册id")
    @PreAuthenticated
    public CommonResult<Boolean> addRegisterId(String registerId) {
        if (registerId == null || registerId.trim().isEmpty()) return success(true);
        userService.addRegisterId(registerId);
        return success(true);
    }

    @PutMapping("/updateByAlipayAccount")
    @Operation(summary = "修改用户支付宝账号")
    public CommonResult<Boolean> updateUserByAlipayAccount(@RequestBody @Valid AppMemberUserUpdateAlipayAccountReqVO reqVO) {
        userService.updateUserByAlipayAccount(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/updateByBankAccount")
    @Operation(summary = "修改用户银行账号")
    public CommonResult<Boolean> updateByBankAccount(@RequestBody @Valid AppMemberUserUpdateBankAccountReqVO reqVO) {
        userService.updateUserByBankAccount(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile-by-weixin")
    @Operation(summary = "基于微信小程序的授权码，修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-payPassword")
    @Operation(summary = "修改用户支付密码", description = "用户修改支付密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserPayPassword(@RequestBody @Valid AppMemberUserUpdatePayPasswordReqVO reqVO) {
        userService.updateUserPayPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

    @GetMapping("/getUserByUserNameWithMobile")
    @Operation(summary = "根据用户名和手机号查询用户")
    public CommonResult<UserRespVO> getUserByUserNameWithMobile(@RequestParam(value = "username") String username, @RequestParam(value = "mobile") String mobile) {
        return success(BeanUtils.toBean(userService.getUserByUserNameWithMobile(username, mobile), UserRespVO.class));
    }

}

