import {VersionedTextDocumentIdentifier, MarkupContent, Position, Range, CodeLens} from 'vscode-languageclient'
import * as VSCode from 'vscode';

export function toVSRange(rng : Range) : VSCode.Range {
    return new VSCode.Range(toPosition(rng.start), toPosition(rng.end));
}

function toPosition(p : Position) : VSCode.Position {
    return new VSCode.Position(p.line, p.character);
}
 
export interface HighlightParams {
    doc: VersionedTextDocumentIdentifier;
    codeLenses: CodeLens[];
}

export class HighlightService {

    DECORATION : VSCode.TextEditorDecorationType;

    highlights : Map<String, HighlightParams>;

    dispose() {
        this.DECORATION.dispose();
    }

    constructor() {
        this.DECORATION = VSCode.window.createTextEditorDecorationType({
            // before: {
            //     contentIconPath: path.resolve(__dirname, "../icons/boot-12h.png"),
            //     margin: '2px 2px 0px 0px'
            // },
            backgroundColor: 'rgba(109,179,63,0.25)',
            borderColor: 'rgba(109,179,63,0.25)',
            borderSpacing: '4px',
            borderRadius: '4px',
            borderWidth: '4px'
        });
        this.highlights = new Map();
    }

    handle(params : HighlightParams) : void {
        this.highlights.set(params.doc.uri, params);
        this.refresh(params.doc);
    }

    refresh(docId: VersionedTextDocumentIdentifier) {
        let editors = VSCode.window.visibleTextEditors;
        for (let editor of editors) {
            const activeUri = editor.document.uri.toString();
            const activeVersion = editor.document.version;
            if (docId.uri === activeUri && docId.version === activeVersion) {
                //We only update highlights in the active editor for now
                const highlightParams: HighlightParams = this.highlights.get(docId.uri);
                const highlights: CodeLens[] = highlightParams.codeLenses || [];
                let decorations = highlights.map(hl => toVSRange(hl.range));
                editor.setDecorations(this.DECORATION, decorations);
            }
        }
    }
}
