package com.genersoft.iot.vmp.service;

import com.genersoft.iot.vmp.media.zlm.dto.hook.OnPublishHookParam;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Slf4j
@Data
@Service
@RequiredArgsConstructor
public class ZlmPublishHookService {
    @Qualifier("taskExecutor")
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    public interface ZlmPublishHookHandler {
        void handler();
    }

    @Getter(AccessLevel.PRIVATE)
    private ConcurrentMap<String, ConcurrentMap<String, ZlmPublishHookHandler>> handler = new ConcurrentHashMap<>();

    public ConcurrentMap<String, ZlmPublishHookHandler> getHandler(String app) {
        this.handler.putIfAbsent(app, new ConcurrentHashMap<>());
        return this.handler.get(app);
    }


    public void processEvent(OnPublishHookParam dto) {
        String app = dto.getApp();
        String streamId = dto.getStream();
        String ip = dto.getIp();
        log.debug("推流鉴权: app {}, streamId {}, ip {}", app, streamId, ip);

        ConcurrentMap<String, ZlmPublishHookHandler> handlers = getHandler(app);
        Optional.ofNullable(handlers.remove(streamId)).ifPresent(handler -> {
            taskExecutor.execute(handler::handler);
        });
    }
}
