/**
 * @since Wed Aug 29 2018
 * @author Charles Gouldmann - Klean
 */


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

// Types
import { PageType }         from '@k-types/page'

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


@Component({
    moduleId: module.id+'',
    selector: 'basic-page',
    templateUrl: 'tpl.basic-page.pug',
    styleUrls: ['./sty.basic-page.scss']
})
export class BasicPageView implements OnInit {

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

    // ---- Variables ---- \\
    pageContent: PageType
    inputs: any[]

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

    ngOnInit() {
        // Get current location, strip first occurrence of `/`
        this.route.params.subscribe((params: any) => {
            console.log('params:', params)

            let urlKey:string = params.page
            if(urlKey.match(/\/.+/)){
                urlKey = urlKey.replace('/', '')
            }

            if(params.child) {
                urlKey = params.page+'/'+params.child
            }
            console.log('URLKEY')
            console.log(urlKey)

            this.getPageContent(urlKey)
        })

    }


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

    /**
     * Gets page content from service and sets `pageContent` to result.
     * Also sets `title` and `description` SEO content based on result.
     * 
     * If an error occurs a simple error message is returned instead
     * 
     * @param urlKey 
     * 
     * TODO: 1) Add transfer states
     * TODO: 2) Update error message to be more dynamic. Possibly using translates
     */
    getPageContent(urlKey: string) {
        // Get page content
        this.pageService.getPageContent(urlKey)
            .then((content) => {
    
                this.pageContent = content

                // Set SEO
                if(!!content.meta) {
                    this._seo.setTitle(content.meta.title)
                    this._seo.updateTag('description', content.meta.description)
                }
            }).catch((err) => {

                

                // Display error message
                this.pageContent = {
                    'id': 7357,
                    'created': '',
                    'title': 'Undskyld, der er sket en fejl',
                    'modified': '',
                    'urlKey': '',
                    'fullUrlKey': '',
                    'linkOnly': '',
                    'linkUrl': '',
                    'sort': '',
                    'parent': '',
                    'meta': {
                        'title': '',
                        'description': ''
                    },
                    'image': {
                        'thumbnail': '',
                        'medium': '',
                        'large': '',
                        'full': ''
                    },
                    'content': 'Undskyld, der skete en fejl. Prøv at genindlæse siden eller kontakt os hvis fejlen består.',
                    'forms': []
                }
            });
    }
}