import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef} from '@angular/core';
import defauleFormData from '../../assets/play/default/formData/defaultData.js'

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.scss']
})


export class FormComponent implements OnInit, OnChanges, OnDestroy {

  _item: any;
  KEY = 'DataKey_PU03';
  options = []

  set item(item) {
    this._item = item;
  }
  get item() {
    return this._item;
  }

  @Output()
  update = new EventEmitter();
  constructor(private appRef: ApplicationRef) {

  }

  ngOnInit() {
    this.item = {};
    this.item.contentObj = {};
    const getData = (<any> window).courseware.getData;
    getData((data) => {
      // console.log("读取数据", data)
      if (data) {
        this.item = data;
      } else {
        this.item = {};
      }
      if ( !this.item.contentObj ) {
        this.item.contentObj = {};
      }
      this.init();
      // this.refresh();
      this.save()
    }, this.KEY);
  }
  ngOnChanges() {

  }

  ngOnDestroy() {
  }

  init() {
    if(!this.item.contentObj || !this.item.contentObj.mainQuestion){
      this.item.contentObj = defauleFormData
    }
    this.options = []
    this.item.contentObj.textDown.forEach((item, index) => {
      this.options.push(index)
    });
  }

  cardItemData(){
    return {audio_url:"", type:"text", text:"", image_url:""}
  }

  cardChoiceData(){
    return { isText: true, text: "", image_url: "" }
  }


  initData() {

  }


  handleMoveItemUp(index){
    if(index!=0){
      this.item.contentObj.dataArray[index] = this.item.contentObj.dataArray.splice(index-1, 1, this.item.contentObj.dataArray[index])[0];
    }else{
      this.item.contentObj.dataArray.push(this.item.contentObj.dataArray.shift());
    }
    this.save()
  }

  handleMoveItemDown(index){
    if(index!=this.item.contentObj.dataArray.length-1){
      this.item.contentObj.dataArray[index] = this.item.contentObj.dataArray.splice(index+1, 1, this.item.contentObj.dataArray[index])[0];
    }else{
      this.item.contentObj.dataArray.unshift( this.item.contentObj.dataArray.splice(index,1)[0]);
    }
    this.save()
  }

  deleteItem(item, index){
    item.splice(index,1)
    this.save()
  }

  addItem(item, type) { 
    let newItem = null
    switch(type){
      case "card": newItem = {text:"", lineTo: []}
      default: newItem = {text:"", lineTo: []}
    }
    this.options = []
    this.item.contentObj.textDown.forEach((item, index) => {
      this.options.push(index)
    });

    item.push(newItem);
    this.saveItem();
  }

  onImageUploadSuccessByItem(e, item, key) {
    item[key] = e.url
    this.save(); 
  }

 
  onAudioUploadSuccessByItem(e, item, key) { 
    item[key] = e.url
    this.save();
  }


  saveItem() {
    this.save();
  }

  save() {
    (<any> window).courseware.setData(this.item, null, this.KEY);
    this.refresh();
    console.log("保存", this.item)
  }

  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }
}

