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

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

  }

  addPage() {
    console.log(this.item)
    this.item.pageList.push(
      {
        "pageType": "Image",
        "pageAudio": "",
        "pageText": "",
        "pageImage": "",
        "textColor": "A",
        "pageDragonBone": {
          "skeJsonData": { "url": "" },
          "texJsonData": { "url": "" },
          "texPngData": { "url": "" },
        },
      }
    );
    this.save();
  }

  removePage(idx) {
    this.item.pageList.splice(idx, 1);
    this.save();
  }
  copyPage(idx) {
    this.copyQuestionData = this.item.pageList[idx];
  }
  pastePage(idx) {
    this.item.pageList.splice(idx + 1, 0, JSON.parse(JSON.stringify(this.copyQuestionData)));
    this.save();
  }

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

  ngOnInit() {
    this.item = {
      "haveCover": false,
      "coverType": "Image",
      "coverImage": "",
      "coverAudio": "",
      "coverText": "",
      "coverDragonBone": {
        "skeJsonData": { "url": "" },
        "texJsonData": { "url": "" },
        "texPngData": { "url": "" },
      },
      "titleAudio": "",
      "titleText": "",
      "titleTextColor":"A",
      "pageList": []
    };

    // 获取存储的数据
    (<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.pageList[questionIdx].pageAudio = e.url;
    this.save();
  }
  onOptionUploadSuccess(e, questionIdx, optionIdx) {
    this.item.pageList[questionIdx].optionList[optionIdx].optionAudio = e.url;
    this.save();
  }

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

  onCoverAudioUploadSuccess(e) {
    this.item.coverAudio = e.url;
    this.save();
  }

  radioClick(questionIdx, optionIdx) {
    this.item.pageList[questionIdx].rightOptionIdx = optionIdx;
    this.changeDetectorRef.markForCheck();
    this.changeDetectorRef.detectChanges();
    this.save();
  }


  handleCoverTypeChange(e) {
    this.item.coverType = e;
    if (e == 'Spine') {

    } else if (e == 'Image') {

    }
    this.save()
  }

  handlePageTypeChange(e, i) {
    this.item.pageList[i].pageType = e;
    if (e == 'Spine') {

    } else if (e == 'Image') {

    }
    this.save()
  }

  onCoverImageUploadSuccess(e) {
    this.item.coverImage = e.url;
    this.save()
  }

  onPageImageUploadSuccess(e, i) {
    this.item.pageList[i].pageImage = e.url;
    this.save()
  }


  saveCoverDragonBone(e, group) {
    group.skeJsonData = e.skeJsonData;
    group.texJsonData = e.texJsonData;
    group.texPngData = e.texPngData;
    this.save();
  }
  savePageDragonBone(e, group) {
    group.skeJsonData = e.skeJsonData;
    group.texJsonData = e.texJsonData;
    group.texPngData = e.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);
  }

}
