import { Component, Input, Output, EventEmitter } from '@angular/core';
import { FormBuilderProvider } from '../../providers/form-builder/form-builder';


@Component({
  selector: 'ws-profile-picture',
  template: `
    Profile picture module
  `,
  styles: [`

  `]
})
export class WsProfilePictureComponent {

  @Input('question') question;
  @Output() answerGiven = new EventEmitter();

  fieldValue:string = '';
  isFieldValid:boolean = false;
  showError:boolean = false;
  
  constructor(public formBuilder: FormBuilderProvider){
    	this.fieldValue = '';
  }

  ngOnInit() {
      if(typeof this.question.value !== 'undefined'){
          this.fieldValue = this.question.value;
      } 
  }

  setAnswer(event,question){
  	let isStringValid = this.validateAnswer(question,event.value);

  	if(!isStringValid){
		this.isFieldValid = false;
  		this.showError = true;
  	}else{
		this.isFieldValid = true;
  		this.showError = false;
  	}

  	if(this.isFieldValid){
  		question.value = event.value;
      question.valid = true;
  		this.answerGiven.emit(question);
  	}
  		
  }


  validateAnswer(question,string){	
  		return this.formBuilder.validateAnswer(question,string);
  }


}

