import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef} from '@angular/core';



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

  // 储存数据用
  saveKey = "test_008";
  // 储存对象
  item;

  wordArr = [];

  constructor(private appRef: ApplicationRef) {

  }


  ngOnInit() {

    this.item = {};

    this.item.title = {
      t_font_color:'#000000',
      t_font:'BRLNSDB',
      t_font_size:40
    };

    // 获取存储的数据
    (<any> window).courseware.getData((data) => {

      if (data) {
        this.item = data;
      }

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

      this.init();
      this.refresh();

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {

    //标题
    if(!this.item.title){
      this.item.title = {
        t_font_color:'#000000',
        t_font:'BRLNSDB',
        t_font_size:40
      };
    }

    if (this.item.wordArr) {
      this.wordArr = this.item.wordArr;
    } else {
      this.wordArr = this.getDefaultWordArr();
      this.item.wordArr = this.wordArr;
    }
  }


  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, key) {

      this.item[key] = e.url;
      this.save();
  }

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, item, key) {
    item[key] = e.url;
    this.save();
  }

  onAudioUploadSuccessByItem(e, item, key) {
    item[key] = e.url;
    this.save();
  }

  /**
   * 储存数据
   */
  save() {
    (<any> window).courseware.setData(this.item, null, this.saveKey);
    this.refresh();
  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }

  deleteItem(index) {
    if (index !== -1) {
      this.wordArr.splice(index, 1);
    }

    // this.update.emit(this.item);
    this.save();
  }

  addItem() {
    let item = this.wordItemData();
    this.wordArr.push(item);

    this.saveItem();
  }

  // addLetterItem(letters){

  //   let letter = this.letterData();

  //   letters.push(letter);

  //   this.save();
  // }

  deleteLetterItem(letters, index){
    console.log(letters);
    if (index !== -1) {
      letters.splice(index, 1);
    }
    this.save();
  }


  wordItemData() {
    return {
            left: {
              word_val: '',
              word_audio_url: '',
              letters: []
            },
            right: {
              word_val: '',
              word_audio_url: '',
              letters: []
            }
    };
  }

  // letterData() {
  //   return {
  //     letter_val: '',
  //     letter_color: 'C04',
  //     is_: false
  //   };
  // }

  saveWordItem(index,type) {
    let val = this.wordArr[index][type].word_val;
    this.wordArr[index][type].letters = [];
    for(let i=0;i<val.length;i++){
      this.wordArr[index][type].letters.push({
        letter_val: val[i],
        letter_color: 'C04',
        is_: '0'
      });
    }
    this.save();
  }

  saveItem() {
    this.save();
  }

  getDefaultWordArr() {
    let arr = [];
    return arr;
  }

}

