import { Component, EventEmitter, Output, OnInit } from '@angular/core';
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import * as ProbeEntityStore from '../store/entity/probe';
import { ProbeListContainerSelector } from '../store';
import { AuthContainerSelector } from '../../shared/auth/store';
import { DomainMember } from '@overflow/commons-typescript/model/domain';

@Component({
    selector: 'of-probe-list-container',
    templateUrl: './probe-list-container.component.html',
})
export class ProbeListContainerComponent implements OnInit {

    probeHosts$: Observable<ProbeHost[]>;
    pending$: Observable<boolean>;
    error$:  Observable<any>;
    @Output() select = new EventEmitter();

    constructor(
        private store: Store<any>,
    ) {
    }

    ngOnInit() {
        this.probeHosts$ = this.store.pipe(select(ProbeListContainerSelector.selectAll));
        this.pending$ = this.store.pipe(select(ProbeListContainerSelector.selectPending));
        this.error$ = this.store.pipe(select(ProbeListContainerSelector.selectError));

        this.store.select(AuthContainerSelector.selectDomainMember).subscribe(
            (domainMember: DomainMember) => {
                this.store.dispatch(new ProbeEntityStore.ReadAllByDomainID(domainMember.domain.id));
            }
        );
    }

    onSelect(probeHost: ProbeHost) {
        this.select.emit(probeHost);
    }

}
