import { Component }                from '@angular/core'
import { Router, NavigationEnd }    from '@angular/router'

import { HelperService }            from '@k-services/svc.helper'
import { SeoService }               from '@k-services/svc.seo'
import { PageService }              from '@k-services/svc.page'

@Component({
  moduleId: module.id+'',
  selector: 'frontpage',
  templateUrl: './tpl.frontpage.pug',
  styleUrls: ['./sty.frontpage.scss']
})
export class FrontpageView  {

  constructor(
    private _router: Router,
    private _page: PageService,
    private _helper: HelperService,
    private _seo: SeoService
  ) {
    // Scroll to top 
    _router.events.subscribe(e => {
      if (e instanceof NavigationEnd) {
          this._helper.scrollTo()
      }
    })

    this.getPageSeo()
  }


  // ---- Lifecycle hooks ---- \\



  // ---- Functions ---- \\

  /**
   * Get the page SEO content and set `title` and `description`
   */
  getPageSeo() {
    // Get page info
    this._page.getPageContent('home')
      .then((content) => {

        // Then set SEO
        this._seo.setTitle(content.meta.title)

        if(!!content.meta && !!content.meta.description)
          this._seo.updateTag('description', content.meta.description)
      })
  }
}