package com.red.circle.live.app.listener.agora.callback;

import com.red.circle.tool.core.json.JacksonUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author pengliang on 2023/12/29
 */
@Component
@RequiredArgsConstructor
public class AgoraChannelWebhookFactory {

  private final AgoraChannelWebhookListener agoraChannelWebhookListener;

  public void execute(byte[] body) {
    AgoraChannelWebhook webhook = AgoraChannelWebhook.serializable(body);
    if (Objects.isNull(webhook)) {
      return;
    }

    if (Objects.equals(webhook.getEventType(), 101)) {
      agoraChannelWebhookListener.channelCreate(webhook, webhook.toBody(AgoraChannelBase.class));
      return;
    }

    if (Objects.equals(webhook.getEventType(), 102)) {
      agoraChannelWebhookListener.channelDestroy(webhook, webhook.toBody(AgoraChannelBase.class));
      return;
    }

    if (Objects.equals(webhook.getEventType(), 103)) {
      agoraChannelWebhookListener.broadcasterJoinChannel(webhook,
          webhook.toBody(AgoraChannelUserJoin.class)
      );
      return;
    }

    if (Objects.equals(webhook.getEventType(), 104)) {
      agoraChannelWebhookListener.broadcasterLeaveChannel(webhook,
          webhook.toBody(AgoraChannelUserLeave.class)
      );
      return;
    }

    if (Objects.equals(webhook.getEventType(), 105)) {
      agoraChannelWebhookListener.audienceJoinChannel(webhook,
          webhook.toBody(AgoraChannelUserJoin.class)
      );
      return;
    }

    if (Objects.equals(webhook.getEventType(), 106)) {
      agoraChannelWebhookListener.audienceLeaveChannel(webhook,
          webhook.toBody(AgoraChannelUserLeave.class)
      );
      return;
    }

    if (Objects.equals(webhook.getEventType(), 111)) {
      agoraChannelWebhookListener.clientRoleChangeToBroadcaster(webhook,
          webhook.toBody(AgoraChannelUserRoleChange.class)
      );
      return;
    }

    if (Objects.equals(webhook.getEventType(), 112)) {
      agoraChannelWebhookListener.clientRoleChangeToAudience(webhook,
          webhook.toBody(AgoraChannelUserRoleChange.class)
      );
      return;
    }

    throw new IllegalArgumentException(
        "eventType not found: " + webhook.getEventType() + ", data=" + JacksonUtils.toJson(
            webhook));
  }


}
