import React, { useCallback, useEffect, useRef } from 'react';
import { Box, Line } from 'folds';
import { useParams } from 'react-router-dom';
import { isKeyHotkey } from 'is-hotkey';
import { useAtomValue, useSetAtom } from 'jotai';
import { RoomView } from './RoomView';
import { MembersDrawer } from './MembersDrawer';
import { MediaGallery } from './MediaGallery';
import { mediaGalleryAtom } from '../../state/mediaGallery';
import { ScreenSize, useScreenSizeContext } from '../../hooks/useScreenSize';
import { useSetting } from '../../state/hooks/settings';
import { settingsAtom } from '../../state/settings';
import { PowerLevelsContextProvider, usePowerLevels } from '../../hooks/usePowerLevels';
import { useRoom } from '../../hooks/useRoom';
import { useKeyDown } from '../../hooks/useKeyDown';
import { markAsRead } from '../../utils/notifications';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useRoomMembers } from '../../hooks/useRoomMembers';
import { CallView } from '../call/CallView';
import { RoomViewHeader } from './RoomViewHeader';
import { callChatAtom } from '../../state/callEmbed';
import { CallChatView } from './CallChatView';
import { useCallEmbed } from '../../hooks/useCallEmbed';
import { useCallMembers, useCallSession } from '../../hooks/useCall';
import { roomIdToActiveThreadIdAtomFamily } from '../../state/room/thread';
import { ThreadPanel } from './thread';

export function Room() {
  const { eventId } = useParams();
  const room = useRoom();
  const mx = useMatrixClient();

  const callSession = useCallSession(room);
  const callMembers = useCallMembers(callSession);
  const callEmbed = useCallEmbed();

  const [isDrawer] = useSetting(settingsAtom, 'isPeopleDrawer');
  const activeThreadId = useAtomValue(roomIdToActiveThreadIdAtomFamily(room.roomId));
  const setActiveThreadId = useSetAtom(roomIdToActiveThreadIdAtomFamily(room.roomId));
  const galleryOpen = useAtomValue(mediaGalleryAtom);
  const setGalleryOpen = useSetAtom(mediaGalleryAtom);
  const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
  const screenSize = useScreenSizeContext();
  const powerLevels = usePowerLevels(room);
  const members = useRoomMembers(mx, room.roomId);
  const chat = useAtomValue(callChatAtom);
  useKeyDown(
    window,
    useCallback(
      (evt) => {
        if (isKeyHotkey('escape', evt)) {
          // Skip when a composer already consumed Escape (it preventDefaults).
          if (evt.defaultPrevented) return;
          // Skip while a thread panel is open: listener registration order
          // means this can run BEFORE the panel's own Escape handler, and the
          // user's intent there is "close the panel", not "mark room read".
          if (activeThreadId) return;
          markAsRead(mx, room.roomId, hideActivity);
        }
      },
      [mx, room.roomId, hideActivity, activeThreadId],
    ),
  );

  const callView = callEmbed?.roomId === room.roomId || room.isCallRoom() || callMembers.length > 0;

  // Thread panel and media gallery are mutually exclusive on every screen size:
  // opening one closes the other. Detect the just-opened transition so whichever
  // was opened most recently wins.
  const prevThreadRef = useRef(activeThreadId);
  const prevGalleryRef = useRef(galleryOpen);
  useEffect(() => {
    const threadJustOpened = Boolean(activeThreadId) && !prevThreadRef.current;
    const galleryJustOpened = galleryOpen && !prevGalleryRef.current;
    if (threadJustOpened && galleryOpen) {
      setGalleryOpen(false);
    } else if (galleryJustOpened && activeThreadId) {
      setActiveThreadId(null);
    }
    prevThreadRef.current = activeThreadId;
    prevGalleryRef.current = galleryOpen;
  }, [activeThreadId, galleryOpen, setGalleryOpen, setActiveThreadId]);

  // On non-desktop screens at most one right-side panel may show, priority
  // thread > gallery > members. On desktop thread + members may coexist while
  // thread + gallery stay mutually exclusive (via the effect above).
  const isDesktop = screenSize === ScreenSize.Desktop;
  const showThreadPanel = !callView && Boolean(activeThreadId);
  const showGallery = !callView && galleryOpen && (isDesktop || !activeThreadId);
  const showMembers = !callView && isDrawer && (isDesktop || (!activeThreadId && !galleryOpen));

  return (
    <PowerLevelsContextProvider value={powerLevels}>
      <Box grow="Yes">
        {callView && (screenSize === ScreenSize.Desktop || !chat) && (
          <Box grow="Yes" direction="Column">
            <RoomViewHeader callView />
            <Box grow="Yes">
              <CallView />
            </Box>
          </Box>
        )}
        {!callView && (
          <Box grow="Yes" direction="Column">
            <RoomViewHeader />
            <Box grow="Yes">
              <RoomView eventId={eventId} />
            </Box>
          </Box>
        )}

        {callView && chat && (
          <>
            {screenSize === ScreenSize.Desktop && (
              <Line variant="Background" direction="Vertical" size="300" />
            )}
            <CallChatView />
          </>
        )}
        {showGallery && (
          <>
            {screenSize === ScreenSize.Desktop && (
              <Line variant="Background" direction="Vertical" size="300" />
            )}
            <MediaGallery key={room.roomId} room={room} onClose={() => setGalleryOpen(false)} />
          </>
        )}
        {showThreadPanel && activeThreadId && (
          <>
            {screenSize === ScreenSize.Desktop && (
              <Line variant="Background" direction="Vertical" size="300" />
            )}
            <ThreadPanel
              key={`${room.roomId}${activeThreadId}`}
              room={room}
              threadId={activeThreadId}
              requestClose={() => setActiveThreadId(null)}
            />
          </>
        )}
        {showMembers && (
          <>
            {screenSize === ScreenSize.Desktop && (
              <Line variant="Background" direction="Vertical" size="300" />
            )}
            <MembersDrawer key={room.roomId} room={room} members={members} />
          </>
        )}
      </Box>
    </PowerLevelsContextProvider>
  );
}
