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

import { HelperService }                from '../../../../services/svc.helper'

@Component({
    moduleId: module.id+ '',
    selector: 'media-designer',
    templateUrl: './template.media-designer.pug',
    styleUrls: ['structure.media-designer.scss','visual.media-designer.scss']
})

export class MediaDesignerComponent implements OnChanges {

    @Input('designer') designer: any
    @Input('signature') signature: string
    @Input('house_of') house_of: string

    brokenHouse: string

    constructor(
        private _helper: HelperService
    ) { }

    ngOnChanges() {


        if(!!this.house_of) {
            this.brokenHouse = this.addBreakIfKey(this.house_of, 'House of')
        }
    }


    /**
     * Looks for needle in haystack and adds a break at the needle position
     * 
     * @param haystack 
     * @param needle 
     */
    addBreakIfKey(haystack: string, needle: string): string {
        let foundNeedle = haystack.indexOf(needle)
        let shuffledHaystack = haystack

        if(foundNeedle !== -1) {
            shuffledHaystack = haystack.substring(foundNeedle, foundNeedle + needle.length) + '<br />' + haystack.substring(foundNeedle + needle.length)
        }

        return shuffledHaystack
        
    }

}