import { Component, OnInit, Input } from '@angular/core'
import { Cookie } 					from 'ng2-cookies'

import { AppHelper }	from '@k-settings/app-helper'

@Component({
	selector: 'cookie-handler',
	templateUrl: './tpl.cookie-handler.pug',
	styleUrls: ['./sty.cookie-handler.scss']
})
export class CookieHandlerComponent implements OnInit {

	// Variables ----
	@Input('title') title
	@Input('content') content
	@Input('button-label') buttonLabel
	@Input('readmore-label') readMoreLabel
	@Input('readmore-url') readMoreUrl

	isVisible: boolean = false
	dismissed: boolean = true

	cookieName = AppHelper.cookieName + ':accepted cookies'

	constructor(
	) { }


	// Lifecycle hooks ----
	ngOnInit() {
		this.showHandler()
	}


	// Functions ----

	/**
	 * Show toggle instantly and delay info by 1s
	 */
	showHandler() {
		if(!Cookie.check(this.cookieName) ) {
			this.isVisible = true

			setTimeout(() => { 
				this.toggle()
			}, 1000)
		}
	}


	/**
	 * Closes the cookie info and then hides all.
	 * A cookie is also set so cookie handler remains closed on next visit
	 */
	close() {
		Cookie.set(this.cookieName, 'accepted')
		this.toggle()

		setTimeout(() => { 
			this.isVisible = false
		}, 500)
	}

	
	/**
	 * dismisses the cookie handler
	 */
	dismiss() {
		this.dismissed = true
	}

	/**
	 * Toggle the cookie handler
	 */
	toggle() {
		this.dismissed = !this.dismissed
	}

}