import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef} from '@angular/core';
import { defaultData } from '../../assets/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;
  dataArray: Array<Object> = []; 
  hotZoneItemArr: Array<Object> = [];
  bgItem: Object;
  bgImages: Object = {leftTop:"", middleTop:"", rightTop:"", leftMiddle:"", rightMiddle:"", leftBottom:"", middleBottom:"", rightBottom:""};
  KEY = 'DataKey_YM4-11';

  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.KEY);
  }
  ngOnChanges() {

  }

  ngOnDestroy() {
  }

  saveData(e){
    this.bgItem = e.bgItem;
    // this.bgImages = e.bgImages
    this.hotZoneItemArr.length = 0;
    e.hotZoneItemArr.forEach(item => {
      this.hotZoneItemArr.push(item)
    });
    this.save();
  }

  init() {
    if(typeof(this.item.contentObj.hotZoneItemArr) == undefined || typeof(this.item.contentObj.bgItem) == undefined || typeof(this.item.contentObj.bgImages) == undefined || this.item.contentObj.version != defaultData.version) {
      console.log("Init defalue data")
      this.item.contentObj = defaultData
      this.hotZoneItemArr = defaultData.hotZoneItemArr
      this.bgItem = defaultData.bgItem
      this.bgImages = defaultData.bgImages
    } else {
      console.log("Data loaded")
      this.hotZoneItemArr = this.item.contentObj.hotZoneItemArr;
      this.bgItem = this.item.contentObj.bgItem;
      this.bgImages = this.item.contentObj.bgImages;
    }
  
  }

  cardItemData(){
    return {
      audio_url: "",
      image_url: "",
      hotZoneIndex: -1
    };
  }

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

  getDefaultPicArr() {
    let arr = []; 
    return arr;
  }

  initData() {

  }


  addChoice(questionIndex) { 
    // let item = this.cardChoiceData();  
    // this.dataArray[questionIndex].choice.incorrect.push(item);
    // this.saveItem();
  }

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

  onTitleAudioUploadSuccess(e) { 
    this.item.contentObj.titleAudio_url = e.url;
    this.save();
  }

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

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

  clickCheckBox() {
    this.saveItem();
  }

  saveItem() {
    this.save();
  }

  backgroundImageChanged(e) {
    this.saveItem()
  }

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

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