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 { ProbeEntitySelector, ProbeListContainerSelector } from '../store';
import { AuthSelector } from '@overflow/member/store';
import { Domain } 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>;
    @Output() select = new EventEmitter();

    constructor(
        private store: Store<ProbeEntityStore.State>,
    ) {
        this.probeHosts$ = store.pipe(select(ProbeEntitySelector.selectAll));
        this.pending$ = store.pipe(select(ProbeListContainerSelector.selectPending));
    }

    ngOnInit() {
        this.store.select(AuthSelector.select('domain')).subscribe(
            (domain: Domain) => {
                this.store.dispatch(new ProbeEntityStore.ReadAllByDomainID(domain.id));
            },
            (error) => {
                console.log(error);
            }
        );
    }

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

}
