/**
 * @since Mon Jun 04 2018
 * @author Charles Gouldmann - Klean
 */

import { Component, OnInit } from '@angular/core'
import { DomSanitizer, SafeResourceUrl, SafeUrl }   from '@angular/platform-browser'
import { makeStateKey, TransferState } from '@angular/platform-browser'

// Shared Services
import { MenuService }          from '@k-services/svc.menu'
import { HelperService }        from '@k-services/svc.helper'

// Local service
import { FooterService }        from '@k-core/modules/footer/services/svc.footer'


// Keys for TransferState data
const SITEINFO = makeStateKey('siteInfoKey')
const FOOTERMENUS = makeStateKey('footerMenusKey')


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

export class FooterTemplate implements OnInit {

    constructor(
        protected _sanitizer: DomSanitizer,
        public _footerService: FooterService,
        public _menuService: MenuService,
        public _helper: HelperService,
        public state: TransferState
    ) {}

    // ---- Variables ----
    siteInfo: any
    categories
    categoryLabel		    = this._helper.categories

    footerMenus             = new Map()
    footerMenusToGet        = ['footer-menu-1', 'footer-menu-2', 'footer-menu-3']

    date                    = new Date
    currentYear             = this.date.toLocaleDateString('da-dk', {year: 'numeric'})




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

    ngOnInit() {
        // pulls and adds SiteInfo to TransferState
        this.siteInfo = this.state.get(SITEINFO, null as any)

        if(!this.siteInfo) {

            this._helper.siteData$.subscribe((response: any) => {
                this.siteInfo = response
                this.state.set(SITEINFO, response as any)
            })
        }

        this.getFooterMenus()
    }




    
    // ---- Functions ----

    /**
     * Remove spaces from supplied string
     * 
     * @param text 
     */
    stripWhitespace(text : string) {
        return text.replace(new RegExp(' ', 'g'), '')
    }



    /**
     * Sanatize given url
     * 
     * @param path 
     */
    sanitize(path:string) {
        return this._sanitizer.bypassSecurityTrustStyle('url('+path+')')
    }



    /**
     * Get footer menus based on given names and add results to transfer state
     * 
     * TODO: Get transfer state to work with multiple menus. Eg. Promise.all()
     */
    getFooterMenus() {
        // Get footermenu from state
        //this.footerMenus = this.state.get(FOOTERMENUS, new Map() as any)

        // Only get footermenus if they dont exist in state
        if(this.footerMenus.size === 0) {
            for(let menu of this.footerMenusToGet) {
                this._menuService.getMenu(menu).then((result) => {
                    this.footerMenus.set(menu, result)
                })
            }

            // if(this.footerMenus.size) {
            //     console.log('setting state')
            //     this.state.set(FOOTERMENUS, this.footerMenus as any)
            // }

        }

    }
}