import { IAction, IPoint } from "@fullstackcraftllc/codevideo-types";
import { STANDARD_PAUSE_MS } from "src/constants/CodeVideoIDEConstants";
import { sleep } from "src/utils/sleep";
import { getCoordinatesOfEditor } from "../coordinateFunctions/getCoordinatesOfEditor";
import { getCoordinatesOfFileOrFolder } from "../coordinateFunctions/getCoordinatesOfFileOrFolder";
import { getCoordinatesOfTerminalInput } from "../coordinateFunctions/getCoordinatesOfTerminalInput";
import { parseCoordinatesFromMouseCoordinateAction } from "../coordinateFunctions/parseCoordinatesFromAction";
import { getCoordinatesByCodeVideoDataID } from "../coordinateFunctions/getCoordinatesByCodeVideoDataID";

export const getNewMousePosition = async (targetMousePosition: IPoint, currentAction: IAction, containerRef: React.RefObject<HTMLDivElement | null>) => {
    let newPosition = { x: targetMousePosition.x, y: targetMousePosition.y };

    // before continuing, wait a little bit so file explorer can update the DOM
    await sleep(STANDARD_PAUSE_MS);

    switch (currentAction.name) {
      // movement actions
      case 'mouse-move-file-explorer':
        newPosition = getCoordinatesByCodeVideoDataID('file-explorer', containerRef)
      case 'mouse-move-terminal':
        newPosition = getCoordinatesOfTerminalInput(containerRef)
        break;
      case 'mouse-move-editor':
        newPosition = getCoordinatesOfEditor(containerRef)
        break;
      case 'mouse-move-file-explorer-file':
      case 'mouse-move-file-explorer-folder':
        newPosition = getCoordinatesOfFileOrFolder(currentAction.value, containerRef)
        break;
      case 'mouse-move-to-coordinates-pixels':
        newPosition = parseCoordinatesFromMouseCoordinateAction(currentAction.value, containerRef)
        break;
      case 'mouse-move-to-coordinates-percent':
        const percentages = parseCoordinatesFromMouseCoordinateAction(currentAction.value, containerRef)
        // convert x and y percentage to pixels
        const rect = containerRef?.current?.getBoundingClientRect();
        if (rect) {
          newPosition = {
            x: rect.width * percentages.x,
            y: rect?.height * percentages.y
          }
        }
        break;
      // file explorer context menu movements
      case 'mouse-move-file-explorer-context-menu-create-new-file':
         newPosition = getCoordinatesByCodeVideoDataID("file-explorer-context-menu-new-file", containerRef)
        break;
      case 'mouse-move-file-explorer-context-menu-create-new-folder':
         newPosition = getCoordinatesByCodeVideoDataID("file-explorer-context-menu-new-folder", containerRef)
        break;
      // file or folder context menu movements
      case 'mouse-move-file-explorer-file-context-menu-rename':
         newPosition = getCoordinatesByCodeVideoDataID("file-explorer-file-context-menu-rename", containerRef)
        break;
      case 'mouse-move-file-explorer-file-context-menu-delete':
         newPosition = getCoordinatesByCodeVideoDataID("file-explorer-file-context-menu-delete", containerRef)
        break;
        case 'mouse-move-file-explorer-folder-context-menu-rename':
           newPosition = getCoordinatesByCodeVideoDataID("file-explorer-folder-context-menu-rename", containerRef)
          break;
        case 'mouse-move-file-explorer-folder-context-menu-delete':
           newPosition = getCoordinatesByCodeVideoDataID("file-explorer-folder-context-menu-delete", containerRef)
          break;
      // editor context menu movements
      // click actions
      // TODO: all click actions should be derived directly from the virtual layer!!!!
      // case 'mouse-right-click':
      //   // calculate if the mouse is over the file explorer plain, file explorer folder or file, editor tabs, editor, terminal, or terminal tabs
      //   switch (currentMouseLocation) {
      //     case 'file-explorer':
      //       setIsFileExplorerContextMenuVisible(true)
      //       break;
      //   }
      //   break;
      // case 'mouse-left-click':
      //   // any side effects here are handled by the virtual layer
      //   // in any left click event, all context menus should be closed
      //   setIsFileExplorerContextMenuVisible(false)
      //   setIsFileOrFolderContextMenuVisible(false)
    }

    return newPosition;
}