"use strict";
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below

import * as vscode from "vscode";
import * as languageClient from "vscode-languageclient";
import * as path from "path";
import * as fs from "fs";

// Defines the search path of your language server DLL. (.NET Core)
const languageServerPaths = [
  "../UpsilonLanguageServer/UpsilonLanguageServer/bin/Debug/netcoreapp2.1/UpsilonLanguageServer.dll",
  "./server/UpsilonLanguageServer.dll"
];

function activateLanguageServer(context: vscode.ExtensionContext) {
  // The server is implemented in an executable application.
  let serverModule: string = null;
  for (let p of languageServerPaths) {
    p = context.asAbsolutePath(p);
    // console.log(p);
    if (fs.existsSync(p)) {
      serverModule = p;
      break;
    }
  }
  if (!serverModule)
    throw new URIError("Cannot find the language server module.");
  let workPath = path.dirname(serverModule);
  console.log(`Use ${serverModule} as server module.`);
  console.log(`Work path: ${workPath}.`);

  // If the extension is launched in debug mode then the debug server options are used
  // Otherwise the run options are used
  let serverOptions: languageClient.ServerOptions = {
    run: {
      command: "dotnet",
      args: [serverModule],
      options: { cwd: workPath }
    },
    debug: {
      command: "dotnet",
      args: [serverModule, "--debug"],
      options: { cwd: workPath }
    }
  };
  // Options to control the language client
  let clientOptions: languageClient.LanguageClientOptions = {
    // Register the server for Upsilon documents
    documentSelector: [
      {
        language: "upsilon",
        scheme: "file"
      }
    ],
    synchronize: {
      configurationSection: "upsilonLanguageServer",
      // Notify the server about file changes to '.clientrc files contain in the workspace
      fileEvents: [
        vscode.workspace.createFileSystemWatcher("**/*.clientrc"),
        vscode.workspace.createFileSystemWatcher("**/*.yup"),
        vscode.workspace.createFileSystemWatcher("**/*.lua"),
        vscode.workspace.createFileSystemWatcher("**/.upsilon/*.json")
      ]
    }
  };

  // Create the language client and start the client.
  let client = new languageClient.LanguageClient(
    "upsilonLanguageServer",
    "Upsilon Language Server",
    serverOptions,
    clientOptions
  );
  let disposable = client.start();

  // Push the disposable to the context's subscriptions so that the
  // client can be deactivated on extension deactivation
  context.subscriptions.push(disposable);
}

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
  console.log("Upsilon extension is now activated.");
  activateLanguageServer(context);
}

// this method is called when your extension is deactivated
export function deactivate() {}
