'use strict';
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
import { DiffWithWorkingCommandArgs } from './diffWithWorking';
import { GitCommit, GitService, GitUri } from '../gitService';
import { Logger } from '../logger';

export interface DiffLineWithWorkingCommandArgs {
    commit?: GitCommit;
    line?: number;
    showOptions?: TextDocumentShowOptions;
}

export class DiffLineWithWorkingCommand extends ActiveEditorCommand {

    constructor(private git: GitService) {
        super(Commands.DiffLineWithWorking);
    }

    async execute(editor: TextEditor, uri?: Uri, args: DiffLineWithWorkingCommandArgs = {}): Promise<any> {
        uri = getCommandUri(uri, editor);
        if (uri === undefined) return undefined;

        const gitUri = await GitUri.fromUri(uri, this.git);
        args.line = args.line || (editor === undefined ? gitUri.offset : editor.selection.active.line);

        if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
            if (editor !== undefined && editor.document !== undefined && editor.document.isDirty) return undefined;

            const blameline = args.line - gitUri.offset;
            if (blameline < 0) return undefined;

            try {
                const blame = await this.git.getBlameForLine(gitUri, blameline);
                if (blame === undefined) return window.showWarningMessage(`Unable to open compare. File is probably not under source control`);

                args.commit = blame.commit;
                // If the line is uncommitted, find the previous commit
                if (args.commit.isUncommitted) {
                    args.commit = new GitCommit(args.commit.type, args.commit.repoPath, args.commit.previousSha!, args.commit.previousFileName!, args.commit.author, args.commit.date, args.commit.message);
                    args.line = blame.line.line + 1 + gitUri.offset;
                }
            }
            catch (ex) {
                Logger.error(ex, 'DiffLineWithWorkingCommand', `getBlameForLine(${blameline})`);
                return window.showErrorMessage(`Unable to open compare. See output channel for more details`);
            }
        }

        return commands.executeCommand(Commands.DiffWithWorking, uri, args as DiffWithWorkingCommandArgs);
    }
}
