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 = "ww_07";
  // 储存对象
  item;

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

  }

  addQuestion() {
    this.item.questionList.push({
      auido: '',
      image: '',
      text: '',
      words: []
    });
    this.save();
  }

  removeQuestion(idx) {
    this.item.questionList.splice(idx, 1);
    this.save();
  }

  removeImage(idx) {
    this.item.questionList[idx].image = '';
    this.save();
  }

  updateQuestionWords(idx, str) {
    const audios = {};
    const words = this.item.questionList[idx].words;
    words.forEach(item => {
      audios[item.word] = item.audio;
    });
    this.item.questionList[idx].words = str
      .replaceAll('.', ' . ')
      .replaceAll(',', ' , ')
      .replaceAll('?', ' ? ')
      .replaceAll('!', ' ! ')
      .replaceAll(/  /g, ' ')
      .trim()
      .split(' ')
      .map(word => {
        return { audio: audios[word] || '', word: word };
      });
  }

  ngOnInit() {

    this.item = {
      title: '',
      titleAudio: '',
      questionList: [],
    };

    // 获取存储的数据
    (<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() {

  }

  onQuestionLabelUpdate(idx) {
    this.updateQuestionWords(idx, this.item.questionList[idx].text);
    this.save();
  }

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

  onQuestionImageUploadSuccess(e, idx) {
    this.item.questionList[idx].image = e.url;
    this.save();
  }

  onTitleAudioUploadSuccess(e) {
    this.item.titleAudio = e.url;
    this.save();
  }

  removeTitleAudio() {
    this.item.titleAudio = '';
    this.save();
  }

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

  removeQuestionAudio(idx) {
    this.item.questionList[idx].audio = '';
    this.save();
  }

  onWordAudioUploadSuccess(e, questiongIdx, wordIdx) {
    this.item.questionList[questiongIdx].words[wordIdx].audio = e.url;
    this.save();
  }

  removeWordAudio(questiongIdx, wordIdx) {
    this.item.questionList[questiongIdx].words[wordIdx].audio = '';
    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);
  }

}