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

import { MediaService }             from '../../../services/svc.media'
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: 'media-page-view',
    templateUrl: './tpl.media-page.pug',
    styleUrls: ['./sty.media-page.scss'],
    providers: [MediaService]
})

export class MediaPageView {

    media: any;
    mediaObject = [];
    pageContent: PageType;

    constructor(
        private mediaService: MediaService,
        private _location: Location,
        private _page: PageService,
        private _seo : SeoService,
        private _helper: HelperService
    ) {}

    server: string = this._helper.server
    imageAddress = this.server + '/blocks/uploads/';

    ngOnInit(): void {

        console.log(this.imageAddress)

        this.mediaService.getMediaPagesList()
            .then((response) => {
                this.media = response.blocks;
        })


        // 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);
        });
   }

   cleanString(text: string) {
       return text.replace('-', ' ')
   }
}
