package com.ycwl.basic.controller.mobile;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.exception.CheckTokenException;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.mobile.goods.*;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.service.mobile.GoodsService;
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 java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/5 14:49
 * 成片（video）和源素材（source）都可作为商品，订单中可购买多个
 */
@RestController
@RequestMapping("/api/mobile/goods/v1")
// 商品相关接口
public class AppGoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FaceService faceService;

    // 商品列表
    @PostMapping("/goodsList")
    public ApiResponse<List<GoodsPageVO>> goodsList(@RequestBody GoodsReqQuery query) {
        JwtInfo worker = JwtTokenUtil.getWorker();
        Long userId = worker.getUserId();
        List<FaceRespVO> faceRespVOS = faceService.listByUser(userId, query.getScenicId());
        List<Long> faceIds = faceRespVOS.stream().map(FaceRespVO::getId).toList();
        return goodsService.listGoodsByFaceIdList(faceIds, query.getIsBuy(), query.getScenicId());
    }

    // 源素材(原片/照片)商品列表
    @PostMapping("/sourceGoodsList")
    public ApiResponse<List<GoodsDetailVO>> sourceGoodsList(@RequestBody GoodsReqQuery query) {
        List<GoodsDetailVO> goodsDetailVOS = goodsService.sourceGoodsList(query);
        return ApiResponse.success(goodsDetailVOS);
    }

    // 源素材(原片/照片)商品数量
    @PostMapping("/sourceGoodsCount")
    public ApiResponse<Integer> sourceGoodsCount(@RequestBody GoodsReqQuery query) {
        Integer count = goodsService.sourceGoodsCount(query);
        return ApiResponse.success(count);
    }

    @PostMapping("/sourceGoodsList/download")
    public ApiResponse<List<GoodsUrlVO>> sourceGoodsListDownload(@RequestBody GoodsReqQuery query) {
        List<GoodsUrlVO> goodsUrlList = goodsService.sourceGoodsListDownload(query);
        return ApiResponse.success(goodsUrlList);
    }


    // 成片vlog商品详情
    @GetMapping("/getVideoGoodsDetail/{videoId}")
    @IgnoreToken
    public ApiResponse<VideoGoodsDetailVO> videoGoodsDetail(@PathVariable("videoId") Long videoId) {
        try {
            JwtInfo worker = JwtTokenUtil.getWorker();
            return goodsService.videoGoodsDetail(worker.getUserId(), videoId);
        } catch (CheckTokenException e) {
            return goodsService.videoGoodsDetail(null, videoId);
        }
    }

    @GetMapping("/sourceGoods/{sourceId}")
    public ApiResponse<GoodsDetailVO> sourceGoodsInfo(@PathVariable("sourceId") Long sourceId) {
        return goodsService.sourceGoodsInfo(sourceId);
    }

    /**
     * 查询用户当前景区的视频合成任务状态
     *
     * @return 0没有任务 1 合成中 2 合成成功
     */
    // 查询用户当前景区的整体视频合成任务状态 0没有任务 1 合成中 2 合成成功
    @GetMapping("/getTaskStatus/")
    public ApiResponse<VideoTaskStatusVO> getAllTaskStatus() {
        JwtInfo worker = JwtTokenUtil.getWorker();
        return ApiResponse.success(goodsService.getAllTaskStatus(worker.getUserId()));
    }

    /**
     * 查询用户当前景区的具体模版视频合成任务状态
     *
     * @param templateId 模版id
     * @return 1 合成中 2 合成成功
     */
    // 查询用户当前景区的具体模版视频合成任务状态 1 合成中 2 合成成功
    @GetMapping("/task/face/{faceId}/template/{templateId}")
    public ApiResponse<VideoTaskStatusVO> getTemplateTaskStatus(@PathVariable("faceId") Long faceId, @PathVariable("templateId") Long templateId) {
        return ApiResponse.success(goodsService.getTaskStatusByTemplateId(faceId, templateId));
    }

    /**
     * 检查视频是否可更新
     *
     * @param videoId 视频ID
     * @return 视频更新检查结果
     */
    @GetMapping("/video/{videoId}/updateCheck")
    public ApiResponse<VideoUpdateCheckVO> checkVideoUpdate(@PathVariable("videoId") Long videoId) {
        VideoUpdateCheckVO result = goodsService.checkVideoUpdate(videoId);
        return ApiResponse.success(result);
    }
}
