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.scss']
})
export class FormComponent implements OnInit, OnChanges, OnDestroy {

  // 储存数据用
  saveKey = "DataKey_Cocos_ET20";
  // 储存对象
  item;
  aniEnter: any = {
    skeJsonData: {},
    texJsonData: {},
    texPngData: {},
    audioUrl: null,
    time: 1,
  }
  copyQuestionData;

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

  }

  addQuestion() {
    this.item.questionList.push(
      {
        "audio": "",
        "image": "",
        "optionList": [{
          "type": 0,
          "text": "",
          "image": "",
          "optionAudio": "",
          "time": '',
          'voice': '',
        }]
      }
    );
    this.save();
  }

  removeQuestion(idx) {
    this.item.questionList.splice(idx, 1);
    this.save();
  }
  copyQuestion(idx) {
    this.copyQuestionData = this.item.questionList[idx];
  }
  pasteQuestion(idx) {
    this.item.questionList.splice(idx + 1, 0, JSON.parse(JSON.stringify(this.copyQuestionData)));
  }

  addOption(questionIdx) {
    this.item.questionList[questionIdx].optionList.push({
      "type": 0,
      "text": "",
      "image": "",
      "optionAudio": "",
      "time": '',
      'voice': '',
    });
    this.save();
  }

  removeOption(questionIdx, optionIdx) {
    this.item.questionList[questionIdx].optionList.splice(optionIdx, 1);
    this.save();
  }

  getQuestionText(question) {
    let text = '';
    for (let i = 0; i < question.optionList.length; i++) {
      text += question.optionList[i].text;
    }
    return text
  }

  ngOnInit() {
    this.item = {
      questionList: [{
        "audio": "",
        "image": "",
        "optionList": [{
          "type": 0,
          "text": "",
          "image": "",
          "optionAudio": "",
          "time": '',
          'voice': '',
        }]
      }]
    };

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

      console.log("data", data);
      if (data) {
        if (data.aniEnter) {
          this.aniEnter = data.aniEnter;
        }
        if (data.item) {
          this.item = data.item;
        }
      }

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

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {

  }
  onVioceUploadSuccess(e, questionIdx) {
    this.item.questionList[questionIdx].voice = e.url;
    this.save();
  }
  onAudioUploadSuccess(e, questionIdx) {
    this.item.questionList[questionIdx].audio = e.url;
    this.save();
  }
  onOptionUploadSuccess(e, questionIdx, optionIdx) {
    this.item.questionList[questionIdx].optionList[optionIdx].optionAudio = e.url;
    this.save();
  }

  onQuestionAudioUploadSuccess(e) {
    // this.item.questionAudio = e.url;
    // this.save();
    // questionText
  }
  onImageUploadSuccessByItem(e, questionIdx) {
    this.item.questionList[questionIdx].image = e.url
    console.log(this.item.questionList[questionIdx])
    this, this.refresh()
    this.save();
  }
  onOptionImageUploadSuccessByItem(e, option) {
    option.image = e.url;
    this.save();
  }
  radioClick(questionIdx, optionIdx) {
    this.item.questionList[questionIdx].rightOptionIdx = optionIdx;
    this.changeDetectorRef.markForCheck();
    this.changeDetectorRef.detectChanges();
    this.save();
  }

  saveAnima(event, group) {
    group.skeJsonData = event.skeJsonData;
    group.texJsonData = event.texJsonData;
    group.texPngData = event.texPngData;
    this.save();
  }
  saveAnima2(event, group) {
    group.skeJsonData2 = event.skeJsonData;
    group.texJsonData2 = event.texJsonData;
    group.texPngData2 = event.texPngData;
    this.save();
  }


  saveAniEnter(e) {
    console.log(e);
    this.save();
  }
  onAudioUploadAniEnter(e) {
    this.aniEnter.audioUrl = e.url
    this.save();
  }
  onTxtChangeAniEnter() {
    this.save()
  }
  /**
   * 储存数据
   */
  save() {
    let obj: any = {
      aniEnter: this.aniEnter,
      item: this.item
    };
    (<any>window).courseware.setData(obj, null, this.saveKey);

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

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

}
