import * as vscode from "vscode";
import { ICViewProvider } from "./views/ICViewProvider";
import { showICHelperPanel } from "./panels/ICHelperPanel";
import { VCDViewerPanel, VCDViewerEditorProvider } from "./panels/VCDViewerPanel";
import { ChatHistoryManager } from "./utils/chatHistoryManager";
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
import { VCDFileServer } from "./services/vcdFileServer";
import { initUserService } from "./services/userService";

export function activate(context: vscode.ExtensionContext) {
  console.log("🎉 IC Coder 插件已激活！");

  // 初始化用户服务
  initUserService(context);

  // 初始化 VCD 文件服务器
  const vcdFileServer = new VCDFileServer(context.extensionUri);
  vcdFileServer.start().then((port) => {
    console.log(`VCD 文件服务器已启动，端口: ${port}`);
  }).catch((error) => {
    console.error("启动 VCD 文件服务器失败:", error);
  });

  // 在插件停用时关闭服务器
  context.subscriptions.push({
    dispose: () => vcdFileServer.stop()
  });

  // 注册 Authentication Provider
  const authProvider = new ICCoderAuthenticationProvider(context);
  context.subscriptions.push(
    vscode.authentication.registerAuthenticationProvider(
      "iccoder",
      "IC Coder",
      authProvider
    )
  );

  // 检查登录状态，如果已登录则自动打开聊天面板
  vscode.authentication.getSession("iccoder", [], { createIfNone: false })
    .then((session) => {
      if (session) {
        vscode.commands.executeCommand("ic-coder.openChat");
      }
    }, () => {
      // 未登录，不做任何操作
    });

  // 注册命令：打开助手面板
  const openPanelCommand = vscode.commands.registerCommand(
    "ic-coder.openPanel",
    async () => {
      await showICHelperPanel(context);
    }
  );

  // 注册命令：打开聊天（用于侧边栏）
  const openChatCommand = vscode.commands.registerCommand(
    "ic-coder.openChat",
    async () => {
      await showICHelperPanel(context);
    }
  );

  // 注册命令：打开 VCD 波形查看器
  const openVCDViewerCommand = vscode.commands.registerCommand(
    "ic-coder.openVCDViewer",
    async (vcdFilePath?: string) => {
      if (!vcdFilePath) {
        // 如果没有提供文件路径，让用户选择 VCD 文件
        const fileUri = await vscode.window.showOpenDialog({
          canSelectFiles: true,
          canSelectFolders: false,
          canSelectMany: false,
          filters: {
            "VCD 文件": ["vcd"],
            "所有文件": ["*"],
          },
          title: "选择 VCD 文件",
        });

        if (fileUri && fileUri[0]) {
          vcdFilePath = fileUri[0].fsPath;
        } else {
          return;
        }
      }

      VCDViewerPanel.createOrShow(context.extensionUri, vcdFilePath, vcdFileServer);
    }
  );

  // 注册命令：在浏览器中打开 VCD 波形查看器
  const openVCDViewerInBrowserCommand = vscode.commands.registerCommand(
    "ic-coder.openVCDViewerInBrowser",
    async (vcdFilePath?: string) => {
      if (!vcdFilePath) {
        const fileUri = await vscode.window.showOpenDialog({
          canSelectFiles: true,
          canSelectFolders: false,
          canSelectMany: false,
          filters: {
            "VCD 文件": ["vcd"],
            "所有文件": ["*"],
          },
          title: "选择 VCD 文件",
        });

        if (fileUri && fileUri[0]) {
          vcdFilePath = fileUri[0].fsPath;
        } else {
          return;
        }
      }

      // 注册文件到服务器
      const fileId = vcdFileServer.registerFile(vcdFilePath);
      const viewerUrl = vcdFileServer.getViewerUrl(fileId);

      // 在默认浏览器中打开
      vscode.env.openExternal(vscode.Uri.parse(viewerUrl));
      vscode.window.showInformationMessage(`波形查看器已在浏览器中打开`);
    }
  );

  // 注册命令：用户登录
  const loginCommand = vscode.commands.registerCommand(
    "ic-coder.login",
    async () => {
      try {
        // 先清除 session 偏好，避免 VSCode 弹出"账户不一致"确认框
        try {
          await vscode.authentication.getSession("iccoder", [], {
            clearSessionPreference: true,
            createIfNone: false
          });
        } catch {
          // 忽略错误
        }

        // 创建新 session
        await vscode.authentication.getSession("iccoder", [], { createIfNone: true });
      } catch (error) {
        vscode.window.showErrorMessage(`登录失败: ${error}`);
      }
    }
  );

  // 注册命令：用户登出
  const logoutCommand = vscode.commands.registerCommand(
    "ic-coder.logout",
    async () => {
      try {
        const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
        if (session) {
          // 通过创建新会话并清除偏好来实现登出
          await vscode.authentication.getSession("iccoder", [], {
            clearSessionPreference: true,
            forceNewSession: true
          });
          vscode.window.showInformationMessage("已退出登录");
        } else {
          vscode.window.showInformationMessage("当前未登录");
        }
      } catch (error) {
        vscode.window.showInformationMessage("当前未登录");
      }
    }
  );

  // 注册命令：查看会话历史
  // TODO: 这些命令需要根据新的任务架构重新实现
  // 暂时注释掉，等待重新实现
  /*
  const viewHistoryCommand = vscode.commands.registerCommand(
    "ic-coder.viewHistory",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );

  const newSessionCommand = vscode.commands.registerCommand(
    "ic-coder.newSession",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );

  const exportSessionCommand = vscode.commands.registerCommand(
    "ic-coder.exportSession",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );

  const deleteSessionCommand = vscode.commands.registerCommand(
    "ic-coder.deleteSession",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );

  const clearHistoryCommand = vscode.commands.registerCommand(
    "ic-coder.clearHistory",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );

  const searchSessionCommand = vscode.commands.registerCommand(
    "ic-coder.searchSession",
    () => {
      vscode.window.showInformationMessage("此功能正在重构中，敬请期待");
    }
  );
  */

  // 注册侧边栏视图
  const viewProvider = new ICViewProvider(context.extensionUri, context);
  const viewRegistration = vscode.window.registerWebviewViewProvider(
    "ic-coder.mainView",
    viewProvider
  );

  // 注册 VCD 自定义编辑器
  const vcdEditorProvider = VCDViewerEditorProvider.register(context, vcdFileServer);

  // 添加到订阅
  context.subscriptions.push(
    openPanelCommand,
    openChatCommand,
    openVCDViewerCommand,
    openVCDViewerInBrowserCommand,
    loginCommand,
    logoutCommand,
    // TODO: 等待重新实现这些命令
    // viewHistoryCommand,
    // newSessionCommand,
    // exportSessionCommand,
    // deleteSessionCommand,
    // clearHistoryCommand,
    // searchSessionCommand,
    viewRegistration,
    vcdEditorProvider
  );
}

export function deactivate() {}
