import {useTranslation} from 'react-i18next';
import i18next from 'i18next';
import {persistedUserId} from '@services/localStorage';
import {getListFormatter} from '@shared/utils';

import {Block, BlockType} from '../interfaces';

interface Props {
  block?: Block;
  emitterUserId?: number;
  emitterUsername?: string;
  isGroup?: boolean;
}

export const useBlockTextHandler = ({
  block,
  emitterUserId,
  emitterUsername,
}: Props): string => {
  const {t} = useTranslation();

  const isCurrentUser = emitterUserId === persistedUserId;

  if (!block || !emitterUserId || !emitterUsername) {
    return '';
  }

  switch (block.type) {
    case BlockType.ChannelCreated: {
      return isCurrentUser
        ? t('chat.system_messages.channel_created_by_user')
        : t('chat.system_messages.channel_created_by_others', {
            emitter: emitterUsername,
          });
    }
    case BlockType.ChannelJoined: {
      const peopleToShow: string[] = block.usernames!.slice(0, 3);
      const currentUserWasAdded = block.user_ids!.includes(persistedUserId!);
      const restPeople = block.usernames!.slice(3);
      if (restPeople.length > 0) {
        peopleToShow.push(
          t('chat.system_messages.more_collaborators', {
            count: block.user_ids!.length - 3,
          }),
        );
      }

      const formatter = getListFormatter(i18next.language);

      const people = formatter.format(peopleToShow);
      return currentUserWasAdded
        ? t('chat.system_messages.channel_joined_current_user', {
            emitter: emitterUsername,
          })
        : isCurrentUser
        ? t('chat.system_messages.channel_joined_by_user', {
            user: people,
          })
        : t('chat.system_messages.channel_joined_by_others', {
            emitter: emitterUsername,
            users: people,
          });
    }
    case BlockType.ChannelLeft: {
      const hasLeftGroup = emitterUserId === block.user_id;
      const currentUserLeft = persistedUserId === block.user_id;
      const currentUserEmitter = persistedUserId === emitterUserId;
      return hasLeftGroup
        ? currentUserLeft
          ? t('chat.system_messages.channel_left_by_user')
          : t('chat.system_messages.channel_left_by_others', {
              emitter: block.username!,
            })
        : currentUserEmitter
        ? t('chat.system_messages.channel_kick_by_user', {
            user: block.username!,
          })
        : t('chat.system_messages.channel_kick_by_others', {
            emitter: emitterUsername,
            users: block.username!,
          });
    }
    case BlockType.ChannelPictureChanged: {
      return isCurrentUser
        ? t('chat.system_messages.channel_picture_changed_by_user')
        : t('chat.system_messages.channel_picture_changed_by_others', {
            emitter: emitterUsername,
          });
    }
    case BlockType.ChannelRenamed: {
      return isCurrentUser
        ? t('chat.system_messages.channel_renamed_by_user', {
            old: block.old_name,
            new: block.new_name,
          })
        : t('chat.system_messages.channel_renamed_by_others', {
            emitter: emitterUsername,
            old: block.old_name,
            new: block.new_name,
          });
    }
    case BlockType.ChannelDescriptionChanged: {
      return isCurrentUser
        ? t('chat.system_messages.channel_description_changed_by_user')
        : t('chat.system_messages.channel_description_changed_by_others', {
            emitter: emitterUsername,
          });
    }
    case BlockType.CallInitialized:
      return t('chat.system_messages.call_initialized', {
        emitter: `${block.member.first_name} ${block.member.last_name}`,
      });
    case BlockType.CallEnded:
      return block.reason === 'NO_RESPONSE'
        ? t(
            `chat.system_messages.${
              isCurrentUser ? 'call_missed' : 'call_missed_by_others'
            }`,
          )
        : block.reason === 'ALL_PARTICIPANTS_LEFT'
        ? t('chat.system_messages.call_ended')
        : block.reason === 'REJECTED'
        ? t('chat.system_messages.call_refused')
        : t('chat.system_messages.call_ended'); // Fallback for other CallEnded reasons
    case BlockType.MessagePinAdded:
      return isCurrentUser
        ? t('chat.system_messages.message_pin_added_by_user')
        : t('chat.system_messages.message_pin_added_by_other', {
            emitter: emitterUsername,
          });
    default:
      return '';
  }
};
