import { Component, Input, AfterViewInit } from "@angular/core";
import { NgParameter } from "../../formulaire/ngparam";
import { DialogEditParamValuesComponent } from "../dialog-edit-param-values/dialog-edit-param-values.component";
import { MatDialog } from "@angular/material";
import { ParamValueMode } from "jalhyd";
import { I18nService } from "../../services/internationalisation/internationalisation.service";
import { ApplicationSetupService } from "../../services/app-setup/app-setup.service";

@Component({
    selector: "param-values",
    templateUrl: "./param-values.component.html",
    styleUrls: [
        "./param-values.component.scss"
    ]
})
export class ParamValuesComponent implements AfterViewInit {

    @Input()
    public param: NgParameter;

    @Input()
    public title: string;


    constructor(
        private editValuesDialog: MatDialog,
        private intlService: I18nService,
        private appSetupService: ApplicationSetupService
    ) { }

    public get isMinMax() {
        return this.param.valueMode === ParamValueMode.MINMAX;
    }

    public get isListe() {
        return this.param.valueMode === ParamValueMode.LISTE;
    }

    public get infoText() {
        return NgParameter.preview(this.param.paramDefinition);
    }

    public openDialog() {
        // modification des valeurs variables
        this.editValuesDialog.open(
            DialogEditParamValuesComponent,
            {
                disableClose: true,
                data: {
                    param: this.param
                }
            }
        );
    }

    public ngAfterViewInit() {
        // open dialog when switching to this mode, but only the first time this component is built,
        // otherwise switching back from another calc tab will trigger the dialog again
        if (this.param.valueMode === ParamValueMode.MINMAX && this.param.minValue === undefined) {
            // use Promise trick to introduce a pseudo-timeout and avoid ExpressionChangedAfterItHasBeenCheckedError
            Promise.resolve().then(() => {
                this.openDialog();
            });
        }
    }
}