import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef} from '@angular/core';

const defaultData: any = {
  version: "1.1",
  bgmusic: "",
  dataArray:[]
}

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

export class FormComponent implements OnInit, OnChanges, OnDestroy {

  _item: any;
  contentObj: any = {
    bgMusic:String,
    dataArray:[]
  }
  hotZoneItemArr: Array<Object> = [];
  bgItem: Object;

  KEY = 'DataKey_East_L230';

  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 (Object.keys(this.item.contentObj).length != 0 && this.item.contentObj.version && this.item.contentObj.version==defaultData.version) {
      // console.log("读取数据", this.item.contentObj)
      this.contentObj = this.item.contentObj;
    } else {
      this.contentObj = defaultData;
      // console.log("使用默认数据", this.contentObj)
      this.item.contentObj = this.contentObj;
    }
  }

  cardItemData(){
    return  {
      audio_url: "",
      options: [
        {type:"Text", image_url:"", text:"", audio_url:"", isCorrect:true},
        {type:"Text", image_url:"", text:"", audio_url:"", isCorrect:false},
        {type:"Text", image_url:"", text:"", audio_url:"", isCorrect:false},
        {type:"Text", image_url:"", text:"", audio_url:"", isCorrect:false}
      ]
    }
  }

  cardChoiceData(){
    return {type:"Text", image_url:"", text:"", audio_url:"", isCorrect:false}
  }

  getDefaultPicArr() {
    let arr = defaultData; 
    return arr;
  }

  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(index){
    this.item.contentObj.dataArray.splice(index,1)
    this.save()
  }
  addChoice(questionIndex) { 
    let item = this.cardChoiceData();
    this.item.contentObj.dataArray[questionIndex].options.push(item)
    this.saveItem();
  }

  deleteChoice(questionIndex, choiceIndex){
    if (questionIndex !== -1 && choiceIndex !== -1) {
      this.item.contentObj.dataArray[questionIndex].options.splice(choiceIndex, 1);
    }
    this.save();
  }

  onImageUploadSuccessByItem(e, item) {
    item.image_url = e.url
    this.save(); 
  }

  onImageUploadSuccess_Container(e) {
    this.item.contentObj.cardContainerSetting.image_url = e.url
    this.save();
  }
 
  onAudioUploadSuccessByItem(e, item) { 
    item.audio_url = e.url; 
    this.save();
  }

  onTitleAudioUploadSuccess(e) {
    // this.contentObj.bgMusic = e.url;
    this.save();
  }

  addItem() { 
    let item = this.cardItemData();  
    this.contentObj.dataArray.push(item);
    this.saveItem();
  }

  radioClick(it, radioValue) {
    it.radioValue = radioValue;
    this.saveItem();
  }

  clickCheckBox() {
    this.saveItem();
  }

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

