import { Component, OnInit, Inject, PLATFORM_ID, Renderer2 }    from '@angular/core'
import {Router, NavigationEnd}  from "@angular/router"

import { DOCUMENT, isPlatformBrowser, isPlatformServer } from '@angular/common'

import { MenuService }          from '@k-services/svc.menu'
import { BasketService }        from '@k-services/svc.basket'
import { SharedService }        from '@k-services/svc.shared'
import { HelperService }        from '@k-services/svc.helper'
import { CookieService } 		from 'ng2-cookies';
import { CheckFactory } 		from '@k-services/factories/fac.check';
import { is_local } 			from '@k-settings/app-setup'
import { cookiebot_Id, server } from '@k-settings/store-helper'
@Component({
	moduleId: module.id+ '', // Adds relative path to assets
	selector: 'app',
	styleUrls: [ 'sty.app.scss' ],
	templateUrl: './tpl.app.pug',
	providers: [BasketService]
})

export class AppComponent {
	state: boolean;
	public sessionkey: string = undefined
	validatorInterval: any
	constructor (
		@Inject(PLATFORM_ID) protected _platformId: Object,
		@Inject(DOCUMENT) protected document,
		protected renderer: Renderer2,
		protected _cookie: CookieService,
		public router: Router,
		protected _menuService: MenuService,
		public basketService: BasketService,
		protected _helper: HelperService,
		protected _checkFactory: CheckFactory
	) {


		if(isPlatformServer(this._platformId) && !!cookiebot_Id) {

			const scriptElt = this.renderer.createElement('script');
			this.renderer.setAttribute(scriptElt, 'type', 'text/javascript');
			this.renderer.setAttribute(scriptElt, 'src', `https://consent.cookiebot.com/${cookiebot_Id}/cc.js?renew=false&referer=${server.split('://')[1]}&dnt=false&forceshow=false&cbid=${cookiebot_Id}&whitelabel=false&brandid=Cookiebot&framework=`);
			this.renderer.appendChild(this.document.head, scriptElt);
		}
		



		_menuService.state$.subscribe((result) => {
			this.state = result;

			/**
			 * Lock html page height so you cannot scroll when menu is open
			 */
			if(isPlatformBrowser(this._platformId)) {
				if(result) {
					this._helper.addClass(document.querySelector('html'), 'is-locked');
				}
				else {
					this._helper.removeClass(document.querySelector('html'), 'is-locked');
				}
			}
		})
		

		if(isPlatformBrowser(this._platformId)) {

			if(this._cookie.get('kakesession') && false) {

				this.sessionkey = this._cookie.get('kakesession')
				
			}

			if(!!this.sessionkey && !is_local) {

				this.validateLogin()

				this.validatorInterval = setInterval(() => {

					this.validateLogin()

				}, 30000)

				this.validatorInterval
			}
		}
	}

	validateLogin() {
		
		this._checkFactory.checkLogin(this.sessionkey).subscribe({
			next: () => {

				// Extend the session by 1 hour every 30 seconds
				let newTime = new Date(Date.now() + 1 * 1*3600*1000);
				this._cookie.set('kakesession', this.sessionkey, newTime, '/')
			},
			error: () => {

				// Clears the interval on error
				clearInterval(this.validatorInterval)
				console.error('error observed')
			}
		}
		)
	}

}
