import { Component, HostListener, OnInit, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Params, Router, NavigationEnd } from '@angular/router';
import { AppState } from './app.service';
import { AppStore } from './app.store';
import { AuthService } from './user/auth.service';
import { User } from './user/user/user.service';

import { TinCanService } from './tincan/tincan.service';
import { MatIconRegistry } from '@angular/material';
import { APIService } from './shared/services/api.service';

import * as _ from 'underscore';

@Component({
  selector: 'app',
  styleUrls: [
    './app.component.scss', "../assets/scss/hsa-theme.scss"
  ],
  templateUrl: './app.component.html',
  encapsulation : ViewEncapsulation.None
})
export class AppComponent implements OnInit{
  angularclassLogo = 'assets/img/pa_logo.png';
  name = 'peer activate';
  url = 'https://github.com/leChuckles/PeerActivate';
  
  //@HostListener('window:beforeunload', [ '$event' ])
  //beforeUnloadHander(event) {
    //this.setUnload();
  //}

  constructor(private api : APIService, public appState: AppState, public authService: AuthService, public route: ActivatedRoute, public router : Router, public store : AppStore, public tinCanService : TinCanService, public MatIconRegistry : MatIconRegistry) {
    
    MatIconRegistry.registerFontClassAlias('materialdesignicons-webfont', 'mat-custom-icons');

  }

  setUnload(){
    this.tinCanService.makeStatement()
      .subscribe(res => res)
    /*this.authService.setLogout()
      .subscribe(res => res)*/
  }
  ngOnInit() {
    console.log('START')
    //this.startSession();
    this.api.findOne({'username' : this.authService.getUser().username}, 'user')
      .subscribe((user : User) => {
        console.log('USER: ', this.authService.getUser().username, user)
        this.store.setState({user : user});
      })
    
    
  }
  startSession(){
    this.appState.set('session', {
      session_id : Date.now(),
      statements : []
    });
  }
}