import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Title} from '@angular/platform-browser';
import {FullPageSliderComponent} from '../openaireLibrary/utils/full-page-slider/full-page-slider.component';

@Component({
  selector: 'home',
  templateUrl: 'home.component.html',
  styleUrls: ['home.component.css'],
})
export class HomeComponent implements OnInit{
  
  public initSlide: number = 1;
  public fragments: string[] = ['', 'why', 'who', 'features'];
  @ViewChild("slider")
  public slider: FullPageSliderComponent;
  
  constructor(private route: ActivatedRoute,
              private router: Router,
              private title: Title) {}
  
  ngOnInit() {
    this.title.setTitle('OpenAIRE - UsageCounts');
    this.route.fragment.subscribe(name => {
      if(name) {
        let slide = this.fragments.findIndex(fragment => fragment === name) + 1;
        if(slide > 0) {
          this.initSlide = slide;
        }
      }
    });
    this.slider.stateAsObservable.subscribe(state => {
      if(state == 1) {
        this.router.navigate(['/']);
      } else {
        this.router.navigate(['/'], {fragment: this.fragments[state - 1]});
      }
    });
  }
}
