import { Component, OnInit }                        from "@angular/core"
import { Http }                                     from '@angular/http'
import { Router, ActivatedRoute, Params }           from '@angular/router'
import { Location }                                 from '@angular/common'

import { Cookie }                                   from 'ng2-cookies/ng2-cookies'
import { isPlatformBrowser } 						from '@angular/common'

import { LibraryService }                           from '../../../services/svc.library'

@Component({
	moduleId: module.id+ '',
	selector: 'library-view',
	templateUrl: './tpl.library.pug',
	providers: [LibraryService]
})

export class LibraryView {
	access: boolean = false
	ip: string
	url: string = 'https://app.box.com/embed_widget/s/zcy55vtmb2b1sp7fizmpa34h4efgtnec?theme=gray&amp;view=list&amp;sort=name&amp;direction=ASC'
	pageContent: any
	inputs: any
	action: string
	button: string

	constructor(
		private http: Http,
		private libraryService: LibraryService,
		private location: Location
	) {}

	ngOnInit() {
		// Get current location, strip first occurrence of `/`
		let urlKey:string = this.location.path();
		if(urlKey.match(/\/.+/)){
			urlKey = urlKey.replace('/', '');
		}
		this.libraryService.getIp().then(response => {

			this.access = response.access

			// If Access is not set or false, get cookie and change access if accepted
			if(!!!this.access && isPlatformBrowser) {
				if(Cookie.get('library') == 'true') {
					this.access = true;
				}
			}
		})

		this.libraryService.getPageContent(urlKey).then((content) => {
			this.pageContent = content

			// Set forms into the input value, pushed to dynamic fields
			this.inputs = content.forms[0].fields
			this.action = content.forms[0].action
			this.button = content.forms[0].submitLabel
		})
	}
}