import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const { MockViewportHost, viewportHostInstances } = vi.hoisted(() => {
  const viewportHostInstances: Array<{
    mount: ReturnType<typeof vi.fn>;
    dispose: ReturnType<typeof vi.fn>;
    updateWorld: ReturnType<typeof vi.fn>;
    updateSimulation: ReturnType<typeof vi.fn>;
    updateAssets: ReturnType<typeof vi.fn>;
    updateDocument: ReturnType<typeof vi.fn>;
    updateSelection: ReturnType<typeof vi.fn>;
    setPanelId: ReturnType<typeof vi.fn>;
    setRenderEnabled: ReturnType<typeof vi.fn>;
    setViewMode: ReturnType<typeof vi.fn>;
    setDisplayMode: ReturnType<typeof vi.fn>;
    setGridVisible: ReturnType<typeof vi.fn>;
    setCameraState: ReturnType<typeof vi.fn>;
    setBrushSelectionChangeHandler: ReturnType<typeof vi.fn>;
    setCameraStateChangeHandler: ReturnType<typeof vi.fn>;
    setCreationPreviewChangeHandler: ReturnType<typeof vi.fn>;
    setCreationCommitHandler: ReturnType<typeof vi.fn>;
    setTransformSessionChangeHandler: ReturnType<typeof vi.fn>;
    setTransformPreviewChangeHandler: ReturnType<typeof vi.fn>;
    setTransformCommitHandler: ReturnType<typeof vi.fn>;
    setTransformCancelHandler: ReturnType<typeof vi.fn>;
    setWhiteboxHoverLabelChangeHandler: ReturnType<typeof vi.fn>;
    setWhiteboxSelectionMode: ReturnType<typeof vi.fn>;
    setWhiteboxSnapSettings: ReturnType<typeof vi.fn>;
    setToolMode: ReturnType<typeof vi.fn>;
    setTerrainLodGridVisibleTerrainIds: ReturnType<typeof vi.fn>;
    setCreationPreview: ReturnType<typeof vi.fn>;
    setTransformSession: ReturnType<typeof vi.fn>;
    focusSelection: ReturnType<typeof vi.fn>;
  }> = [];

  class MockViewportHost {
    mount = vi.fn();
    dispose = vi.fn();
    updateWorld = vi.fn();
    updateSimulation = vi.fn();
    updateAssets = vi.fn();
    updateDocument = vi.fn();
    updateSelection = vi.fn();
    setPanelId = vi.fn();
    setRenderEnabled = vi.fn();
    setViewMode = vi.fn();
    setDisplayMode = vi.fn();
    setGridVisible = vi.fn();
    setCameraState = vi.fn();
    setBrushSelectionChangeHandler = vi.fn();
    setCameraStateChangeHandler = vi.fn();
    setCreationPreviewChangeHandler = vi.fn();
    setCreationCommitHandler = vi.fn();
    setTransformSessionChangeHandler = vi.fn();
    setTransformPreviewChangeHandler = vi.fn();
    setTransformCommitHandler = vi.fn();
    setTransformCancelHandler = vi.fn();
    setWhiteboxHoverLabelChangeHandler = vi.fn();
    setWhiteboxSelectionMode = vi.fn();
    setWhiteboxSnapSettings = vi.fn();
    setToolMode = vi.fn();
    setTerrainLodGridVisibleTerrainIds = vi.fn();
    setCreationPreview = vi.fn();
    setTransformSession = vi.fn();
    focusSelection = vi.fn();

    constructor() {
      viewportHostInstances.push(this);
    }
  }

  return {
    MockViewportHost,
    viewportHostInstances
  };
});

vi.mock("../../src/viewport-three/viewport-host", () => ({
  ViewportHost: MockViewportHost
}));

vi.mock("../../src/assets/project-asset-storage", () => ({
  getBrowserProjectAssetStorageAccess: vi.fn(async () => ({
    storage: null,
    diagnostic: null
  }))
}));

import { App } from "../../src/app/App";
import { createEditorStore } from "../../src/app/editor-store";

