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


class SettingProperty extends Tuple {

    id: number;
    settingId: number;
    key: string;
    type: string;

    int_value: number;
    char_value: string;
    boolean_value: boolean;

    constructor() {
        super('c.s.p.setting.property')
    }
}

@Component({
    selector: 'core-email-setting-list',
    templateUrl: './admin-setting-list.component.html'
})
export class AdminSettingListComponent extends ComponentLifecycleEventEmitter implements OnInit {
    private readonly filt = extend({
        key: "server.setting.data"
    },coreEmailFilt );

    items: SettingProperty[] = [];

    loader: TupleLoader;

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

        this.loader = vortexService.createTupleLoader(this, this.filt);

        this.loader.observable.subscribe(
            tuples => this.items = <SettingProperty[]>tuples);
    }

    ngOnInit() {
    }

    saveClicked() {
        this.loader.save()
            .then(() => this.balloonMsg.showSuccess("Save Successful"))
            .catch(e => this.balloonMsg.showError(e));
    }

    resetClicked() {
        this.loader.load()
            .then(() => this.balloonMsg.showSuccess("Reset Successful"))
            .catch(e => this.balloonMsg.showError(e));
    }

}
