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 = "beatmole";

  item = {
    onlineFlg: false,
    title: "疯狂打地鼠",
    questionText: "调皮的地鼠偷走了魔法卡牌，亲爱的小玩家，请仔细听游戏指令，快速找到藏着正确魔法卡牌的地鼠，用锤子敲打它，就能夺回魔法卡牌。在本次的游戏中，你将和其他玩家进行PK，游戏结束，获得魔法卡牌数量最多的玩家，将获得胜利。最后根据获得的魔法卡牌数量，你将获得相应的能量石奖励！开始挑战吧！",
    questionTextAudio: "http://staging-teach.cdn.ireadabc.com/d08192ced3bd81941d38941bb36e5042_l.mp3",
    questions: [],
    bgAudio: "",
    audioBgName: "",
    audioName: ""
  };

  deleteBgAudio() {
    this.item.bgAudio = "";
    this.item.audioBgName = "";
    this.save();
  }
  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: "",
      right: false
    });
    this.save();
  }

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

  addquestion() {
    this.item.questions.push({
      questionImg: "",
      questionAudio: "",
      duration: 120,
      options: []
    });
    this.save();
  }

  copyquestion(i) {
    let data = this.item.questions[i];
    this.item.questions.push(JSON.parse(JSON.stringify(data)));
    this.save();
  }
  ngChange(i, j) {
    this.save();
  }
}