import { Component, OnInit, OnChanges, Inject } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { AppHelper }                from '@k-settings/app-helper'

import { UserService }                          from '../../services/svc.user'
import { RestrictedCmsService }                 from '../../services/svc.restricted-cms'

@Component({
    moduleId: module.id+ '',
    selector: 'partial-user-userpage',
    templateUrl: './tpl.userPage.pug',
    styleUrls: ['sty.userPage.scss']
})

export class userPagePartial {

    // Variables

    content: any;

    constructor(
        private _user: UserService,
        private _restrict: RestrictedCmsService,
        private _router: Router,
        private route: ActivatedRoute
    ) { }

    // Lifehooks
    ngOnInit() {

        this.route.params.subscribe((params: any) => {
            console.log(params)



            let urlKey:string = params.url

            if(urlKey.match(/\/.+/)){
                urlKey = urlKey.replace('/', '');
            }

            if(true) {
                urlKey = AppHelper.press + '/'+urlKey
            }

            this.getPageContent(urlKey)
        })
        // Get current location, strip first occurrence of `/`

    }

    // Functions
    getPageContent(urlkey) {

        this._restrict.getPageContent(urlkey).then(response => {

            if(!!response.status && response.status == 'error')
                this._router.navigate(['/'], { queryParams: { returnUrl: this._router.url }});

            this.content = response;
        })
    }
}