import {Component} from "@angular/core";
import {
    ComponentLifecycleEventEmitter,
    extend,
    TupleLoader,
    VortexService
} from "@synerty/vortexjs";
import {abstracDataLoaderFilt} from "../PluginNames";
import {AppServerSettingsTuple} from "../tuples/AppServerSettingsTuple";
import {Ng2BalloonMsgService} from "@synerty/ng2-balloon-msg";


@Component({
    selector: 'pl-pof-diagram-loader-edit-source-settings',
    templateUrl: './edit.component.html'
})
export class EditAppSettingsComponent extends ComponentLifecycleEventEmitter {
    // This must match the dict defined in the admin_backend handler
    private readonly filt = {
        "key": "admin.Edit." + AppServerSettingsTuple.tupleName
    };

    item: AppServerSettingsTuple = new AppServerSettingsTuple();

    loader: TupleLoader;

    constructor(private balloonMsg: Ng2BalloonMsgService,
                vortexService: VortexService) {
        super();

        this.loader = vortexService.createTupleLoader(
            this,
            () => extend({}, this.filt, abstracDataLoaderFilt)
        );

        this.loader.observable
            .subscribe((tuples: AppServerSettingsTuple[]) => {
                if (tuples.length == 1) {
                    this.item = tuples[0];
                } else {
                    this.item = new AppServerSettingsTuple();
                }

                // Setup defaults
                if (this.item.enabled == null)
                    this.item.enabled = false;

                if (this.item.appSshUsername == null)
                    this.item.appSshUsername = 'enmac';
            });
    }


    save() {

        this.loader.save([this.item])
            .then(() => {
                this.balloonMsg.showSuccess("Success");
            })
            .catch((e) => {
                this.balloonMsg.showError(e);
            });
    }

}