import { Component, AfterContentInit ,AnimationTransitionEvent,OnDestroy  } from '@angular/core';
import { Router } from '@angular/router';
import { SpeechRecognitionService } from './../../services/speech-recognition.service';
import {Broadcaster} from './../../pipes/Broadcaster';
@Component({
   selector:"header",
    template: require('./header.component.html'),
    styles: [require('./header.component.css')]
})

export class HeaderComponent implements AfterContentInit ,OnDestroy {
   
   
    public message:string;
    public username: string = "TEST USER";
   showSearchButton: boolean;
    speechData: string;

    constructor(private BroadcasterService: Broadcaster,private router: Router,private speechRecognitionService: SpeechRecognitionService) { 

        this.showSearchButton = false;
        this.speechData = "";
    }

   

  private _toggleSidebar() {
      
    this.BroadcasterService.sendMessage('sidebarToggle');
    
  
  }
   
 ngOnDestroy() {
        this.speechRecognitionService.DestroySpeechObject();
    }
    onLogout(){
        //this.ds.logout();
        this.router.navigate(['/login']);
    }

    ngAfterContentInit() {
        setTimeout(() => this.router.navigateByUrl('login'), 100);
    }
    destroyVoiceListner(){
if( !this.showSearchButton){
    alert("off")
 this.speechRecognitionService.DestroySpeechObject();
 return;
}


    }
     activateSpeechSearchMovie(): void {
        this.showSearchButton = this.showSearchButton ? false : true;
if(this.showSearchButton){
    
 
}
else{
this.speechRecognitionService.DestroySpeechObject();
 return;
}
        this.speechRecognitionService.record()
            .subscribe(
            //listener
            (value) => {
                this.speechData = value;
               // console.log(value);
this.openComponent(value);

            },
            //errror
            (err) => {
                console.log(err);
                if (err.error == "no-speech") {
                    console.log("--restatring service--");
                    this.showSearchButton = true;
                    this.activateSpeechSearchMovie();
                }
            },
            //completion
            () => {
                this.showSearchButton = true;
                console.log("--complete--");
                this.activateSpeechSearchMovie();
            });
    }
    openComponent(voicedata:String){
         console.log(voicedata);
        if(voicedata=="add widget" || voicedata=="add visit")
        this.router.navigateByUrl('addagenda');
        if(voicedata=="open visits" || voicedata=="open widgets")
        this.router.navigateByUrl('visits');
    }
        
}
    