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 = "xytk_cs_001";
  // 储存对象
  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 = {
      bt_text:"",
      bt_audio_url:"",
      pic_or_dragonBone:[
        {
          type:"dragonBone",
          audio_url:"",
          pic_url:"",
          "skeJsonData": { "url": "" },
          "texJsonData": { "url": "" },
          "texPngData": { "url": "" },
        },
        {
          type:"img",
          audio_url:"",
          pic_url:"",
          "skeJsonData": { "url": "" },
          "texJsonData": { "url": "" },
          "texPngData": { "url": "" },
        },
        {
          type:"img",
          audio_url:"",
          pic_url:"",
          "skeJsonData": { "url": "" },
          "texJsonData": { "url": "" },
          "texPngData": { "url": "" },
        }
      ],
      // dragonBone:{
      //   "skeJsonData": { "url": "" },
      //   "texJsonData": { "url": "" },
      //   "texPngData": { "url": "" },
      // },
      // dragonBone_audio_url:"",
      // pic_A_url:"",
      // pic_A_audio_url:"",
      // pic_C_url:"",
      // pic_C_audio_url:"",
      bottom_text:"",
      bottom_audio_url:""
    };

    // 获取存储的数据
    (<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() {
    setTimeout(()=>{
      for (let i = 0; i < this.item.pic_or_dragonBone.length; i++) {
        const type = this.item.pic_or_dragonBone[i].type;
        if(type=="img"){
          //@ts-ignore
          document.getElementById('dragonBone'+i).checked  =false; 
          var r = document.getElementById('img'+i)
          //@ts-ignore
          r.checked = true;  
        }else{
          //@ts-ignore
          document.getElementById('dragonBone'+i).checked  =true; 
          var r = document.getElementById('img'+i)
          //@ts-ignore
          r.checked = false;  
        }
      }
    },500);

  }

  setType(val,i){
    this.item.pic_or_dragonBone[i].type = val;
    this.save();
  }
  
  saveAnima(event, group,i) {
    group[i].skeJsonData = event.skeJsonData;
    group[i].texJsonData = event.texJsonData;
    group[i].texPngData = event.texPngData;
    this.save();
  }
  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, key,i) {

    this.item.pic_or_dragonBone[i][key] = e.url;
    this.save();
  }

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, key) {
    this.item[key] = e.url;
    this.save();
  }
  /**
   * 储存音频数据2
   * @param e
   */
   onAudioUploadSuccess2(e, key,i) {
    this.item.pic_or_dragonBone[i][key] = e.url;
    this.save();
  }
  onWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].audio = e.url;
    this.save();
  }

  onBackWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].backWordAudio = e.url;
    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);
  }

}