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 { SensorEntitySelector, SensorPageSelector } from '../store';
import { AuthSelector } from '../../member/store';
import { Domain } from '@overflow/commons-typescript/model/domain';
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';

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

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

    constructor(private store: Store<SensorEntityStore.State>) {
        this.page$ = store.pipe(select(SensorPageSelector));
    }

    ngOnInit() {
        this.store.select(AuthSelector.select('domain')).subscribe(
            (domain: Domain) => {
                const pageParams: PageParams = {
                    pageNo: 0,
                    countPerPage: 10,
                    sortCol: 'id',
                    sortDirection: 'descending',
                };
                this.store.dispatch(new SensorEntityStore.ReadAllByDomainID({domainID : domain.id, pageParams}));
            },
            (error) => {
                console.log(error);
            }
        );
    }

}
