import {Component, ElementRef, OnInit, ViewChild} 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";
import {CommonService} from '../../shared/common/common.service';
import {ThemeList} from '../../app.constants';
import {AppService} from '../../app.service';
import {Subscription} from 'rxjs';

@Component({
    selector: 'jhi-main',
    templateUrl: './main.component.html',
    styles:[`        
        .layout{
            display: -ms-flexbox;
            -ms-flex-direction:row;
        }
        .layout-content{
            display: -ms-flexbox;
            -ms-flex-direction: column;
            -ms-flex: auto;
            width: 80%;
        }
    `]
})
export class JhiMainComponent implements OnInit {
    @ViewChild('container') container:ElementRef;

    token: boolean;
    theme;

    private sub : Subscription;
    constructor(
        private titleService: Title,
        private router: Router,
        private appSer:AppService,
        private $localStorage: LocalStorageService,
        private $sessionStorage: SessionStorageService,
        public emitService: EmitService,
        private nzI18nService:NzI18nService,
        private commonSer:CommonService
    ) {

    }

    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));
            }
        });

       const sub = this.appSer.theme.subscribe(value=>{
            this.theme = value;
        })
    }

    fullScreen(){
        this.commonSer.showInFullScreen(this.container.nativeElement,"RequestFullScreen")
    }
}
