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

import com.red.circle.live.app.listener.agora.callback.AgoraChannelBase;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelUserJoin;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelUserLeave;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelUserRoleChange;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelWebhook;
import com.red.circle.live.app.listener.agora.callback.AgoraChannelWebhookListener;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 声网监听实现.
 *
 * @author pengliang on 2023/12/29
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AgoraChannelWebhookImplListener implements AgoraChannelWebhookListener {

//  private final RedisService redisService;
//  private final LiveMicCommon liveMicCommon;
//  private final UserProfileClient userProfileClient;
//  private final RoomManagerClient roomManagerClient;
//  private final LiveMicCacheService liveMicCacheService;
//  private final LiveMicUserCacheService liveMicUserCacheService;

  @Override
  public void channelCreate(AgoraChannelWebhook webhook, AgoraChannelBase body) {

  }

  @Override
  public void channelDestroy(AgoraChannelWebhook webhook, AgoraChannelBase body) {

  }

  @Override
  public void broadcasterJoinChannel(AgoraChannelWebhook webhook, AgoraChannelUserJoin body) {

//    UserRoomProfile userRoomProfile = getUserRoomProfile(webhook, body, body.getUid());
//
//    if (Objects.isNull(userRoomProfile)) {
//      return;
//    }
//
//    liveMicCommon.onlineUserNoticeJoin(userRoomProfile.getRoomProfile().getId(),
//        userRoomProfile.getUserProfile().getId());
  }

  @Override
  public void broadcasterLeaveChannel(AgoraChannelWebhook webhook, AgoraChannelUserLeave body) {
    // leaveChannelProcess(webhook, body);
  }


  @Override
  public void audienceJoinChannel(AgoraChannelWebhook webhook, AgoraChannelUserJoin body) {

  }

  @Override
  public void audienceLeaveChannel(AgoraChannelWebhook webhook, AgoraChannelUserLeave body) {
    // leaveChannelProcess(webhook, body);
  }

  @Override
  public void clientRoleChangeToBroadcaster(AgoraChannelWebhook webhook,
      AgoraChannelUserRoleChange body) {

  }

  @Override
  public void clientRoleChangeToAudience(AgoraChannelWebhook webhook,
      AgoraChannelUserRoleChange body) {

  }

  /**
   * 退出频道处理.
   */
//  private void leaveChannelProcess(AgoraChannelWebhook webhook, AgoraChannelUserLeave body) {
//
//    UserRoomProfile userRoomProfile = getUserRoomProfile(webhook, body, body.getUid());
//
//    if (Objects.isNull(userRoomProfile)) {
//      return;
//    }
//
//    RoomProfileDTO roomProfile = userRoomProfile.getRoomProfile();
//    UserProfileDTO userProfile = userRoomProfile.getUserProfile();
//    Long roomId = roomProfile.getId();
//
//    liveMicCacheService.refreshNotActiveUser(roomId);
//    liveMicUserCacheService.refreshNotActiveUser(roomId);
//    liveMicUserCacheService.removeUser(roomProfile.getId(), userProfile.getId());
//
//    List<LiveMicrophone> liveMicrophones = liveMicCacheService.listLiveMicrophone(
//        roomId, userProfile.getId());
//    if (CollectionUtils.isEmpty(liveMicrophones)) {
//      return;
//    }
//
//    LiveMicrophone liveMicrophone = liveMicrophones.get(0);
//
//    liveMicCommon.tryMicOpsLock(roomProfile.getId(), liveMicrophone.getMicIndex(),
//        () -> {
//          log.warn("踢下麦克风: roomId={}, micIndex={}", roomProfile.getId(),
//              liveMicrophone.getMicIndex());
//          liveMicCacheService.goDown(roomProfile.getId(), liveMicrophone.getMicIndex());
//          liveMicCommon.micUserChangeNotice(roomProfile);
//        });
//  }

//  private Long getRoomId(String channelName) {
//
//    if (StringUtils.isBlank(channelName) || !channelName.matches(RegexConstant.NUMBER)) {
//      log.warn("错误的频道名称: {}", channelName);
//      return null;
//    }
//    return DataTypeUtils.toLong(channelName);
//  }
//
//  private UserRoomProfile getUserRoomProfile(AgoraChannelWebhook webhook, AgoraChannelBase body,
//      Long uid) {
//    if (Objects.isNull(webhook) || Objects.isNull(body)) {
//      log.warn("数据错误: {}, {}",
//          JacksonUtils.toJson(webhook),
//          JacksonUtils.toJson(body)
//      );
//      return null;
//    }
//
//    if (Objects.isNull(uid)) {
//      log.warn("数据错误, uid: {}", uid);
//      return null;
//    }
//
//    Long roomId = getRoomId(body.getChannelName());
//    if (Objects.isNull(roomId) || roomId <= 0) {
//      return null;
//    }
//
//    if (!checkProcess(webhook, body.getTs())) {
//      return null;
//    }
//
//    // log.warn("leaveChannelProcess: {}", JacksonUtils.toJson(webhook));
//
//    RoomProfileDTO roomProfile = roomManagerClient.getById(roomId).getBody();
//    if (Objects.isNull(roomProfile)) {
//      return null;
//    }
//
//    UserProfileDTO userProfile = userProfileClient.getByAccount(roomProfile.getSysOrigin(),
//        Objects.toString(uid)).getBody();
//    if (Objects.isNull(userProfile)) {
//      return null;
//    }
//
//    return new UserRoomProfile()
//        .setRoomProfile(roomProfile)
//        .setUserProfile(userProfile);
//  }

//  /**
//   * true 未处理, false 已处理.
//   */
//  private boolean checkProcess(AgoraChannelWebhook webhook, Long ts) {
//    return redisService.setIfAbsent("AGORA:" + webhook.getEventType() + ":" + ts, 1,
//        TimeUnit.MINUTES);
//  }


  @Data
  @Accessors(chain = true)
  class UserRoomProfile {

    private UserProfileDTO userProfile;
    private RoomProfileDTO roomProfile;

  }

}
