import { Component, OnInit }        from "@angular/core";
import { Router }                   from '@angular/router';
import { Location }                 from '@angular/common';

import { MaterialType }             from '../../../types/material';
import { MaterialService }          from '../../../services/svc.material';

import { PageService }              from '../../../services/svc.page';
import { PageType }                 from '../../../types/page';
import { SeoService }               from '../../../services/svc.seo';
import { HelperService }            from '../../../services/svc.helper'


@Component({
    moduleId: module.id+ '',
    selector: 'material-view',
    templateUrl: './tpl.material-page.pug',
    styleUrls: ['./sty.material-page.scss'],
    providers: [MaterialService]
})

export class MaterialPageView {
   
    constructor(
        private _materialService: MaterialService,
        private _location: Location,
        private _page: PageService,
        private _seo : SeoService,
        private _helper: HelperService
    ) {}

    server: string = this._helper.server

    materials: any;
    pageContent: PageType;

    ngOnInit(): void {
        this._location.go('/materials'); // TODO: remove hack
        this.materials = this._materialService.httpGetMaterial('').then((response) => {
            this.materials = response
        });

        // Get current location, strip first occurrence of `/`
        let urlKey:string = this._location.path();
        if(urlKey.match(/\/.+/)){
            urlKey = urlKey.replace('/', '');
        }

        // Get page content
        this._page.getPageContent(urlKey)
        .then((content) => {
            this.pageContent = content;

            // Set SEO
            this._seo.setTitle(content.meta.title);
            this._seo.updateTag('description', content.meta.description);
        });
   }

}