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.MemberRegisterAndLoginDTO;
import com.jsowell.pile.dto.PileAuthCardDTO;
import com.jsowell.pile.service.IPileAuthCardService;
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;

/**
 * 鉴权卡相关
 *
 * @author JS-ZZA
 * @date 2023/5/23 9:32
 */
@Anonymous
@RestController
@RequestMapping("/uniapp/authCard")
public class AuthCardController extends BaseController {

    @Autowired
    private IPileAuthCardService pileAuthCardService;

    @Autowired
    private MemberService memberService;

    /**
     * 查询鉴权卡列表
     * http://localhost:8080/uniapp/authCard/getAuthCardList
     * @param request
     * @return
     */
    @GetMapping("/getAuthCardList")
    public RestApiResponse<?> getAuthCardList(HttpServletRequest request) {
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            logger.info("查询用户鉴权卡列表 param memberId:{}", memberId);
            List<AuthCardVO> list = pileAuthCardService.getAuthCardListByMemberId(memberId);
            response = new RestApiResponse<>(list);
        } catch (Exception e) {
            logger.error("查询鉴权卡列表 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("查询用户鉴权卡列表 result : {}", response);
        return response;
    }


    /**
     * 用户绑定鉴权卡
     * http://localhost:8080/uniapp/authCard/bindAuthCard
     * @param dto
     * @return
     */
    @PostMapping("/bindAuthCard")
    public RestApiResponse<?> bindAuthCard(HttpServletRequest request, @RequestBody PileAuthCardDTO dto) {
        logger.info("用户绑定鉴权卡 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 = pileAuthCardService.bindAuthCard(dto);
            response = new RestApiResponse<>(i);
        }catch (BusinessException e){
            logger.warn("用户绑定鉴权卡 warn:", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("用户绑定鉴权卡 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("用户绑定鉴权卡 result:{}", response);
        return response;
    }

    /**
     * 用户解绑鉴权卡
     * http://localhost:8080/uniapp/authCard/unbindAuthCard
     * @param dto
     * @return
     */
    @PostMapping("/unbindAuthCard")
    public RestApiResponse<?> unbindAuthCard(HttpServletRequest request, @RequestBody PileAuthCardDTO dto) {
        logger.info("用户解绑鉴权卡 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);
            pileAuthCardService.unbindAuthCard(dto);
            response = new RestApiResponse<>();
        } catch (BusinessException e) {
            logger.warn("用户解绑鉴权卡 warn ", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e) {
            logger.warn("用户解绑鉴权卡 error ", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("用户解绑鉴权卡 result :{}", response);
        return response;
    }

}
