import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { ComponentBase } from './ComponentBase';
import { getDefExercises, getDefPic, getDefWord } from './beans';


@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent extends ComponentBase implements OnInit, OnChanges, OnDestroy {

  // 储存数据用
  saveKey = "OP71";
  // 储存对象
  item: any = {
    guideAudioUrl1:'',
    guideAudioUrl2:'',
    guideAudioUrl3:'',
    pic:{
      picUrl:'',
      audioUrl:''
    }
  };

  init(){
    //练习
    this.item.exercises = this.initExercises(this.item.exercises);

    if(!this.item.pic){
      this.item.pic = getDefPic();
    }
  }

  private initExercises(item){

    let exercises = item;

    if(!exercises){

      exercises = getDefExercises();
    }

    if(!exercises.wordArr){
      exercises.wordArr = [];
    }

    for(let i = exercises.wordArr.length; i < 3; ++ i){
        let word = getDefWord();

        exercises.wordArr.push(word);
    }

    if(!exercises.wordArr2){
      exercises.wordArr2 = [];
    }

    for(let i = exercises.wordArr2.length; i < 3; ++ i){
        let word = getDefWord();

        exercises.wordArr2.push(word);
    }

    return exercises;
  }

  deleteAudio(item, key){
    item[key] = null;
    this.save();
  }

  saveWord(exercises, key){

    let word1 = exercises[key][0].val ? exercises[key][0].val : "";

    let word2 = exercises[key][1].val ? exercises[key][1].val : ""

    exercises[key][2].val =  word1 + word2;
    this.save();
  }
}