<template></template>

<script setup lang="ts">
import { onMounted, onUnmounted, watch } from 'vue';
import { TOAST_TYPE, TUIToast, useUIKit } from '@tencentcloud/uikit-base-component-vue3';
import {  useLiveState, useCoHostState, CoHostEvent, SeatUserInfo, useLiveSeatState, useLoginState, useBattleState, CoHostStatus, useCoGuestState } from 'tuikit-atomicx-vue3';
import { showNotification, hideNotification } from '../base-component/Notification';
import { TUIBattleInfo, TUIBattleUser, TUILiveBattleManagerEvents } from '@tencentcloud/tuiroom-engine-js';

const { loginUserInfo } = useLoginState();
const { currentLive } = useLiveState();
const {
  applicants,
  coHostStatus,
  subscribeEvent: subscribeCoHostEvent,
  unsubscribeEvent: unsubscribeCoHostEvent,
  acceptHostConnection,
  rejectHostConnection,
} = useCoHostState();
const { receivedCoGuestUserList, rejectCoGuestRequest } = useCoGuestState();
const { seatList } = useLiveSeatState();
const { acceptBattle, rejectBattle, subscribeEvent: subscribeBattleEvent, unsubscribeEvent: unsubscribeBattleEvent } = useBattleState();

const { t } = useUIKit();

function safeJsonParse(extensionInfo: string) {
  try {
    return JSON.parse(extensionInfo);
  } catch {
    return {};
  }
}

watch(() => receivedCoGuestUserList.value.length, () => {
  if(applicants.value.length > 0 && receivedCoGuestUserList.value.length > 0) {
    receivedCoGuestUserList.value.forEach((item) => {
      rejectCoGuestRequest({
        userId: item.userId,
      });
    });
    return;
  }
});

const handleCoHostRequestReceived = async ({ inviter, extensionInfo }: { inviter: SeatUserInfo, extensionInfo: string }) => {
  if(receivedCoGuestUserList.value.length > 0) {
    rejectHostConnection({
        liveId: inviter.liveId,
    });
    return;
  }
  const hasMoreSeatUser = seatList.value.filter((item) => item.userInfo?.userId).length > 1;
  const allSeatUserInCoGuest = seatList.value.filter((item) => item.userInfo?.roomId !== currentLive.value?.liveId).length === 0;
  if (hasMoreSeatUser && allSeatUserInCoGuest) {
    await rejectHostConnection({
      liveId: inviter.liveId,
    });
    return;
  }
  const extensionInfoObj = safeJsonParse(extensionInfo);
  const isBattle = extensionInfoObj.withBattle;
  showNotification({
    cancelText: t('Reject'),
    message: isBattle? t('Received battle invitation from userName', { userName: inviter.userName || inviter.userId }) : t('Co-host request received from user', { userName: inviter.userName || inviter.userId }),
    duration: extensionInfoObj.timeout,
    onAccept: () => {
      acceptHostConnection({
        liveId: inviter.liveId,
      });
      hideNotification();
    },
    onCancel: () => {
      rejectHostConnection({
        liveId: inviter.liveId,
      });
      hideNotification();
    },
    onTimeout: () => {
      hideNotification();
    },
  });
};

const handleCoHostUserLeft = ({ userInfo }: { userInfo: SeatUserInfo }) => {
  if (coHostStatus.value === CoHostStatus.Connected) {
    TUIToast({ type: TOAST_TYPE.INFO, message: t('Co-host user left event', { userName: userInfo.userName || userInfo.userId }) });
  }
};
const handleCoHostRequestCancelled = ({ inviter }: { inviter: SeatUserInfo }) => {
  hideNotification();
  TUIToast({ type: TOAST_TYPE.INFO, message: t('Co-host request cancelled by user', { userName: inviter.userName || inviter.userId }) });
}
const handleUserExitBattle = (eventInfo: { battleId: string; user: TUIBattleUser }) => {
  if (eventInfo.user.userId === loginUserInfo.value?.userId) {
    return;
  }
};
const onBattleRequestReceived = (eventInfo: { battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser }) => {
  hideNotification();
  showNotification({
    cancelText: t('Reject'),
    message: t('Received battle invitation from userName', { userName: eventInfo.inviter.userName || eventInfo.inviter.userId}),
    onAccept: () => {
      acceptBattle({
        battleId: eventInfo.battleInfo.battleId,
      });
      hideNotification();
    },
    onCancel: () => {
      rejectBattle({
        battleId: eventInfo.battleInfo.battleId,
      });
      hideNotification();
    },
    onTimeout: () => {
      hideNotification();
    },
  });
};
const onBattleRequestCancelled = (eventInfo: { battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser }) => {
  TUIToast({ type: TOAST_TYPE.INFO, message: t('Battle request cancelled by user', { userName: eventInfo.invitee.userName || eventInfo.invitee.userId}) });
  hideNotification();
};
let timeoutUsers: string[] = [];
let timeoutTimer: NodeJS.Timeout | null = null;

const onBattleRequestTimeout = (eventInfo: { battleInfo: TUIBattleInfo, inviter: TUIBattleUser, invitee: TUIBattleUser }) => {
  timeoutUsers.push(eventInfo.invitee.userName || eventInfo.invitee.userId);
  
  if (timeoutTimer) {
    clearTimeout(timeoutTimer);
  }
  
  timeoutTimer = setTimeout(() => {
    if (timeoutUsers.length === 1) {
      TUIToast({ 
        type: TOAST_TYPE.INFO, 
        message: t('Battle request timeout for user', { userName: timeoutUsers[0] }) 
      });
    } else {
      TUIToast({ 
        type: TOAST_TYPE.INFO, 
        message: t('Battle request timeout for multiple users', { userName: timeoutUsers.join('、') }) 
      });
    }
    
    timeoutUsers = [];
    timeoutTimer = null;
  }, 500);
};

onMounted(() => {
  subscribeCoHostEvent(CoHostEvent.onCoHostRequestReceived, handleCoHostRequestReceived);
  subscribeCoHostEvent(CoHostEvent.onCoHostRequestCancelled, handleCoHostRequestCancelled);
  subscribeCoHostEvent(CoHostEvent.onCoHostUserLeft, handleCoHostUserLeft);
  subscribeBattleEvent(TUILiveBattleManagerEvents.onUserExitBattle, handleUserExitBattle);
  subscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestReceived, onBattleRequestReceived);
  subscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestCancelled, onBattleRequestCancelled);
  subscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestTimeout, onBattleRequestTimeout);
});

onUnmounted(() => {
  unsubscribeCoHostEvent(CoHostEvent.onCoHostRequestReceived, handleCoHostRequestReceived);
  unsubscribeCoHostEvent(CoHostEvent.onCoHostRequestCancelled, handleCoHostRequestCancelled);
  unsubscribeCoHostEvent(CoHostEvent.onCoHostUserLeft, handleCoHostUserLeft);
  unsubscribeBattleEvent(TUILiveBattleManagerEvents.onUserExitBattle, handleUserExitBattle);
  unsubscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestReceived, onBattleRequestReceived);
  unsubscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestCancelled, onBattleRequestCancelled);
  unsubscribeBattleEvent(TUILiveBattleManagerEvents.onBattleRequestTimeout, onBattleRequestTimeout);
});
</script>

<style scoped lang="scss">

</style>