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

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

  }

  addQuestion() {
    this.item.questionList.push(
      {
        "pageAudio":"",
        "rightOptionIdx": 0,
        "optionList": [{
          "text": "",
          "optionDragonBone": {
            "skeJsonData": { "url": "" },
            "texJsonData": { "url": "" },
            "texPngData": { "url": "" },
          },
          "optionAudio": ""
        },
        {
          "text": "",
          "optionDragonBone": {
            "skeJsonData": { "url": "" },
            "texJsonData": { "url": "" },
            "texPngData": { "url": "" },
          },
          "optionAudio": ""
        },
        {
          "text": "",
          "optionDragonBone": {
            "skeJsonData": { "url": "" },
            "texJsonData": { "url": "" },
            "texPngData": { "url": "" },
          },
          "optionAudio": ""
        }]
      }
    );
    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({
      "text": "",
      "optionDragonBone": {
        "skeJsonData": { "url": "" },
        "texJsonData": { "url": "" },
        "texPngData": { "url": "" },
      },
      "optionAudio": ""
    });
    this.save();
  }

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

  ngOnInit() {
    this.item = {
      questionAudio: "",
      questionText: "",
      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() {

  }
  onPageUploadSuccess(e, questionIdx) {
    this.item.questionList[questionIdx].pageAudio = 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
  }

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

  /**
   * 储存数据
   */
  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);
  }

}
