package com.multictrl.modules.business.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.multictrl.common.config.SysConfig;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.constant.DeviceType;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.exception.RenException;
import com.multictrl.common.utils.CacheUtils;
import com.multictrl.modules.business.dto.live.LiveUrl;
import com.multictrl.modules.business.service.CacheService;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.LiveService;
import com.multictrl.modules.business.service.SrsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播管理
 *
 * @author Sdy
 * @since 1.0.0 2026/4/24
 */
@Service
@RequiredArgsConstructor
public class LiveServiceImpl implements LiveService {
    private final DJIBaseService djiBaseService;
    private final SrsService srsService;
    private final SysConfig sysConfig;
    private final CacheService cacheService;

    @Override
    public String startLive(String dockSn, Integer videoQuality, String videoType) {
        JSONObject data = new JSONObject();
        data.set("url_type", 1);
        if (videoType.equals(DeviceType.DOCK.getCode())) {
            data.set("video_id", getDockVideoId(dockSn));
            data.set("url", srsService.getDockPushLiveUrl(dockSn));
        } else {
            data.set("video_id", getUavVideoId(dockSn));
            data.set("url", srsService.getUavPushLiveUrl(dockSn));
        }
        data.set("video_quality", videoQuality);
        return djiBaseService.executeAndReturnResult(dockSn, "live_start_push", data);
    }

    @Override
    public String stopLive(String dockSn, String videoType) {
        JSONObject data = new JSONObject();
        if (videoType.equals(DeviceType.DOCK.getCode())) {
            data.set("video_id", getDockVideoId(dockSn));
        } else {
            data.set("video_id", getUavVideoId(dockSn));
        }
        return djiBaseService.executeAndReturnResult(dockSn, "live_stop_push", data);
    }


    @Override
    public String liveCameraChange(String dockSn, Integer cameraPosition) {
        JSONObject data = new JSONObject();
        data.set("camera_position", cameraPosition);
        data.set("video_id", getDockVideoId(dockSn));
        return djiBaseService.executeAndReturnResult(dockSn, "live_camera_change", data);
    }

    @Override
    public String liveLensChange(String dockSn, String videoType) {
        JSONObject data = new JSONObject();
        data.set("video_type", videoType);
        String result = djiBaseService.executeAndReturnResult(dockSn, "live_lens_change", data);
        CacheUtils.set(BusinessConstant.UAV_VIDEO_TYPE, videoType);
        return result;
    }

    @Override
    public String liveSetQuality(String dockSn, Integer videoQuality, String videoType) {
        JSONObject data = new JSONObject();
        if (videoType.equals(DeviceType.DOCK.getCode())) {
            data.set("video_id", getDockVideoId(dockSn));
        } else {
            data.set("video_id", getUavVideoId(dockSn));
        }
        data.set("video_quality", videoQuality);
        return djiBaseService.executeAndReturnResult(dockSn, "live_set_quality", data);
    }

    @Override
    public List<LiveUrl> getLiveUrl(String dockSn) {
        List<LiveUrl> liveUrls = new ArrayList<>();
        String protocol = BusinessConstant.HTTP_PROTOCOL;
        if (sysConfig.getIsSsl()) {
            protocol = BusinessConstant.HTTPS_PROTOCOL;
        }
        //webrtc、flv 都需要nginx做代理
        String webrtcUrl = protocol + sysConfig.getIp() + ":" + sysConfig.getPort() + "/rtc/v1/whep/?app=" + dockSn + "&stream=";
        String flvUrl = protocol + sysConfig.getIp() + ":" + sysConfig.getPort() + "/flv/" + dockSn;

        //机库
        LiveUrl dockLive = new LiveUrl();
        dockLive.setType(DeviceType.DOCK.getCode());
        dockLive.setWebRtcUrl(webrtcUrl + "dock.flv");
        dockLive.setFlvUrl(flvUrl + "dock.flv");
        dockLive.setRtmpUrl(srsService.getDockPushLiveUrl(dockSn));
        liveUrls.add(dockLive);

        //飞机
        LiveUrl uavLive = new LiveUrl();
        uavLive.setType(DeviceType.UAV.getCode());
        uavLive.setWebRtcUrl(webrtcUrl + "uav.flv");
        uavLive.setFlvUrl(flvUrl + "uav.flv");
        uavLive.setRtmpUrl(srsService.getDockPushLiveUrl(dockSn));
        liveUrls.add(uavLive);

        return liveUrls;
    }

    //获取机库video_id
    private String getDockVideoId(String dockSn) {
        String key = BusinessConstant.DOCK_VIDEO_ID + dockSn;
        String videoId = (String) CacheUtils.get(key);
        if (videoId == null) {
            String cache = cacheService.getCache(key);
            if (StrUtil.isBlank(cache)) {
                throw new RenException(ErrorCode.DOCK_VIDEO_ID_NOT_EXIST);
            } else {
                videoId = cache;
                CacheUtils.set(key, videoId);
            }
        }
        return videoId;
    }

    //获取飞机video_id
    private String getUavVideoId(String dockSn) {
        String key = BusinessConstant.UAV_VIDEO_ID + dockSn;
        String videoId = (String) CacheUtils.get(key);
        if (videoId == null) {
            String cache = cacheService.getCache(key);
            if (StrUtil.isBlank(cache)) {
                throw new RenException(ErrorCode.UAV_VIDEO_ID_NOT_EXIST);
            } else {
                videoId = cache;
                CacheUtils.set(key, videoId);
            }
        }
        return videoId;
    }
}
