package cn.skcks.docking.gb28181.wvp.sip.message.bye.request.request;

import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.cache.CacheUtil;
import cn.skcks.docking.gb28181.core.sip.listener.SipListener;
import cn.skcks.docking.gb28181.core.sip.message.processor.MessageProcessor;
import cn.skcks.docking.gb28181.core.sip.message.subscribe.GenericSubscribe;
import cn.skcks.docking.gb28181.sdp.GB28181SDPBuilder;
import cn.skcks.docking.gb28181.wvp.sip.response.SipResponseBuilder;
import cn.skcks.docking.gb28181.wvp.sip.sender.SipSender;
import cn.skcks.docking.gb28181.wvp.sip.subscribe.SipSubscribe;
import gov.nist.javax.sip.message.SIPRequest;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.sip.RequestEvent;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.util.EventObject;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Component
public class ByeRequestProcessor implements MessageProcessor {
    private final SipListener sipListener;

    private final SipSubscribe subscribe;

    private final SipSender sender;

    @PostConstruct
    @Override
    public void init() {
        sipListener.addRequestProcessor(Request.BYE, this);
    }

    @Override
    public void process(EventObject eventObject) {
        RequestEvent requestEvent = (RequestEvent) eventObject;
        SIPRequest request = (SIPRequest) requestEvent.getRequest();
        String callId = request.getCallId().getCallId();
        String key = GenericSubscribe.Helper.getKey(Request.BYE, callId);
        log.info("key {}", key);
        String ip = request.getLocalAddress().getHostAddress();
        String transport = request.getTopmostViaHeader().getTransport();
        Optional.ofNullable(subscribe.getByeSubscribe().getPublisher(key))
                .ifPresentOrElse(
                        publisher -> publisher.submit(request),
                        () -> {
                            RedisUtil.KeyOps.delete(CacheUtil.getKey(GB28181SDPBuilder.Action.PLAY.getAction(), callId));
                            sender.sendResponse(ip, transport, ((provider, ip1, port) ->
                                SipResponseBuilder.response(request, Response.OK, "OK")));
                        });
    }
}
