import { Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
import { Subscription } from 'rxjs';
import { LoanEditComponent } from './loan-edit/loan-edit.component';

import { AuthService } from './service/auth.service';
import { MenuService } from './service/menu.service';

import {WebSocketService} from './websocket';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit , OnDestroy {
  title = 'SFM broker';
  private menutItemSub: Subscription;
  @ViewChild('loanEditComponent', {static: true}) loanEdit: LoanEditComponent;

  constructor(private menuService: MenuService, private authService: AuthService, private wsService: WebSocketService){
          wsService.createObservableSocket(this.authService.apiWsUrl)
            .subscribe(m => {
              console.log('websocket server send this :', m);
            });
  }

  // tslint:disable-next-line:typedef
  ngOnInit() {
    this.listenToMenuEvent();
    // must be the last, as it may emit events
    this.authService.AutoLogin();

  }

  listenToMenuEvent(): void {
    this.menutItemSub = this.menuService.itemClicked.subscribe(
      (item:any) =>{
        // console.log("emit on select : " + item.text);
        if ( item.popup ===  'loanEdit'){
//          this.loanEdit.somedata = '' + Math.random() + 'changed';
//          this.loanEdit.open('dialog');
        }
      }
    );

  }

  // tslint:disable-next-line:typedef
  ngOnDestroy() {

  }

}
