import {Component} from '@angular/core';

import {Hint} from 'codemirror';

import {HintProvider, StringMap} from 'm2-core';
import {CdDemoComponent} from '../../../abstract/CdDemoComponent';


@Component({
    template: `
        <div *ngIf="!isInitialized" class="h-100 busy-modal"></div>

        <div class="cd-demo-widget h-100">
            <code-editor-lazy
                    [code]="code"
                    [editModeON]="editModeON"
                    [checkSyntax]="checkSyntax"
                    [checkSyntaxDebounce]="checkSyntaxDebounce"
                    [showCheckSyntaxBtn]="showCheckSyntaxBtn"
                    [hintProvider]="hintProvider"
                    (updated)="onUpdated($event)"
                    (initialized)="isInitialized = true"
            >
            </code-editor-lazy>
        </div>
    `
})
export class CdCodeEditorDemoComponent extends CdDemoComponent {

    isInitialized = false;

    code = `public static void main(String[] args) {
    println("Hello world!");
}
`;
    editModeON = true;
    checkSyntax = true;
    checkSyntaxDebounce = 1500;
    showCheckSyntaxBtn = true;
    hintProvider: HintProvider = new CdHintProvider();

    onUpdated(code: string): void {
        this.code = code;
    }

}


// region hintProvider
class CdHintProvider extends HintProvider {

    getSnippets(): StringMap<Hint | string> {
        return {
            fnstr: 'String fn() {\n\treturn "";\n}\n'
        };
    }

    getTypeHints(): StringMap<Hint[]> {
        return {
            MyClass: [
                // @ts-ignore
                { text: 'myStaticField' },
                // @ts-ignore
                { text: 'myStaticMethod()' }
            ]
        };
    }

    getTypeInstanceHints(): StringMap<Hint[]> {
        return {
            MyClass: [
                // @ts-ignore
                { text: 'myInstanceField' },
                // @ts-ignore
                { text: 'myInstanceMethod()' }
            ]
        };
    }

    getTypeImports(): StringMap<string> {
        return {
            MyClass: 'import java.my.package.MyClass'
        };
    }

}
// endregion hintProvider
