// app/DraggableWindows.tsx

'use client';

import React from 'react';
import { Rnd } from 'react-rnd';
import {
  AiOutlineClose,
  AiOutlineMinus,
  AiOutlineFullscreen,
} from 'react-icons/ai';
import { useWindowManager } from '../contexts/WindowManagerContext';
import { motion, AnimatePresence } from 'framer-motion';

const DraggableWindows: React.FC = () => {
  const {
    windows,
    windowStates,
    closeWindow,
    minimizeWindow,
    maximizeWindow,
    updateWindowState,
    bringToFront,
  } = useWindowManager();

  return (
    <div className="w-full h-full fixed top-0 left-0">
      {Object.keys(windows).map((name) => {
        const { Component } = windows[name];
        const state = windowStates[name];

        if (!Component) return null;

        const isVisible = state.visible && !state.minimized;

        return (
          <AnimatePresence key={name}>
            {isVisible && (
              <WindowWrapper
                key={name}
                name={name}
                state={state}
                bringToFront={bringToFront}
                updateWindowState={updateWindowState}
                minimizeWindow={minimizeWindow}
                maximizeWindow={maximizeWindow}
                closeWindow={closeWindow}
              >
                <Component />
              </WindowWrapper>
            )}
          </AnimatePresence>
        );
      })}
    </div>
  );
};

interface WindowWrapperProps {
  name: string;
  state: WindowState;
  bringToFront: (name: string) => void;
  updateWindowState: (name: string, newState: Partial<WindowState>) => void;
  minimizeWindow: (name: string) => void;
  maximizeWindow: (name: string) => void;
  closeWindow: (name: string) => void;
  children: React.ReactNode;
}

const WindowWrapper: React.FC<WindowWrapperProps> = ({
  name,
  state,
  bringToFront,
  updateWindowState,
  minimizeWindow,
  maximizeWindow,
  closeWindow,
  children,
}) => {
  const { windowStates } = useWindowManager();

  // Determine if the window is the topmost window
  const maxZIndex = Math.max(...Object.values(windowStates).map((w) => w.zIndex));
  const isTopWindow = state.zIndex === maxZIndex;

  // Determine box shadow based on zIndex
  const boxShadow = isTopWindow
    ? '0px 0px 10px rgba(0,0,0,0.5)'
    : '0px 0px 5px rgba(0,0,0,0.2)';

  // Remove the scale effect to fix blurriness
  // Use layout animations for smooth transitions
  const windowMotionProps = {
    layout: true,
    initial: { opacity: 0 },
    animate: { opacity: 1 },
    exit: { opacity: 0 },
    transition: { duration: 0.2 },
  };

  return (
    <Rnd
      size={{ width: state.width, height: state.height }}
      position={{ x: state.x, y: state.y }}
      onMouseDown={() => bringToFront(name)}
      onDragStart={() => bringToFront(name)}
      onResizeStart={() => bringToFront(name)}
      onDragStop={(e, d) => {
        updateWindowState(name, { x: d.x, y: d.y });
      }}
      onResizeStop={(e, direction, ref, delta, position) => {
        const newWidth = parseInt(ref.style.width, 10);
        const newHeight = parseInt(ref.style.height, 10);
        updateWindowState(name, {
          width: newWidth,
          height: newHeight,
          x: position.x,
          y: position.y,
        });
      }}
      minWidth={200}
      minHeight={100}
      cancel=".window-content"
      disableDragging={state.maximized}
      enableResizing={!state.maximized}
      bounds="parent"
      style={{ zIndex: state.zIndex }}
    >
      <motion.div
        {...windowMotionProps}
        className="bg-white border border-gray-500 flex flex-col h-full"
        style={{ boxShadow }}
        onMouseDown={() => bringToFront(name)}
      >
        {/* Window Bar */}
        <div
          className="flex items-center justify-between bg-gray-200 p-1"
          style={{ cursor: state.maximized ? 'default' : 'move' }}
        >
          <span className="pl-2">{name}</span>
          <div className="flex">
            <button
              onClick={(e) => {
                e.stopPropagation();
                minimizeWindow(name);
              }}
              className="p-1 hover:bg-gray-300"
            >
              <AiOutlineMinus />
            </button>
            <button
              onClick={(e) => {
                e.stopPropagation();
                maximizeWindow(name);
              }}
              className="p-1 hover:bg-gray-300"
            >
              <AiOutlineFullscreen />
            </button>
            <button
              onClick={(e) => {
                e.stopPropagation();
                closeWindow(name);
              }}
              className="p-1 hover:bg-gray-300"
            >
              <AiOutlineClose />
            </button>
          </div>
        </div>
        {/* Window Content */}
        <div className="flex-grow overflow-auto window-content">{children}</div>
      </motion.div>
    </Rnd>
  );
};

export default DraggableWindows;

ERRORS:
[{
	"resource": "/workspaces/gdrl-dev-testing-1/app/DraggableWindows.tsx",
	"owner": "typescript",
	"code": "2304",
	"severity": 8,
	"message": "Cannot find name 'WindowState'.",
	"source": "ts",
	"startLineNumber": 61,
	"startColumn": 10,
	"endLineNumber": 61,
	"endColumn": 21
},{
	"resource": "/workspaces/gdrl-dev-testing-1/app/DraggableWindows.tsx",
	"owner": "typescript",
	"code": "2304",
	"severity": 8,
	"message": "Cannot find name 'WindowState'.",
	"source": "ts",
	"startLineNumber": 63,
	"startColumn": 55,
	"endLineNumber": 63,
	"endColumn": 66
}]

Fix these erros. do not change functionality