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


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

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

  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();
  }

  onDeleteError(listId, id) {
    this.item.list[listId].error = this.item.list[listId].error.filter(li => {
      return li.id != id;
    })
    for (let i = 0; i < this.item.list[listId].error.length; i++) {
      this.item.list.error[i].id = 2000 + i + 1;
    }
    this.save();
  }
  onAddError(listId) {
    this.item.list[listId].error.push({
      id: 2000 + (this.item.list[listId].error.length + 1),
      text: "",
      pic: "",
      type: "pic",
    })
    this.save();
  }
  onAddItem() {
    this.item.list.push(this.createItem())
    this.save();
  }
  onDeleteItem(listId) {
    this.item.list.splice(listId, 1);
    for (let i = 0; i < this.item.list.length; i++) {
      this.item.list[i].id = i;
    }
    this.save();
  }
  onMoveUp(listId) {
    this.item.list[listId - 1].id = listId;
    this.item.list[listId].id = listId - 1;
    this.item.list.sort((A, B) => {
      return A.id - B.id;
    })
    this.save();
  }
  onMoveDown(listId) {
    this.item.list[listId + 1].id = listId;
    this.item.list[listId].id = listId + 1;
    this.item.list.sort((A, B) => {
      return A.id - B.id;
    })
    this.save();
  }

  createItem() {
    return {
      id: this.item.list.length,
      stem: {
        audio: "",
      },
      right: [{
        id: 1001,
        text: "",
        pic: "",
        type: "pic",
      }],
      error: [{
        id: 2001,
        text: "",
        pic: "",
        type: "pic",
      }, {
        id: 2002,
        text: "",
        pic: "",
        type: "pic",
      }, {
        id: 2003,
        text: "",
        pic: "",
        type: "pic",
      }]
    }
  }

  ngOnInit() {
    this.item = { list: [] };

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

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

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

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {

  }


  /**
   * 题干音频
   * @param e
   */
  onStemAudioUploadSuccess(e, stem) {
    stem.audio = e.url;
    this.save();
  }


  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, item, key) {
    item.pic = e.url;
    // this.item[key] = e.url;
    this.save();
  }

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

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

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

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

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

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

}