import { Component, EventEmitter, Output, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Store, select } from '@ngrx/store';
import { Sensor } from '@overflow/commons-typescript/model/sensor';
import * as SensorEntityStore from '../store/entity/sensor';
import { SensorListContainerSelector } from '../store';
import { AuthContainerSelector } from '@overflow/shared/auth/store';
import { PageParams } from '@overflow/commons-typescript/model/commons/PageParams';
import { Page } from '@overflow/commons-typescript/model/commons/Page';
import { Target } from '@overflow/commons-typescript/model/target';
import { DomainMember } from '@overflow/commons-typescript/model/domain';

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

    page$: Observable<Page<Sensor>>;
    pending$: Observable<boolean>;
    @Output() select = new EventEmitter<Sensor>();
    @Output() addSensor = new EventEmitter();
    pageNo: number;

    constructor(private store: Store<any>) {
        this.pageNo = 0;
    }

    ngOnInit() {
        this.page$ = this.store.pipe(select(SensorListContainerSelector.selectPage));
        // this.page$ = this.store.pipe(select(SensorListContainerSelector.selectPage));

        this.getSensors();
    }

    getSensors() {
        this.store.select(AuthContainerSelector.selectDomainMember).subscribe(
            (domainMember: DomainMember) => {
                const pageParams: PageParams = {
                    pageNo: this.pageNo,
                    countPerPage: 10,
                    sortCol: 'id',
                    sortDirection: 'descending',
                };
                this.store.dispatch(new SensorEntityStore.ReadAllByDomainID({ domainID: domainMember.domain.id, pageParams }));
            }
        );
    }
}
