import { Component, Input, OnInit, OnChanges } from '@angular/core'

import { BlockService } from '../services/svc.block'
import { TransferState, makeStateKey } from '@angular/platform-browser'
import { debug }	from '@k-settings/store-helper'

const BLOCKS_ID = makeStateKey('blockskey')

interface Block {
	id: number;
	storeId: number;
	blockId: string;
	content: {
		template?: string;
		header?: string;
		text?:string;
		text_alignment?: string;
		cta_label?: string;
		cta_type?: string;
		cta_url?: string;
		cta_alignment?: string;
		image_mobile?: string;
		image?: string;
	}
	start: string;
	end: string;
	enabled: boolean;
	locked: boolean;
	sort: number;
	currentState: string;
	typeId: number;
	origin: string;
	block;
}


@Component({
	selector: 'blocks',
	templateUrl: 'tpl.block.pug'
})

export class BlockView implements OnInit, OnChanges {

	// ---- Vars ---- \\
	@Input('blockName') blockName
	blocks: Block

	constructor(
		private _blockService : BlockService,
		private state: TransferState
	) { }




	// ---- Lifecycle hooks ---- \\
	ngOnInit() {

	}


	ngOnChanges() {

		// this.blocks = this.state.get(BLOCKS_ID, null as any)

		if(!this.blocks || debug) {

			if(!!this.blockName) {

				// if frontblock is referenced, remove the blockName
				if(this.blockName == 'frontblock')
					this.blockName = ''

				this._blockService.getBlocks(this.blockName).then((response: any) => {

					this.state.set(BLOCKS_ID, response as any)
					this.blocks = response
				})
			}
		}
	}



	
	// ---- Functions ---- \\

	getBlocks(id:string) {
		
	}
}