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

@Component({
  selector: 'app-page5',
  templateUrl: './page5.component.html',
  styleUrls: ['./page5.component.css']
})
export class Page5Component implements OnInit {
  @Input() curPageData: Array<any>
  @Output() getAllAnswer = new EventEmitter<any>()
  selectedObj: Object
  constructor() {}

  ngOnInit() {
    this.selectedObj = {}
    this.setOptionSelected()
  }
  getAnswer(option) {
    if (!this.selectedObj['selected']) {
      this.selectedObj = option
      option['selected'] = true
    } else {
      if (this.selectedObj['optionId'] != option.optionId) {
        this.selectedObj['selected'] = false
        option['selected'] = true
        this.selectedObj = option
      }
    }
    const ret = {
      pageId: this.curPageData['pageId'],
      questions: [
        {
          questionId: this.curPageData['questions'][0].questionId,
          questionName:this.curPageData['questions'][0].questionName,
          options: [option]
        }
      ]
    }
    this.getAllAnswer.emit(ret)
  }
  setOptionSelected() {
    const options = this.curPageData['questions'][0].options
    if (!options) {
      return
    }
    options.forEach(option => {
      if (option['selected']) {
        this.selectedObj = option;
        this.getAnswer(this.selectedObj)
      }
    })
  }
}
