import { Component, OnInit, OnDestroy } from '@angular/core';
import { CurrentDeviceService } from '@iotize/ionic';
import { Router } from '@angular/router';
import { monitoringRoutes } from './<%= dasherize(name) %>-routes';
import { Subscription } from 'rxjs';

@Component({
    selector: 'app-<%= dasherize(name) %>',
    templateUrl: '<%= dasherize(name) %>.component.html'
})
export class <%= classify(name) %>Component implements OnInit, OnDestroy {
    subscription: Subscription;

    constructor(public tapService: CurrentDeviceService, public router: Router) {

    }

    ngOnInit(): void {
        this.subscription = this.tapService
            .sessionState
            .subscribe((sessionState) => {
                console.info(`New session state`, sessionState);
                if (monitoringRoutes.find(route => route.path === sessionState.name)) {
                    let url = `/device/monitoring/${sessionState.name}`;
                    console.info(`Navigate to`, url);
                    this.router.navigateByUrl(url);
                }
            });
    }

    ngOnDestroy(): void {
        this.subscription.unsubscribe();
    }

}