package com.fdkankan.ucenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.service.impl.LoginService;
import com.fdkankan.ucenter.vo.request.RegisterParam;
import com.fdkankan.ucenter.vo.request.ShipAddressParam;
import com.fdkankan.ucenter.vo.request.UserParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 个人中心
 */
@RestController
@RequestMapping("/ucenter/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private LoginService loginService;


    @RequestMapping(value = "/checkToken", method = RequestMethod.POST)
    public Result checkToken() {
        return Result.success();
    }

    /**
     * 获取用户信息
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public Result getUserInfo() {
        return Result.success(userService.getUserInfo(getUserName()));
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    public Result changePassword(@RequestBody RegisterParam param){
        loginService.changePassword(param);
        return Result.success();
    }

    /**
     * 用户上传头像
     * imgdata 图片的base64码
     */
    @RequestMapping(value = "/uploadHead", method = RequestMethod.POST)
    public Result uploadHead(@RequestBody JSONObject jsonObject) throws Exception {
        return Result.success(userService.uploadHead(jsonObject.getString("imgdata"),getUserName()));
    }

    /**
     * 用户新增收货地址
     * shipAddress      收货地址-详细地址
     * shipAreaPath     收货地址-省-市-区
     * province         所在地区-省
     * city             所在地区-市
     * shipName         收货人
     * shipMobile       收货人手机
     */
    @RequestMapping(value = "/insertAddress", method = RequestMethod.POST)
    public Result insertAddress(@RequestBody ShipAddressParam param){
        userService.insertAddress(param,getUserName());
        return Result.success();
    }

    /**
     * 用户更新默认地址
     * id
     * shipAddress      收货地址-详细地址
     * shipAreaPath     收货地址-省-市-区
     * province         所在地区-省
     * city             所在地区-市
     * shipName         收货人
     * shipMobile       收货人手机
     * setDefault       1表示默认地址，0表示不是默认地址
     */
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)

    public Result updateAddress(@RequestBody ShipAddressParam param){
        userService.updateAddress(param,getUserName());
        return Result.success();
    }

    /**
     * 用户删除默认地址
     */
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    public Result deleteAddress(@RequestBody ShipAddressParam param){
        userService.deleteAddress(param.getId());
        return Result.success();
    }
    /**
     * 用户更新邮箱地址
     * email        邮箱地址
     */
    @RequestMapping(value = "/updateEmail", method = RequestMethod.POST)
    public Result updateEmail(@RequestBody UserParam param){
        userService.updateEmail(param.getEmail(),getUserName());
        return Result.success();
    }

    /**
     * 获取用户默认收货信息
     */
    @RequestMapping(value = "/getReceiverInfo", method = RequestMethod.POST)
    public Result getReceiverInfo(){
        return  Result.success(userService.getReceiverInfo(getUserName()));
    }

    /**
     * 获取用户收货信息列表
     */
    @RequestMapping(value = "/getReceiverList", method = RequestMethod.POST)
    public Result getReceiverList(){
        return Result.success(userService.getReceiverList(getUserName()));
    }

    /**
     * 更改昵称
     * nickName     用户昵称
     */
    @RequestMapping(value = "/updateNickName", method = RequestMethod.POST)
    public Result updateNickName(@RequestBody UserParam param){
        userService.updateNickName(param.getNickName(),getUserName());
        return Result.success();
    }
    /**
     * 修改用户信息
     * nickName     昵称
     * email        邮箱
     * isNotice     isNotice
     */
    @RequestMapping(value = "/updateUserDetail", method = RequestMethod.POST)
    public Result updateUserDetail(@RequestBody UserParam param){
        userService.updateUserDetail(param,getUserName());
        return Result.success();
    }
}
