import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { alphaItem } from '../play/Unit';


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

  // 储存数据用
  saveKey = "test_001";

  dataArr: DataStruct[] = []

  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {

  }

  // createShell() {
  //   this.item.wordList.push({
  //     word: '',
  //     audio: '',
  //     backWord: '',
  //     backWordAudio: '',
  //   });
  //   this.save();
  // }

  // removeShell(idx) {
  // this.item.wordList.splice(idx, 1);
  // this.save();
  // }

  ngOnInit() {

    this.dataArr = [];

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

      if (data) {
        this.dataArr = data.length ? data : [];
      }

      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {

  }

  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, index) {
    let one = this.dataArr[index]
    one.pic_url = e.url
    if (one.txt == '' || one.txt == null) return
    this.save();
  }

  /**
   * 文字发生改变
   */
  onTxtChange(index) {
    let one = this.dataArr[index]
    if (one.pic_url == '' || one.pic_url == null) return
    this.save()
  }

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

  /**
   * 移除音频数组
   */
  onAudioRemoveSuccess(e, index) {
    this.dataArr[index].audio_url = ''
    this.save()
  }

  /**
   * 删除卡片
   */
  onRemoveData(index) {
    let data = this.dataArr[index]
    if (data) {
      this.dataArr.splice(index, 1)
      this.save()
    }
  }

  /** 添加卡片 */
  onAddData() {
    if (this.dataArr.length == 0) {
      this.dataArr.push(new DataStruct())
    } else {
      let one = this.dataArr[this.dataArr.length - 1]
      if (one.txt == '' || one.txt == null) {
        alert('请上传文字')
        return
      }
      if (one.pic_url == '' || one.pic_url == null) {
        alert('请上传图片')
        return
      }
      this.dataArr.push(new DataStruct())
    }
  }



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

    this.refresh();
    console.log('this.item = ' + JSON.stringify(this.dataArr));
  }

  // onWordAudioUploadSuccess(e, idx) {
  //   this.dataArr[this.dataIndex].
  //   this.item.wordList[idx].audio = e.url;
  //   this.save();
  // }

  // onBackWordAudioUploadSuccess(e, idx) {
  //   this.item.wordList[idx].backWordAudio = e.url;
  //   this.save();
  // }


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


}
class DataStruct {
  txt: string
  pic_url: string
  audio_url: string
}