describe("Terrain foundation", () => {
  beforeEach(() => {
    viewportHostInstances.length = 0;
    vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(
      () => ({}) as never
    );
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("creates terrain from the Add menu and surfaces it through outliner selection and inspector summary", async () => {
    const store = createEditorStore();

    render(<App store={store} />);

    await waitFor(() => {
      expect(viewportHostInstances.length).toBeGreaterThan(0);
    });

    fireEvent.click(screen.getByRole("button", { name: "Add" }));
    fireEvent.click(await screen.findByTestId("add-menu-terrain"));

    await waitFor(() => {
      expect(store.getState().selection).toEqual({
        kind: "terrains",
        ids: [Object.keys(store.getState().document.terrains)[0]]
      });
      expect(Object.keys(store.getState().document.terrains)).toHaveLength(1);
    });

    const createdTerrain = Object.values(store.getState().document.terrains)[0];

    if (createdTerrain === undefined) {
      throw new Error("Expected the created terrain to exist.");
    }

    expect(
      screen.getByTestId(`outliner-terrain-${createdTerrain.id}`)
    ).toBeInTheDocument();
    expect(
      screen.getByText(
        "Terrain editing stays inspector-driven. Arm a brush here, then drag on the selected terrain in the viewport."
      )
    ).toBeInTheDocument();
    expect(screen.getAllByText(/9 x 9 samples/).length).toBeGreaterThan(0);
  });

  it("updates selected terrain grid settings and collision from the inspector", async () => {
    const store = createEditorStore();

    render(<App store={store} />);

    await waitFor(() => {
      expect(viewportHostInstances.length).toBeGreaterThan(0);
    });

    fireEvent.click(screen.getByRole("button", { name: "Add" }));
    fireEvent.click(await screen.findByTestId("add-menu-terrain"));

    await waitFor(() => {
      expect(Object.keys(store.getState().document.terrains)).toHaveLength(1);
    });

    const createdTerrain = Object.values(store.getState().document.terrains)[0];

    if (createdTerrain === undefined) {
      throw new Error("Expected the created terrain to exist.");
    }

    fireEvent.change(screen.getByTestId("terrain-grid-sample-count-x"), {
      target: { value: "5" }
    });
    fireEvent.change(screen.getByTestId("terrain-grid-sample-count-z"), {
      target: { value: "7" }
    });
    fireEvent.change(screen.getByTestId("terrain-grid-cell-size"), {
      target: { value: "2" }
    });
    fireEvent.click(screen.getByTestId("terrain-grid-apply"));

    await waitFor(() => {
      const updatedTerrain = store.getState().document.terrains[createdTerrain.id];

      expect(updatedTerrain?.sampleCountX).toBe(5);
      expect(updatedTerrain?.sampleCountZ).toBe(7);
      expect(updatedTerrain?.cellSize).toBe(2);
    });

    fireEvent.click(screen.getByTestId("terrain-collision-enabled"));

    await waitFor(() => {
      expect(
        store.getState().document.terrains[createdTerrain.id]?.collisionEnabled
      ).toBe(false);
    });
  });

  it("keeps terrain LoD grid hidden by default and exposes an inspector toggle", async () => {
    const store = createEditorStore();

    render(<App store={store} />);

    await waitFor(() => {
      expect(viewportHostInstances.length).toBeGreaterThan(0);
    });

    fireEvent.click(screen.getByRole("button", { name: "Add" }));
    fireEvent.click(await screen.findByTestId("add-menu-terrain"));

    const checkbox = await screen.findByTestId("terrain-lod-grid-visible");

    expect(checkbox).not.toBeChecked();

    fireEvent.click(checkbox);

    const createdTerrain = Object.values(store.getState().document.terrains)[0];

    if (createdTerrain === undefined) {
      throw new Error("Expected the created terrain to exist.");
    }

    await waitFor(() => {
      expect(
        viewportHostInstances[0].setTerrainLodGridVisibleTerrainIds
      ).toHaveBeenLastCalledWith([createdTerrain.id]);
    });

    expect(checkbox).toBeChecked();
  });
});
