import React from 'react';
import { useWindowManager } from '../../contexts/WindowManagerContext';
import { ChatWindow } from './ChatWindow';
import { StatsWindow } from './StatsWindow';
import { CharacterWindow } from './CharacterWindow';
import { InventoryWindow } from './InventoryWindow';
import { RadarWindow } from './RadarWindow';
import { CombatStatsWindow } from './CombatStatsWindow';
import { IssuesWindow } from './IssuesWindow';
import { VitalSharingWindow } from './VitalSharingWindow';
import type { CharacterState } from '../../types';

interface Props {
  characters: Map<string, CharacterState>;
  chatMessages: Map<string, Array<{ text: string; color?: number; timestamp: string }>>;
  nearbyObjects: Map<string, any>;
  socket: WebSocket | null;
}

export const WindowRenderer: React.FC<Props> = ({ characters, chatMessages, nearbyObjects, socket }) => {
  const { windows } = useWindowManager();

  return (
    <>
      {windows.map(w => {
        const charName = w.charName ?? '';
        const prefix = w.id.split('-')[0];

        switch (prefix) {
          case 'chat':
            return <ChatWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex}
                     messages={chatMessages.get(charName) ?? []} socket={socket} />;
          case 'stats':
            return <StatsWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex} />;
          case 'char':
            return <CharacterWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex}
                     vitals={characters.get(charName)?.vitals ?? undefined} />;
          case 'inv':
            return <InventoryWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex} />;
          case 'radar':
            return <RadarWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex}
                     socket={socket} radarData={nearbyObjects.get(charName) ?? null} />;
          case 'combat':
            return <CombatStatsWindow key={w.id} id={w.id} charName={charName} zIndex={w.zIndex} />;
          case 'issues':
            return <IssuesWindow key={w.id} id={w.id} zIndex={w.zIndex} />;
          case 'vitalsharing':
            return <VitalSharingWindow key={w.id} id={w.id} zIndex={w.zIndex} />;
          default:
            return null;
        }
      })}
    </>
  );
};
