package com.wteam.controller.user;

import com.wteam.framework.common.enums.*;
import com.wteam.framework.common.security.AuthUser;
import com.wteam.framework.common.security.context.UserContext;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.book.entity.Card;
import com.wteam.framework.modules.book.entity.CouponUser;
import com.wteam.framework.modules.book.entity.OrderInfo;
import com.wteam.framework.modules.book.entity.vo.OrderInfoVo;
import com.wteam.framework.modules.book.service.OrderInfoService;
import com.wteam.framework.modules.user.entity.User;
import com.wteam.framework.modules.user.entity.form.UpdateUserForm;
import com.wteam.framework.modules.user.mapper.UserMapper;
import com.wteam.framework.modules.user.service.UserService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.Sun;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;

/**
 * @author doncic
 * @date 2023/9/16 22:20
 */
@Slf4j
@Tag(name = "用户相关接口管理")
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;


    /**
     * 修改用户信息
     *
     * @param accessToken
     * @param userForm
     * @return
     */
    @Operation(summary = "修改用户信息")
    @PutMapping("/update/{accessToken}")
    public ResultMessage<UpdateUserForm> updateUser(@NotNull @PathVariable("accessToken") String accessToken, @RequestBody UpdateUserForm userForm) {
        AuthUser authUser = UserContext.getAuthUser(accessToken);
        assert authUser != null;
        return userService.updateById(userForm) ? ResultUtil.success() : ResultUtil.error(ResultCode.UPDATE_USER_ERROR);
    }

    /**
     * 查看订单信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "查看订单信息")
    @PostMapping("/showOrder/{id}")
    public ResultMessage showOrder(
            @ApiParam(value = "订单id", required = true) @PathVariable("id") String id) {
        return userService.showOrder(id);
    }

    @Operation(summary = "查看对应类型的订单信息")
    @GetMapping("/showOrder/{orderTypeEnum}/{id}")
    public ResultMessage showOrderByType(
            @PathVariable("id") String id,
            @PathVariable OrderTypeEnums orderTypeEnum) {
        return userService.showOrderByType(id, orderTypeEnum);
    }

    /**
     * 查看我的卡情况
     * id
     *
     * @param cardTypeEnum
     * @return
     */
    @Operation(summary = "查看我的卡")
    @PostMapping("/show/{cardTypeEnum}/{id}")
    public ResultMessage<List<Card>> showMyCard(
            @ApiParam(value = "用户id", required = true) @NotNull @PathVariable Long id,
            @PathVariable CardTypeEnum cardTypeEnum) {
        return userService.showMyCard(id, cardTypeEnum);
    }


    @Operation(summary = "查看我的优惠卷")
    @PostMapping("/showMyCoupon/{accessToken}")
    private ResultMessage<CouponUser> showMyCoupon(@NotNull @PathVariable String accessToken) {
        return userService.showMyCoupon(accessToken);

    }
}
