import { create } from 'zustand';
import type { TileInfo } from '../types/assets';
import type { ClassSummary, MethodSummary, FlowNode } from '../types/scripts';
import type { GumpDrawList } from '../types/gump';

export type LeftPanelTab = 'scripts' | 'statics' | 'mobiles' | 'gumps';

export type CenterMode =
  | 'empty'
  | 'config'
  | 'asset_static'
  | 'asset_mobile'
  | 'script_class'
  | 'flow_method'
  | 'gump_render'
  | 'gump_image';

export interface GumpEntry {
  id: number;
  name: string;
  tags: string[];
}

export interface ScriptGumpEntry {
  class_name: string;
  file_path: string;
  tags: string[];
}

interface AppState {
  // Config
  uoRoot: string;
  servuoScripts: string;
  assetFormat: 'mul' | 'uop';
  isConfigured: boolean;
  setUoRoot: (p: string) => void;
  setServuoScripts: (p: string) => void;
  setAssetFormat: (f: 'mul' | 'uop') => void;
  setIsConfigured: (v: boolean) => void;

  // Navigation
  activeTab: LeftPanelTab;
  centerMode: CenterMode;
  setActiveTab: (tab: LeftPanelTab) => void;
  setCenterMode: (mode: CenterMode) => void;

  // Asset browser
  selectedTile: TileInfo | null;
  setSelectedTile: (tile: TileInfo | null) => void;

  // Script browser
  selectedClass: ClassSummary | null;
  setSelectedClass: (c: ClassSummary | null) => void;

  // Method / flow
  selectedMethod: MethodSummary | null;
  setSelectedMethod: (m: MethodSummary | null) => void;

  // Flow tree
  flowRoot: FlowNode | null;
  setFlowRoot: (node: FlowNode | null) => void;

  // Gump
  gumpDrawList: GumpDrawList | null;
  setGumpDrawList: (g: GumpDrawList | null) => void;

  // Gump browser (Phase 4)
  selectedGump: GumpEntry | null;
  setSelectedGump: (g: GumpEntry | null) => void;

  // Script gump browser
  selectedScriptGump: ScriptGumpEntry | null;
  setSelectedScriptGump: (g: ScriptGumpEntry | null) => void;

  // Fake data for dynamic gumps / flow conditions
  fakeData: Record<string, string>;
  setFakeData: (key: string, value: string) => void;
  clearFakeData: () => void;
}

export const useAppStore = create<AppState>((set) => ({
  uoRoot: '',
  servuoScripts: '',
  assetFormat: 'mul',
  isConfigured: false,
  setUoRoot: (p) => set({ uoRoot: p }),
  setServuoScripts: (p) => set({ servuoScripts: p }),
  setAssetFormat: (f) => set({ assetFormat: f }),
  setIsConfigured: (v) => set({ isConfigured: v }),

  activeTab: 'statics',
  centerMode: 'empty',
  setActiveTab: (tab) => set({ activeTab: tab }),
  setCenterMode: (mode) => set({ centerMode: mode }),

  selectedTile: null,
  setSelectedTile: (tile) => set({ selectedTile: tile }),

  selectedClass: null,
  setSelectedClass: (c) => set({ selectedClass: c, selectedMethod: null, flowRoot: null }),

  selectedMethod: null,
  setSelectedMethod: (m) => set({ selectedMethod: m }),

  flowRoot: null,
  setFlowRoot: (node) => set({ flowRoot: node }),

  gumpDrawList: null,
  setGumpDrawList: (g) => set({ gumpDrawList: g }),

  selectedGump: null,
  setSelectedGump: (g) => set({ selectedGump: g }),

  selectedScriptGump: null,
  setSelectedScriptGump: (g) => set({ selectedScriptGump: g }),

  fakeData: {},
  setFakeData: (key, value) =>
    set((s) => ({ fakeData: { ...s.fakeData, [key]: value } })),
  clearFakeData: () => set({ fakeData: {} }),
}));
