import React, { useState, useCallback } from 'react';
import TitleBar from './components/TitleBar/TitleBar';
import Toolbar from './components/Toolbar/Toolbar';
import Sidebar from './components/Sidebar/Sidebar';
import MainPanel from './components/MainPanel/MainPanel';
import AIPanel from './components/AIPanel/AIPanel';
import './App.css';
import { IntentActionHandlers, IntentActionStatus } from './types/intentActions';
import { ExtractedStep } from './services/api';

// 功能模块类型定义
export type ModuleType = 'intent' | 'task' | 'simulation' | 'code' | 'data' | 'knowledge';

const App: React.FC = () => {
	// 当前选中的功能模块
	const [activeModule, setActiveModule] = useState<ModuleType>('intent');

	const [intentActions, setIntentActions] = useState<IntentActionHandlers | null>(null);
	const [intentActionStatus, setIntentActionStatus] = useState<IntentActionStatus>({
		isSaving: false,
		isGenerating: false,
		isChecking: false,
	});

	// 从意图编制提取的步骤（共享给任务规划）
	const [extractedSteps, setExtractedSteps] = useState<ExtractedStep[]>([]);

	// 当前选中的知识库
	const [selectedKnowledgeBase, setSelectedKnowledgeBase] = useState<string>('default');

	// AI 面板是否最大化
	const [aiPanelMaximized, setAiPanelMaximized] = useState(false);

	// AI 面板高度
	const [aiPanelHeight, setAiPanelHeight] = useState(250);

	// 完成编制后跳转到任务规划
	const handleIntentComplete = useCallback((steps: ExtractedStep[]) => {
		setExtractedSteps(steps);
		setActiveModule('task');
	}, []);

	const handleToolbarAction = useCallback((action: string) => {
		if (activeModule === 'intent') {
			switch (action) {
				case '新建意图':
					intentActions?.onNew?.();
					return;
					case '打开':
						intentActions?.onOpen?.();
						return;
				case '保存':
					intentActions?.onSave?.();
					return;
				case 'AI 生成':
					intentActions?.onGenerate?.();
					return;
				case 'AI 检查':
					intentActions?.onCheck?.();
					return;
				case '导出':
					intentActions?.onExport?.();
					return;
				default:
					return;
			}
		}
	}, [activeModule, intentActions]);

	return (
		<div className="app">
			{/* 标题栏 */}
			<TitleBar />

			{/* 二级功能快捷栏 */}
			<Toolbar
				activeModule={activeModule}
				onActionClick={handleToolbarAction}
				intentActionStatus={intentActionStatus}
				selectedKnowledgeBase={selectedKnowledgeBase}
				onKnowledgeBaseChange={setSelectedKnowledgeBase}
			/>

			{/* 主体区域 */}
			<div className="app-body">
				{/* 左侧功能面板 */}
				<Sidebar
					activeModule={activeModule}
					onModuleChange={setActiveModule}
				/>

				{/* 工作区 */}
				<div className="workspace">
					{/* 主窗口 */}
					<MainPanel
						activeModule={activeModule}
						aiPanelMaximized={aiPanelMaximized}
						onRegisterIntentActions={setIntentActions}
						onIntentStatusChange={setIntentActionStatus}
						onIntentComplete={handleIntentComplete}
						extractedSteps={extractedSteps}
					/>

					{/* AI 交互窗口 */}
					<AIPanel
						height={aiPanelHeight}
						onHeightChange={setAiPanelHeight}
						maximized={aiPanelMaximized}
						onMaximizeChange={setAiPanelMaximized}
					/>
				</div>
			</div>
		</div>
	);
};

export default App;
