import React, { useMemo } from 'react';
import { Box, Text } from 'folds';
import { Room } from 'matrix-js-sdk';
import { useTranslation } from 'react-i18next';
import { useIsOneOnOne } from '../../hooks/useRoom';
import { useIsBridgedRoom } from '../../hooks/useIsBridgedRoom';
import { getBridgeNetworkName } from '../../utils/room';

// Context-aware empty-chat placeholder. The room class decides which copy the
// (hour-rotated) line is drawn from:
//   • native 1:1    → conversational `dm` prompt (+ E2E note if encrypted)
//   • native group  → conversational `group` prompt (+ E2E note if encrypted)
//   • bridged 1:1   → `dm` prompt + a muted line naming the bridge network
//   • bridged group → just the bridge-network line (no conversational prompt)
// The E2E note is shown only for native (non-bridged) encrypted rooms — a
// bridge leg (e.g. Telegram) is NOT end-to-end, so claiming so would mislead.
//
// Rotation mirrors the composer placeholder in `RoomInput.tsx`: the hour-of-day
// slot indexes a set, memoised against the room context so the line is stable
// while the user sits in one chat and only re-rolls on navigation. Keep these
// key lists in sync with `public/locales/{en,ru}.json` `Room.empty_*` entries.
const EMPTY_KEY_SETS = {
  dm: ['Room.empty_dm', 'Room.empty_dm_alt_1', 'Room.empty_dm_alt_2', 'Room.empty_dm_alt_3'],
  group: [
    'Room.empty_group',
    'Room.empty_group_alt_1',
    'Room.empty_group_alt_2',
    'Room.empty_group_alt_3',
  ],
  bridge: ['Room.empty_bridge', 'Room.empty_bridge_alt_1', 'Room.empty_bridge_alt_2'],
  bridgeGeneric: ['Room.empty_bridge_generic'],
} as const;

type EmptySetKind = keyof typeof EMPTY_KEY_SETS;

const pickByHour = (kind: EmptySetKind): string => {
  const set = EMPTY_KEY_SETS[kind];
  return set[new Date().getHours() % set.length];
};

export function EmptyTimeline({ room, roomId }: { room: Room; roomId: string }) {
  const { t } = useTranslation();
  const isOneOnOne = useIsOneOnOne();
  const bridged = useIsBridgedRoom(room);
  const network = bridged ? getBridgeNetworkName(room) : undefined;
  // E2E note only for native encrypted rooms — never for bridges (the remote
  // leg isn't end-to-end, so the claim wouldn't hold).
  const encrypted = !bridged && room.hasEncryptionStateEvent();

  const promptKind: EmptySetKind = isOneOnOne ? 'dm' : 'group';
  const bridgeKind: EmptySetKind = network ? 'bridge' : 'bridgeGeneric';

  const promptKey = useMemo(
    () => pickByHour(promptKind),
    // roomId is an intentional re-roll trigger on chat navigation, not read in
    // the callback body.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [roomId, promptKind]
  );
  const bridgeKey = useMemo(
    () => (bridged ? pickByHour(bridgeKind) : null),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [roomId, bridged, bridgeKind]
  );

  const bridgeLine = bridgeKey ? t(bridgeKey, { network }) : null;

  // bridged group → the bridge line stands alone as the primary line; every
  // other class leads with the conversational prompt.
  const bridgeLineIsPrimary = bridged && !isOneOnOne;
  const primary = bridgeLineIsPrimary ? bridgeLine : t(promptKey);
  let secondary: string | null = null;
  if (!bridgeLineIsPrimary) {
    if (bridged) secondary = bridgeLine;
    else if (encrypted) secondary = t('Room.empty_encrypted');
  }

  return (
    <Box
      grow="Yes"
      direction="Column"
      alignItems="Center"
      justifyContent="Center"
      gap="100"
      style={{ padding: '0 24px', textAlign: 'center' }}
    >
      <Text size="T300" priority="300">
        {primary}
      </Text>
      {secondary && (
        <Text size="T200" priority="300">
          {secondary}
        </Text>
      )}
    </Box>
  );
}
