package com.jsowell.api.uniapp;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.pile.dto.CarVinDTO;
import com.jsowell.pile.dto.MemberRegisterAndLoginDTO;
import com.jsowell.pile.service.ICarVinInfoService;
import com.jsowell.pile.vo.CarVinInfoVO;
import com.jsowell.pile.vo.uniapp.AuthCardVO;
import com.jsowell.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * TODO
 *
 * @author JS-ZZA
 * @date 2023/6/8 13:56
 */
@Anonymous
@RestController
@RequestMapping("/uniapp/carVin")
public class CarVinController extends BaseController {

    @Autowired
    private ICarVinInfoService carVinInfoService;

    @Autowired
    private MemberService memberService;

    /**
     * 用户查询绑定的vin列表
     * http://localhost:8080/uniapp/carVin/getCarVinList
     * @param request
     * @return
     */
    @GetMapping("/getCarVinList")
    public RestApiResponse<?> getCarVinList(HttpServletRequest request){
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            logger.info("查询用户Vin列表 param memberId:{}", memberId);
            List<CarVinInfoVO> list = carVinInfoService.getCarVinListByMemberId(memberId);
            response = new RestApiResponse<>(list);
        } catch (Exception e) {
            logger.error("查询用户Vin列表 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询用户Vin列表 result : {}", response);
        return response;
    }

    /**
     * 用户绑定车辆vin信息
     * http://localhost:8080/uniapp/carVin/bindCarVin
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/bindCarVin")
    public RestApiResponse<?> bindCarVin(HttpServletRequest request, @RequestBody CarVinDTO dto) {
        logger.info("用户绑定Vin params: {}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            if (memberId == null) {
                throw new BusinessException(ReturnCodeEnum.CODE_AUTHENTICATION_ERROR);
            }
            dto.setMemberId(memberId);
            // 校验短信验证码
            // MemberRegisterAndLoginDTO registerAndLoginDTO = MemberRegisterAndLoginDTO.builder()
            //         .mobileNumber(dto.getPhoneNumber())
            //         .verificationCode(dto.getVerificationCode())
            //         .build();
            // memberService.checkVerificationCode(registerAndLoginDTO);
            int i = carVinInfoService.bindAuthCard(dto);
            response = new RestApiResponse<>(i);
        }catch (BusinessException e){
            logger.warn("用户绑定Vin warn:", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("用户绑定Vin error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("用户绑定Vin result:{}", response);
        return response;
    }

    /**
     * 用户解绑车辆vin信息
     * http://localhost:8080/uniapp/carVin/unbindCarVin
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/unbindCarVin")
    public RestApiResponse<?> unbindCarVin(HttpServletRequest request, @RequestBody CarVinDTO dto) {
        logger.info("用户解绑车辆vin信息 params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            if (memberId == null) {
                throw new BusinessException(ReturnCodeEnum.CODE_AUTHENTICATION_ERROR);
            }
            dto.setMemberId(memberId);
            carVinInfoService.unbindCarVin(dto);
            response = new RestApiResponse<>();
        } catch (BusinessException e) {
            logger.warn("用户解绑车辆vin信息 warn ", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e) {
            logger.warn("用户解绑车辆vin信息 error ", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("用户解绑车辆vin信息 result :{}", response);
        return response;
    }
}
