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

const defaultData = [
  {audio_url:"", displayType:"image", text:"This is demoA.", image_url:""},
  {audio_url:"", displayType:"text", text:"This is demoB.", image_url:""},
  {audio_url:"", displayType:"image", text:"This is demoC.", image_url:""},
  {audio_url:"", displayType:"text", text:"This is demoD.", image_url:""}
]

const defauleFormData = {
  sex:"male",
  audio: [
    {url: ""},
    {url: ""},
    {url: ""},
    {url: ""},
    {url: ""},
    {url: ""},
  ]
}

@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;

  KEY = 'DataKey_East_L215';
  roleMapping = [
    "建筑工",
    "消防员",
    "警察",
    "教师",
    "农民伯伯",
    "医生"
  ]
  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 = {};
      }
      console.log(this.item)
      if ( !this.item.contentObj ) {
        this.item.contentObj = defauleFormData;
      }
      if(!this.item.contentObj.sex){
        this.item.contentObj = defauleFormData;
      }
      if(!this.item.contentObj.audio || this.item.contentObj.audio.length === 0){
        this.item.contentObj = defauleFormData;
      }
      this.init();
      this.refresh();
      this.save()
    }, this.KEY);
  }
  ngOnChanges() {

  }

  ngOnDestroy() {
  }

  init() {
    // if (this.item.contentObj.dataArray) {
    //   this.dataArray = this.item.contentObj.dataArray;
    // } else {
    //   this.dataArray = this.getDefaultPicArr();
    //   this.item.contentObj.dataArray = this.dataArray;
    // }
  }

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

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

  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.dataArray[questionIndex].choice.incorrect.push(item);
    // this.saveItem();
  }

  onImageUploadSuccessByItem(e, item) {
    item.image_url = e.url
    this.save(); 
  }
 
  onAudioUploadSuccessByItem(e, it) {
    it.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();
  }

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

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

