package com.dji.sample.manage.controller;

import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.manage.model.dto.CapacityDeviceDTO;
import com.dji.sample.manage.model.dto.LiveTypeDTO;
import com.dji.sample.manage.service.ILiveStreamService;
import com.dji.sdk.common.HttpResultResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;

/**
 * @author sean.zhou
 * @version 0.1
 * @date 2021/11/19
 */

@RestController
@Slf4j
@RequestMapping("${url.manage.prefix}${url.manage.version}/live")
public class LiveStreamController {

    @Autowired
    private ILiveStreamService liveStreamService;

    @Autowired
    private ObjectMapper mapper;

    /**
     * Get live capability data of all drones in the current user's workspace from the database.
     * @param request
     * @return  live capability
     */
    @GetMapping("/capacity")
    public HttpResultResponse<List<CapacityDeviceDTO>> getLiveCapacity(HttpServletRequest request) {
        // Get information about the current user.
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);

        List<CapacityDeviceDTO> liveCapacity = liveStreamService.getLiveCapacity(customClaim.getWorkspaceId());

        return HttpResultResponse.success(liveCapacity);
    }

    /**
     * Get live capability data of all drones in the current user's workspace from the database.
     * @param request
     * @return  live capability
     */
    @GetMapping("/oneCapacity")
    public HttpResultResponse<CapacityDeviceDTO> getLiveCapacityByDeviceSn(HttpServletRequest request,
                                                                           @RequestParam(value = "device_sn") String deviceSn) {
        // Get information about the current user.
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);

        CapacityDeviceDTO liveCapacity = liveStreamService.getLiveCapacity(customClaim.getWorkspaceId(), deviceSn);

        return HttpResultResponse.success(liveCapacity);
    }

    /**
     * Get live capability data of all drones in the current user's workspace from the database.
     * @param request
     * @return  live capability
     */
    @GetMapping("/oneCapacity2")
    public HttpResultResponse<CapacityDeviceDTO> getLiveCapacityByDeviceSn2(HttpServletRequest request,
                                                                           @RequestParam(value = "device_sn") String deviceSn) {

        CapacityDeviceDTO liveCapacity = liveStreamService.getLiveCapacity2(deviceSn);

        return HttpResultResponse.success(liveCapacity);
    }

    /**
     * Live streaming according to the parameters passed in from the web side.
     * Server generates a unique sessionId and returns it in the response.
     * @param liveParam Live streaming parameters.
     * @return
     */
    @PostMapping("/streams/start")
    public HttpResultResponse liveStart(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStart(liveParam);
    }

    /**
     * 不需要权限 Live streaming according to the parameters passed in from the web side.
     * Server generates a unique sessionId and returns it in the response.
     * @param liveParam Live streaming parameters.
     * @return
     */
    @PostMapping("/streams/start2")
    public HttpResultResponse liveStart2(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStart(liveParam);
    }

    /**
     * Restart live stream: stop then start, and broadcast restart lifecycle events.
     * @param liveParam restart parameters (workspace_id, video_id required).
     * @return
     */
    @PostMapping("/streams/restart")
    public HttpResultResponse liveRestart(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveRestart(liveParam);
    }

    /**
     * Refresh live stream: frontend-controlled refresh flow.
     * Executes refreshStop (device stop without clearing sessions) + start + broadcast.
     * Other clients reuse session_id and wait for MQTT push to auto-recover.
     * @param liveParam refresh parameters (workspace_id, video_id, url_type required).
     * @return
     */
    @PostMapping("/streams/refresh")
    public HttpResultResponse liveRefresh(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveRefresh(liveParam);
    }

    /**
     * Session-aware stop: only removes the specified sessionId's viewer session.
     * Actual device stop is triggered only when all viewers have left.
     * @param liveParam Live streaming parameters (must include session_id from liveStart response).
     * @return
     */
    @PostMapping("/streams/stop")
    public HttpResultResponse liveStop(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveStopSession(liveParam.getVideoId(), liveParam.getSessionId());
    }

    /**
     * 不需要权限 Force stop: clears all viewer sessions and immediately stops the stream.
     * @param liveParam Live streaming parameters.
     * @return
     */
    @PostMapping("/streams/stop2")
    public HttpResultResponse liveStop2(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveForceStop(liveParam.getVideoId());
    }

    /**
     * Heartbeat to keep viewer session alive. Frontend should call every 10 seconds.
     * @param liveParam Live streaming parameters (videoId + session_id required).
     * @return
     */
    @PostMapping("/streams/heartbeat")
    public HttpResultResponse liveHeartbeat(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveHeartbeat(liveParam.getVideoId(), liveParam.getSessionId());
    }

    /**
     * Set the quality of the live streaming according to the parameters passed in from the web side.
     * @param liveParam Live streaming parameters.
     * @return
     */
    @PostMapping("/streams/update")
    public HttpResultResponse liveSetQuality(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveSetQuality(liveParam);
    }

    @PostMapping("/streams/switch")
    public HttpResultResponse liveLensChange(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveLensChange(liveParam);
    }

    @PostMapping("/streams/liveCameraChange")
    public HttpResultResponse liveCameraChange(@RequestBody LiveTypeDTO liveParam) {
        return liveStreamService.liveCameraChange(liveParam);
    }

}