import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { MessageEvent, StateEvent } from '../../../../types/matrix/room';
import { PermissionGroup } from '../../common-settings/permissions';

export const usePermissionGroups = (isCallRoom: boolean): PermissionGroup[] => {
  const { t } = useTranslation();
  const groups: PermissionGroup[] = useMemo(() => {
    const messagesGroup: PermissionGroup = {
      name: t('RoomSettings.perm_messages'),
      items: [
        {
          location: {
            key: MessageEvent.RoomMessage,
          },
          name: t('RoomSettings.perm_send_messages'),
        },
        {
          location: {
            key: MessageEvent.Sticker,
          },
          name: t('RoomSettings.perm_send_stickers'),
        },
        {
          location: {
            key: MessageEvent.Reaction,
          },
          name: t('RoomSettings.perm_send_reactions'),
        },
        {
          location: {
            notification: true,
            key: 'room',
          },
          name: t('RoomSettings.perm_ping_room'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomPinnedEvents,
          },
          name: t('RoomSettings.perm_pin_messages'),
        },
        {
          location: {},
          name: t('RoomSettings.perm_other_message_events'),
        },
      ],
    };

    const callSettingsGroup: PermissionGroup = {
      name: t('RoomSettings.perm_calls'),
      items: [
        {
          location: {
            state: true,
            key: StateEvent.GroupCallMemberPrefix,
          },
          name: t('RoomSettings.perm_join_call'),
        },
      ],
    };

    const moderationGroup: PermissionGroup = {
      name: t('RoomSettings.perm_moderation'),
      items: [
        {
          location: {
            action: true,
            key: 'invite',
          },
          name: t('RoomSettings.perm_invite'),
        },
        {
          location: {
            action: true,
            key: 'kick',
          },
          name: t('RoomSettings.perm_kick'),
        },
        {
          location: {
            action: true,
            key: 'ban',
          },
          name: t('RoomSettings.perm_ban'),
        },
        {
          location: {
            action: true,
            key: 'redact',
          },
          name: t('RoomSettings.perm_delete_others_messages'),
        },
        {
          location: {
            key: MessageEvent.RoomRedaction,
          },
          name: t('RoomSettings.perm_delete_self_messages'),
        },
      ],
    };

    const roomOverviewGroup: PermissionGroup = {
      name: t('RoomSettings.perm_room_overview'),
      items: [
        {
          location: {
            state: true,
            key: StateEvent.RoomAvatar,
          },
          name: t('RoomSettings.perm_room_avatar'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomName,
          },
          name: t('RoomSettings.perm_room_name'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomTopic,
          },
          name: t('RoomSettings.perm_room_topic'),
        },
      ],
    };

    const roomSettingsGroup: PermissionGroup = {
      name: t('RoomSettings.perm_settings'),
      items: [
        {
          location: {
            state: true,
            key: StateEvent.RoomJoinRules,
          },
          name: t('RoomSettings.perm_change_room_access'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomCanonicalAlias,
          },
          name: t('RoomSettings.perm_publish_address'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomPowerLevels,
          },
          name: t('RoomSettings.perm_change_all_permission'),
        },
        {
          location: {
            state: true,
            key: StateEvent.PowerLevelTags,
          },
          name: t('RoomSettings.perm_edit_power_levels'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomEncryption,
          },
          name: t('RoomSettings.perm_enable_encryption'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomHistoryVisibility,
          },
          name: t('RoomSettings.perm_history_visibility'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomTombstone,
          },
          name: t('RoomSettings.perm_upgrade_room'),
        },
        {
          location: {
            state: true,
          },
          name: t('RoomSettings.perm_other_settings'),
        },
      ],
    };

    const otherSettingsGroup: PermissionGroup = {
      name: t('RoomSettings.perm_other'),
      items: [
        {
          location: {
            state: true,
            key: StateEvent.PoniesRoomEmotes,
          },
          name: t('RoomSettings.perm_manage_emojis_stickers'),
        },
        {
          location: {
            state: true,
            key: StateEvent.RoomServerAcl,
          },
          name: t('RoomSettings.perm_change_server_acls'),
        },
        {
          location: {
            state: true,
            key: 'im.vector.modular.widgets',
          },
          name: t('RoomSettings.perm_modify_widgets'),
        },
      ],
    };

    return [
      messagesGroup,
      ...(isCallRoom ? [callSettingsGroup] : []),
      moderationGroup,
      roomOverviewGroup,
      roomSettingsGroup,
      otherSettingsGroup,
    ];
  }, [isCallRoom, t]);

  return groups;
};
