import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRouteSnapshot, NavigationEnd } from '@angular/router';
import { EmitService } from "../../shared/event/eventEmitter";
import { Title } from '@angular/platform-browser';
import {LocalStorageService, SessionStorageService} from "ngx-webstorage";
import { zh_CN,NzI18nService } from "ng-zorro-antd";

@Component({
    selector: 'jhi-main',
    templateUrl: './main.component.html'
})
export class JhiMainComponent implements OnInit {

    token: boolean;
    isCollapsed: boolean;
    constructor(
        private titleService: Title,
        private router: Router,
        private $localStorage: LocalStorageService,
        private $sessionStorage: SessionStorageService,
        public emitService: EmitService,
        private nzI18nService:NzI18nService
    ) {}

    private getPageTitle(routeSnapshot: ActivatedRouteSnapshot) {
        let title: string = (routeSnapshot.data && routeSnapshot.data['pageTitle']) ? routeSnapshot.data['pageTitle'] : '智能监控';
        if (routeSnapshot.firstChild) {
            title = this.getPageTitle(routeSnapshot.firstChild) || title;
        }
        return title;
    }

    ngOnInit() {
        this.nzI18nService.setLocale(zh_CN);

        const accessToken = this.$localStorage.retrieve('accessToken');
        if(accessToken){
            this.token = true;
        }else{
            this.token = false;
        }

        this.router.events.subscribe((event) => {
            if (event instanceof NavigationEnd) {
                this.titleService.setTitle(this.getPageTitle(this.router.routerState.snapshot.root));
            }
        });
    }


}
