import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ElementRef, ViewChild} from '@angular/core';
import {NzMessageService} from "ng-zorro-antd";
@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.scss']
})

export class FormComponent implements OnInit, OnChanges, OnDestroy {

  dataArray = []; 
  _item: any; 
  jsonData: String = "";
  KEY = 'DataKey_MultipleChoice';

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

  @ViewChild('copyTextArea', {static: true}) copyTextArea: ElementRef;

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

  }

  ngOnInit() {
    this.item = {};
    this.item.contentObj = {};
    const getData = (<any> window).courseware.getData;
    getData((data) => {
      if (data) {
        this.item = data;
      } else {
        this.item = {};
      }
      if ( !this.item.contentObj ) {
        this.item.contentObj = {};
      }
      this.init();
      this.refresh();
      this.saveItem();
    }, 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 {
      question:{
        text:"",
        image_url:"",
        audio_url:"",
        questionShowType: 1,
        questionType: 1, // 1-选择题 2-判断对错
      },
      choice:{
        correct:{ showType: 1, text: "", image_url: "", audio_url: "" },
        incorrect:[
          { showType: 1, text: "", image_url: "", audio_url: "" },
          { showType: 1, text: "", image_url: "", audio_url: "" }
        ],
        choiceQuestionRight: false,
      }
    };
  }

  cardChoiceData(){
    return { showType: 1, text: "", image_url: "", audio_url: "" }
  }

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


  initData() {
  }

  deleteItem(index) { 
    if (index !== -1) {
      this.dataArray.splice(index, 1);
    }
    this.save();
  }
 
  deleteChoice(questionIndex, choiceIndex){
    if (questionIndex !== -1 && choiceIndex !== -1) {
      this.dataArray[questionIndex].choice.incorrect.splice(choiceIndex, 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, item, key="audio_url") { 
    item[key] = 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();
    this.jsonData = JSON.stringify(this.item)
  }

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

  copyJsonData() {
    this.copyTextArea.nativeElement.select();
    document.execCommand('copy');
    this.nzMessageService.success('已复制');
  }

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

}

