package com.ycwl.basic.controller.mobile;

import com.ycwl.basic.exception.BaseException;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.mobile.face.FaceRecognizeResp;
import com.ycwl.basic.model.mobile.face.FaceStatusResp;
import com.ycwl.basic.model.mobile.scenic.content.ContentPageVO;
import com.ycwl.basic.model.mobile.face.FaceRecognitionUpdateReq;
import com.ycwl.basic.model.mobile.face.FaceRecognitionDetailVO;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
import com.ycwl.basic.repository.FaceRepository;
import com.ycwl.basic.service.pc.FaceService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/4 17:03
 */
@RestController
@RequestMapping("/api/mobile/face/v1")
// 用户人脸相关接口
public class AppFaceController {

    @Autowired
    private FaceService faceService;
    @Autowired
    private FaceRepository faceRepository;

    /**
     * 1、上传人脸照片
     * 2、人脸照片有效性校验
     * 3、校验失败，删除，提示重新上传
     * 4、校验成功，保存用户人脸信息，将访问人脸照片访问地址响应给前端
     * @param file
     * @param scenicId
     * @return
     */
    // 人脸照片上传
    @PostMapping("/faceUPload")
    public ApiResponse<FaceRecognizeResp> faceUpload(@RequestParam("file")MultipartFile file,
                                                     @RequestParam(value = "scene", defaultValue = "", required = false) String scene,
                                                     @RequestParam("scenicId") Long scenicId) {
        //获取用户id
        JwtInfo worker = JwtTokenUtil.getWorker();
        Long userId = worker.getUserId();
        FaceRecognizeResp resp = faceService.faceUpload(file, scenicId, userId, scene);
        return ApiResponse.success(resp);
    }

    @GetMapping("/scenic/{scenicId}/list")
    public ApiResponse<List<FaceRespVO>> list(@PathVariable("scenicId") String scenicId) {
        JwtInfo worker = JwtTokenUtil.getWorker();
        Long userId = worker.getUserId();
        List<FaceRespVO> list = faceService.listByUser(userId, Long.parseLong(scenicId));
        return ApiResponse.success(list);
    }

    @GetMapping("/{faceId}")
    public ApiResponse<FaceEntity> getById(@PathVariable("faceId") Long faceId) {
        FaceEntity face = faceRepository.getFace(faceId);
        return ApiResponse.success(face);
    }

    @DeleteMapping("/{faceId}")
    public ApiResponse<String> deleteFace(@PathVariable("faceId") Long faceId) {
        FaceEntity face = faceRepository.getFace(faceId);
        if (face == null) {
            throw new BaseException("人脸数据不存在");
        }

        return faceService.deleteFace(faceId);
    }

    @PostMapping("/{faceId}/match")
    public ApiResponse<String> match(@PathVariable("faceId") Long faceId) {
        faceService.matchFaceId(faceId);
        return ApiResponse.success("");
    }


    // 景区视频源素材列表
    @GetMapping("/{faceId}/contentList")
    public ApiResponse<List<ContentPageVO>> contentList(@PathVariable Long faceId) {
        List<ContentPageVO> contentPageVOS = faceService.faceContentList(faceId);
        return  ApiResponse.success(contentPageVOS);
    }

    // 绑定人脸
    @PostMapping("/{faceId}/bind")
    public ApiResponse<String> bind(@PathVariable Long faceId) {
        // dummy item
        faceService.matchFaceId(faceId, true);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{faceId}/status")
    public ApiResponse<FaceStatusResp> status(@PathVariable Long faceId) {
        return ApiResponse.success(faceService.getFaceStatus(faceId));
    }

    @GetMapping("/{faceId}/extraCheck")
    public ApiResponse<Boolean> hasExtraCheck(@PathVariable Long faceId) {
        return ApiResponse.success(faceService.checkHasExtraCheck(faceId));
    }

    @GetMapping("/{faceId}/queryOtherFace")
    public ApiResponse<List<FaceSampleEntity>> queryOtherFace(@PathVariable Long faceId) {
        return ApiResponse.success(faceService.getLowMatchedFaceSamples(faceId));
    }

    @PostMapping("/{faceId}/queryOtherFace")
    public ApiResponse<String> queryOtherFace(@PathVariable Long faceId, @RequestBody List<Long> faceIds) {
        faceService.matchCustomFaceId(faceId, faceIds);
        return ApiResponse.success("OK");
    }

    @PutMapping("/{faceId}/recognition")
    public ApiResponse<?> updateRecognition(@PathVariable Long faceId,
                                                                  @RequestBody FaceRecognitionUpdateReq req) {
        req.setFaceId(faceId);
        faceService.updateRecognition(req);
        return ApiResponse.success("OK");
    }

    @GetMapping("/{faceId}/recognition/detail")
    public ApiResponse<FaceRecognitionDetailVO> recognitionDetail(@PathVariable Long faceId) {
        return ApiResponse.success(faceService.getRecognitionDetail(faceId));
    }


}
