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 = "dfzx_cocos_pinjuzi";
  // 储存对象
  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();
  }

  ngOnInit() {

    this.item = {};

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

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

      this.item = JSON.parse('{"groupArr":[{"ques":"","optionArr":[{"text":"AAaa","isEmpty":true,"audio_url":"http://staging-teach.cdn.ireadabc.com/c42408cda175ea4b754b47bba6eeb692.mp3"},{"text":"bbb"},{"text":"ccc","isEmpty":true,"audio_url":"http://staging-teach.cdn.ireadabc.com/3c070a33d021725219ab7eec9e60cb39.mp3"},{"text":"ddddd"},{"text":"ee"},{"text":"fff"}],"wrongArr":[{"text":"zzz"},{"text":"yyyyy"},{"text":"ccc"},{"text":"vvv"},{"text":"bbb"},{"text":"nnnnnn"}],"audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3","word_audio_url":"http://staging-teach.cdn.ireadabc.com/85cff126986f5cc11b05e95181da5869.mp3"},{"ques":"","optionArr":[{"text":"aa"},{"text":"cc","isEmpty":true,"audio_url":"http://staging-teach.cdn.ireadabc.com/baae6b4c92d8a95e184d6d18542e0d21.mp3"},{"text":"d"},{"text":"bbbb","isEmpty":true,"audio_url":"http://staging-teach.cdn.ireadabc.com/79a67cfeb1fee086076bc7f46e1524f1.mp3"}],"wrongArr":[{"text":"b2"},{"text":"eee"},{"text":"fff"},{"text":"ggg"},{"text":"hhh"},{"text":"ggg"},{"text":"ccc"}],"pic_url":"http://staging-teach.cdn.ireadabc.com/f6f4bbef497409fef3c8f1d375893829.png","audio_url":"http://staging-teach.cdn.ireadabc.com/440cf9d57752b30691d90ed565e8c924.mp3","word_audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3"},{"ques":"","optionArr":[{"text":"wwwwwwwwwww"},{"text":"cc","isEmpty":true}],"wrongArr":[{"text":"v"},{"text":"v"},{"text":"x"},{"text":"z"}],"audio_url":"http://staging-teach.cdn.ireadabc.com/a5fa20e0c6024421b0d9a7007c303503.mp3","word_audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3"}]}');
      
      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {

    console.log('this.item: ', this.item);
    if (!this.item.groupArr) {
      this.item.groupArr = [];
    }
  }



  addGroupBtnClick() {
    this.item.groupArr.push({
      ques: '',
      optionArr: [],
      wrongArr: [],
    })
    this.save();
  }

  onBtnDeleteGroup(i) {
    this.item.groupArr.splice(i, 1);
    this.save();
  }

  addOptionBtnClick(group) {
    group.optionArr.push({})
    this.save();
  }

  addWrongBtnClick(group) {
    group.wrongArr.push({})
    this.save();
  }


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

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

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

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

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


  radioChange(e, option, key) {
    option[key] = e;
    this.save();
  }

  onBtnDeleteOption(i, arr) {
    arr.splice(i, 1);
    this.save();
  }

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

}