package cn.skcks.docking.gb28181.wvp.proxy;

import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.wvp.dto.common.GeneralTimeReq;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsReq;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsResp;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceTreeReq;
import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceTreeResp;
import cn.skcks.docking.gb28181.wvp.dto.download.DownloadStartReq;
import cn.skcks.docking.gb28181.wvp.dto.login.WvpLoginReq;
import cn.skcks.docking.gb28181.wvp.dto.login.WvpLoginResp;
import cn.skcks.docking.gb28181.wvp.dto.media.proxy.AddDownloadTaskReq;
import cn.skcks.docking.gb28181.wvp.dto.media.proxy.GetDownloadTaskReq;
import cn.skcks.docking.gb28181.wvp.dto.media.proxy.GetDownloadTaskResp;
import cn.skcks.docking.gb28181.wvp.dto.record.QueryRecordReq;
import cn.skcks.docking.gb28181.wvp.dto.record.QueryRecordResp;
import cn.skcks.docking.gb28181.wvp.dto.stream.StreamContent;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@FeignClient(name = "wvpProxyClient", url = "${proxy.wvp.url}")
public interface WvpProxyClient {
    @GetMapping(value = "/api/user/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JsonResponse<WvpLoginResp> login(@SpringQueryMap WvpLoginReq req);

    @GetMapping("/api/device/query/tree/channel/{deviceId}")
    JsonResponse<GetDeviceTreeResp> getDeviceTree(@RequestHeader("access-token") String token,
                                                  @PathVariable String deviceId,
                                                  @SpringQueryMap GetDeviceTreeReq req);

    @GetMapping("/api/device/query/devices/{deviceId}/channels")
    JsonResponse<GetDeviceChannelsResp> getDeviceChannels(@RequestHeader("access-token") String token,
                                                          @PathVariable String deviceId,
                                                          @SpringQueryMap GetDeviceChannelsReq req);

    @GetMapping("/api/gb_record/query/{deviceId}/{channelId}")
    JsonResponse<QueryRecordResp> queryRecord(@RequestHeader("access-token") String token,
                                              @PathVariable String deviceId,
                                              @PathVariable String channelId,
                                              @SpringQueryMap QueryRecordReq req);

    // /api/playback/start/44050100001180000001/44050100001310000001?startTime=2023-09-07%2000:20:01&endTime=2023-09-07%2000:25:01
    @GetMapping("/api/playback/start/{deviceId}/{channelId}")
    JsonResponse<StreamContent> playbackStart(@RequestHeader("access-token") String token,
                                              @PathVariable String deviceId,
                                              @PathVariable String channelId,
                                              @SpringQueryMap GeneralTimeReq req);

    @GetMapping("/api/playback/stop/{deviceId}/{channelId}/{stream}")
    JsonResponse<StreamContent> playbackStop(@RequestHeader("access-token") String token,
                                              @PathVariable String deviceId,
                                              @PathVariable String channelId,
                                              @PathVariable String stream);

    // /api/playback/stop/44050100001180000001/44050100001310000001/44050100001180000001_44050100001310000001_20230908083001_20230908083501
    @GetMapping("/api/gb_record/download/start/{deviceId}/{channelId}")
    JsonResponse<StreamContent> downloadStart(@RequestHeader("access-token") String token,
                                                  @PathVariable String deviceId,
                                                  @PathVariable String channelId,
                                                  @SpringQueryMap DownloadStartReq req);

    // /api/gb_record/download/progress/44050100001180000001/44050100001310000001/59777645
    @GetMapping("/api/gb_record/download/progress/{deviceId}/{channelId}/{stream}")
    JsonResponse<StreamContent> downloadProgress(@RequestHeader("access-token") String token,
                                                  @PathVariable String deviceId,
                                                  @PathVariable String channelId,
                                                  @PathVariable String stream);

    /**
     * 停止下载, 部分设备会出现长时间卡住不动 且 wvp 会出现 100 错误, 故调用即可无需理会返回值
     * @param token 认证token
     * @param deviceId 国标设备id/下级平台id
     * @param channelId 通道id
     * @param stream downloadStart中的 stream
     */
    // /api/gb_record/download/stop/44050100001180000001/44050100001310000001/59777645
    @GetMapping("/api/gb_record/download/stop/{deviceId}/{channelId}/{stream}")
    JsonResponse<Void> downloadStop(@RequestHeader("access-token") String token,
                                                 @PathVariable String deviceId,
                                                 @PathVariable String channelId,
                                                 @PathVariable String stream);

    /**
     * 添加视频裁剪合并 任务 到 wvp-assist 服务, 返回 taskId 用于后续操作
     * @param token 认证token
     * @param mediaServerId 流媒体服务id, 从 downloadStart/downloadProgress 获得
     * @return 返回 taskId 用于后续操作
     */
    // /record_proxy/your_server_id/api/record/file/download/task/add?app=rtp&stream=59777645
    @GetMapping("/record_proxy/{mediaServerId}/api/record/file/download/task/add")
    JsonResponse<String> addDownloadTask2MediaServer(@RequestHeader("access-token") String token,
                                                     @PathVariable String mediaServerId,
                                                     @SpringQueryMap AddDownloadTaskReq req);

    /**
     * 获取视频裁剪合并任务列表
     * <p>如果 GetDownloadTaskResp.isEnd = true, 就是已经处理完成, 可以直接用里面的 playUrl / downloadUrl下载</p>
     * @param token 认证token
     * @param mediaServerId 流媒体服务id, 从 downloadStart/downloadProgress 获得
     * @param req GetDownloadTaskReq 请求参数
     * @return GetDownloadTaskResp 任务列表
     */
    // /record_proxy/your_server_id/api/record/file/download/task/list?app=rtp&stream=59777645&taskId=0490d767d94ce20aedce57c862b6bfe9&isEnd=true
    @GetMapping("/record_proxy/{mediaServerId}/api/record/file/download/task/list")
    JsonResponse<List<GetDownloadTaskResp>> getDownloadTask4MediaServer(@RequestHeader("access-token") String token,
                                                                        @PathVariable String mediaServerId,
                                                                        @SpringQueryMap GetDownloadTaskReq req);

    @GetMapping("/api/play/start/{deviceId}/{channelId}")
    JsonResponse<StreamContent> playStart(@RequestHeader("access-token") String token,
                                             @PathVariable String deviceId,
                                             @PathVariable String channelId,
                                          @RequestParam boolean isSubStream);
    @GetMapping("/api/play/stop/{deviceId}/{channelId}")
    JsonResponse<Void> playStop(@RequestHeader("access-token") String token,
                                @PathVariable String deviceId,
                                @PathVariable String channelId,
                                @RequestParam boolean isSubStream);
}
