package com.wteam.controller.wxlogin;

import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.security.enums.UserEnums;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.user.entity.User;
import com.wteam.framework.modules.user.entity.dto.WechatMPLoginParams;
import com.wteam.framework.modules.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
//DONE

/**
 * 微信小程序用户接口
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */

@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "微信相关接口管理")
@RequestMapping("/v1/wx/user")
public class WxMaUserController {

    @Autowired
    private UserService userService;

    /**
     * 登陆接口
     * 参数
     * code  微信返回code 用于与微信交互获取openid 等信息
     * encrypteData  微信返回加密信息
     * iv  微信返回
     */
    @ApiOperation(value = "微信登录")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", required = true, paramType = "body"),
            @ApiImplicitParam(name = "encryptedData", required = true, paramType = "body"),
            @ApiImplicitParam(name = "iv", required = true, paramType = "body"),
            @ApiImplicitParam(name = "invitedCode", required = false, paramType = "body")
    })
    public ResultMessage<Map<String, Object>> login(@RequestBody WechatMPLoginParams params) {
        return ResultUtil.data(this.userService.miniProgramLogin(params));
    }

    /**
     * 获取当前登录用户的具体信息
     *
     * @return
     */
    @ApiOperation(value = "获取当前登录用户接口")
    @GetMapping("/currentUser")
    public ResultMessage<User> getUserInfo() {
        return ResultUtil.data(userService.getUserInfo());
    }

    /**
     * <pre>
     * 获取用户绑定手机号信息
     * </pre>
     */
    @ApiOperation(value = " 获取用户绑定手机号信息")
    @PostMapping("/phone")
    public ResultMessage phone(@RequestBody WechatMPLoginParams params) {
        userService.getPhoneInfo(params);
        return ResultUtil.success();
    }


    /**
     * 修改用户名
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户名")
    @PostMapping("/updateUsername")
    public ResultMessage updateUsername(@RequestBody User user) {
        //传openId还有nickName
//      boolean update = userService.update(new UpdateWrapper<User>().eq("open_id", user.getOpenId()).set("nick_name", user.getNickName()));
        boolean update = userService.update().eq("open_id", user.getOpenId()).set("nick_name", user.getNickName()).update();
        return update ? ResultUtil.success() : ResultUtil.error(ResultCode.ERROR);
    }

    /**
     * 修改用户信息
     *
     * @param entity
     * @return
     */
    @PutMapping
    @Operation(summary = "用户修改")
    public ResultMessage update(@RequestBody User entity) {
        return userService.updateById(entity) ? ResultUtil.success() : ResultUtil.error(ResultCode.USER_UPDATE_ERROR);
    }


    /**
     * <pre>
     * 获取用户绑定手机号信息2
     * </pre>
     */
    @ApiOperation(value = "获取用户绑定手机号信息2")
    @PostMapping("/phone2")
    public ResultMessage phone(@RequestParam("phoneNumber") String phoneNumber, @RequestParam("openId") String openId) {
        userService.update().eq("open_id", openId).set("phone_number", phoneNumber).update();
        return ResultUtil.success();
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据用户id查询用户信息")
    @GetMapping("/queryUserById/{id}")
    public ResultMessage<User> queryUserById(@PathVariable("id") String id) {
        User user = userService.query().eq("id", id).one();
        return ResultUtil.data(user);
    }

    /**
     * 修改用户头像
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户头像")
    @PostMapping("/updateAvatar")
    public ResultMessage updateAvatar(@RequestBody User user) {
        //传openId还有
        boolean update = userService.update().eq("open_id", user.getOpenId()).set("avatar", user.getAvatar()).update();
//      boolean update = userService.update(new UpdateWrapper<User>().eq("open_id", user.getOpenId()).set("avatar", user.getAvatar()));
        return update ? ResultUtil.success() : ResultUtil.error(ResultCode.ERROR);
    }

    /***
     * 退出登录
     * @return
     */
    @ApiOperation(value = "用户注销")
    @GetMapping(value = "/logout")
    public ResultMessage<Object> logout() {
        this.userService.logout(UserEnums.USER);
        return ResultUtil.success();
    }


//    /***
//     * 退出登录
//     * @return
//     */
//    @GetMapping(value = "/exit")
//    public ResultMessage exit(HttpServletRequest request) {
//        return userService.exit(request);
//    }
}
