import {
    Component,
    ViewEncapsulation,
    ComponentResolver,
    ViewChild,
    ViewContainerRef
} from "@angular/core";
import {NgIf, NgClass} from "@angular/common";
import {CoreDialogService, ICoreDialogArguments} from "./core-dialog.service";
import {CoreIcon} from "../core-icon/core-icon";

declare var System:any;
@Component(
    {
        selector: 'core-dialog',
        templateUrl: "node_modules/bia-framework/components/core-dialog/core-dialog.template.html",
        styleUrls: ['node_modules/bia-framework/components/core-dialog/core-dialog.style.css'],
        encapsulation: ViewEncapsulation.None,
        directives: [NgIf, NgClass, CoreIcon]
    }
)
export class CoreDialog {
    private isProcessing:boolean = false;
    private isVisible:boolean = false;
    private hasBackButton:boolean = false;
    private tittle:string;
    private backgroundColor:string;

    @ViewChild('container', {read: ViewContainerRef})
    container:ViewContainerRef;

    constructor(private coreDialogService:CoreDialogService, private resolver:ComponentResolver) {
        this.coreDialogService.onShow.subscribe((newRequest)=> this.showDialog(newRequest));

    };

    public back = ():void => {
        this.coreDialogService.back();
        this.isVisible = false;
        this.container.remove();

        //TO-DO REMOVER DEPOIS
        console.log("container removido...");


    };

    private showDialog = (request:ICoreDialogArguments):void => {
        this.isProcessing = true;
        this.isVisible = true;
        this.backgroundColor = request.backgroundColor || "white";
        this.hasBackButton = request.hasBackButton || false;
        this.tittle = request.tittle || "";

        System.import(request.path).then(c => c[request.className]).then((result)=> {
            this.resolver.resolveComponent(result).then(factory => {
                this.container.createComponent(factory, 0, this.container.injector);

            });

        });
    };


}

