'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 { ConnectionProvider, ConnectionNode } from './connections'
import { TextDocumentContentProvider } from './resultsetview'
import { BlockDetector } from './editorsupport'
import { SessionManager } from './sessionmanager'

let sessionManager : SessionManager = null;
let rowLimit = 50;

// 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) {
    sessionManager = new SessionManager(context);
    
    const connectionProvider = new ConnectionProvider();
    vscode.window.registerTreeDataProvider('connections', connectionProvider);

    let disposable = vscode.commands.registerCommand('frog.executeSnippet', () => {        
        let editor = vscode.window.activeTextEditor;

        if (!editor) return;

        let selection = editor.selection;

        if (selection.isEmpty) {
            new BlockDetector(editor).detect();
        }

        let text = editor.document.getText(editor.selection);        

        if (!text) {
            vscode.window.showInformationMessage('No text selected');
            return;
        }

        sessionManager.execute(text, rowLimit);
    });
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.compileFile', () => {        
        let editor = vscode.window.activeTextEditor;

        const text = editor.document.getText();
        
        sessionManager.execute(text, rowLimit);
    });
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.commit', () => {
        sessionManager.execute("commit", 0);
    });
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.rollback', () => {
        sessionManager.execute("rollback", 0);
    });
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.checkConnection', () => {
        sessionManager.execute("select 1 from dual", 1);
    });
    context.subscriptions.push(disposable);

    function changeSession(node: ConnectionNode) {
        sessionManager.currentConnection = node;
    }

    disposable = vscode.commands.registerCommand('frog.setRowLimit', (args : Object) => {
        rowLimit = args['rowLimit'];
        vscode.window.showInformationMessage(`Row limit set to ${args['rowLimit']}`);
    });
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.selectConnection', (node: ConnectionNode) => {
        if (!sessionManager.isSessionActive()) {
            sessionManager.currentConnection = node;
        } else {
            vscode.window.showInformationMessage(`You want to change the connection to '${node.name}'. Sure?`, 'Yes')
            .then( (selection) => {
                if (selection == 'Yes') {
                    sessionManager.currentConnection = node;
                }
            }, (reason) => {})
        }
	})
    context.subscriptions.push(disposable);

    disposable = vscode.commands.registerCommand('frog.chooseConnection', () => {
            vscode.window.showQuickPick(["A", "B", "C"])
            .then( (selection) => {
                if (selection == 'Yes') {
                    console.log(selection);
                }
            }, (reason) => {})
	})
    context.subscriptions.push(disposable);
}

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