import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core';
import { AfterContentInit, OnDestroy, OnChanges, SimpleChanges } from '@angular/core/src/metadata/lifecycle_hooks';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { Domain, DomainMember } from '@overflow/commons-typescript/model/domain';
import * as NotificationEntityStore from '../../store/entity/notification';
import { NotificationEntitySelector } from '../../store/';
import { AuthSelector } from '@overflow/shared/auth/store';
import { PageParams } from '@overflow/commons-typescript/model/commons/PageParams';
import { Page } from '@overflow/commons-typescript/model/commons/Page';
import { Notification } from '@overflow/commons-typescript/model/notification';
import { ActivatedRoute } from '@angular/router';

@Component({
    selector: 'of-notification-badge-container',
    templateUrl: './badge-container.component.html',
})
export class NotificationBadgeContainerComponent implements OnInit, OnChanges {

    notificationPage$: Observable<Page<Notification>>;
    @Output() viewAll = new EventEmitter();
    @Output() select = new EventEmitter<Notification>();

    constructor(
        private store: Store<NotificationEntityStore.State>,
        private route: ActivatedRoute
    ) {
        // this.notificationPage$ = store.pipe(select(NotificationEntitySelector.selectAll));
    }

    ngOnInit() {
        this.getNotifications();
    }

    ngOnChanges(changes: SimpleChanges): void {
        this.getNotifications();
    }

    getNotifications() {
        this.store.select(AuthSelector.selectDomainMember).subscribe(
            (domainMember: DomainMember) => {
                const pageParams: PageParams = {
                    pageNo: 0,
                    countPerPage: 10,
                    sortCol: 'id',
                    sortDirection: 'descending',
                };
                this.store.dispatch(new NotificationEntityStore.ReadAllByMember({ member: domainMember.member, pageParams }));
            }
          );
    }

    markAllasRead() {
        // this.store.select(AuthSelector.select('member')).subscribe(
        //     (member: Member) => {
        //         const pageParams: PageParams = {
        //             pageNo: 0,
        //             countPerPage: 10,
        //             sortCol: 'id',
        //             sortDirection: 'descending'
        //         };
        //         this.store.dispatch(new NotificationEntityStore.MarkAllAsRead({ member, pageParams }));
        //     },
        //     (error) => {
        //         console.log(error);
        //     }
        // );
    }

    onSelect(notification: Notification) {
        this.select.emit(notification);
    }

    onViewAll() {
        this.viewAll.emit();
    }

}