import { Component, OnDestroy, OnChanges, OnInit, ApplicationRef, ChangeDetectorRef, ViewChild, ElementRef } from '@angular/core';
import { ComponentBase } from './ComponentBase';
import { MetaFormCreator } from './mataFormCreator';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent extends ComponentBase implements OnInit, OnChanges, OnDestroy {
  // 储存数据用
  saveKey = "hlxpt";

  item = {
    title: "火力小炮台",
    questionText: "敌人建造了很多炮塔在我们的地盘上，真是太危险了！ 亲爱的小玩家，请认真听游戏指令，利用小炮台摧毁这些炮塔吧！游戏结束后，根据摧毁的炮塔数量，你将获得相应的能量石奖励哟！开始挑战吧！",
    questionTextAudio: "http://staging-teach.cdn.ireadabc.com/8b8983df3ead9128f029c7a1a1e5c524_l.mp3",
    questions: [],
    audioName:"",
    bgAudio: "",
    bgAudioName: "",
  };

  isVisible = false;
  deleteTitle = "是否删除题目";
  deleteCallback = function () { };
  openDelete(title, callback) {
    this.deleteCallback = callback;
    this.deleteTitle = title;
    this.isVisible = true;
  }
  handleCancel() {
    this.isVisible = false;
  }
  handleOk() {
    this.deleteCallback && this.deleteCallback();
    this.handleCancel();
  }
  init(): void {
    console.log(new MetaFormCreator().create());
  }

  removeoption(i, j) {
    this.openDelete("确定删除选项？", () => {
      this.item.questions[i].options.splice(j, 1);
      this.save();
    })
  }
  copyoption(i, j) {
    let data = this.item.questions[i].options[j];
    this.item.questions[i].options.push(JSON.parse(JSON.stringify(data)));
    this.save();
  }

  addoption(i) {
    this.item.questions[i].options.push({
      type: "",
      image: "",
      audio: "",
      text: "",
      time: "",
      right: false
    });
    this.save();
  }

  removequestion(idx) {
    this.openDelete("确定删除题目？", () => {
      this.item.questions.splice(idx, 1);
      this.save();
    })
  }

  deleteBgAudio() {
    this.item.bgAudio = "";
    this.item.bgAudioName = "";
    this.save();
  }
  addquestion() {
    this.item.questions.push({
      options: [],
      type: ""
    });
    this.save();
  }
  ngChange(i,j){
    this.save();
  }
}