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

let replaceAll = function (str, s1, s2) {
  return str.replace(new RegExp(s1, "gm"), s2);
}
@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent extends ComponentBase implements OnInit, OnChanges, OnDestroy {
  // 储存数据用
  saveKey = "dg28_drag";
  item = {
    npcAudioText: "",
    npcAudio: "",
    npcAudioName: "",
    npcAudioEnd: "",
    npcAudioEndName: "",
    
    // 开始音频
    auidoStart: "",
    auidoStartName: "",
    // 结束音频
    auidoEnd: "",
    auidoEndName: "",
    // 肉饼引导音频
    auidoMeet: "",
    auidoMeetName: "",
    // 蔬菜引导音频
    auidoVegetable: "",
    auidoVegetableName: "",
    // 第一次完成音频
    auidoFinish: "",
    auidoFinishName: "",

    questions: [],
    questionText: "",

    contentMain: "",
    contentArr: [],
  };

  constructor(public nzMessageService: NzMessageService, public appRef: ApplicationRef, public changeDetectorRef: ChangeDetectorRef) {
    super(appRef, changeDetectorRef);
  }
  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());

    if (this.item.questions.length == 0) {
      this.addQuestion();
    }
  }

  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: "img",
      image: "",
      audio: "",
      text: "",
      time: "",
      right: false
    });
    this.save();
  }



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

  addQuestion() {
    // 题目自带一堆东西不用添加选项部分
    this.item.questions.push({
      title: "",//大标题
      mainType: "",//题干类型
      mainText: "",//题干文字
      mainTextArr: [],//题干文字高亮
      mainImage: "",//题干图片

      meetType: "",//肉类型
      meetText: "",//肉文字
      meetTextArr: [],//肉文字高亮
      meetImage: "",//肉图片

      vegetableType: "",//菜类型
      vegetableText: "",//菜文字
      vegetableTextArr: [],//菜文字高亮
      vegetableImage: "",//菜图片


      pieType: "",//菜类型
      pieText: "",//菜文字
      pieTextArr: [],//菜文字高亮
      pieImage: "",//菜图片
    });
    this.save();
  }
  ngChange(i, j) {
    this.save();
  }

  saveHotZone(group, e) {
    console.log('e: ', e);
    const { bgItem, hotZoneItemArr } = e;

    group.bgItem = bgItem;
    group.hotZoneItemArr = hotZoneItemArr;

    this.nzMessageService.success('保存成功');

    this.save();
  }

  changeMain(i, name) {
    let question = this.item.questions[i]
    //输出的数据可以识别到\n的换行符
    let contentMain = question[name + "Text"];
    let contentArr = question[name + "TextArr"] || [];
    let arr = contentMain.split(" ");
    let oldArr = contentArr.concat();
    let contetArr = arr.map(ar => {
      let obj = {
        text: replaceAll(`${ar}`, "\n", "<br/>"),
        block: false,
        check: 1,
      }
      for (let i = 0; i < oldArr.length; i++) {
        if (obj.text == oldArr[i].text) {
          obj.block = oldArr[i].block;
          obj.check = oldArr[i].check;
          oldArr.splice(i, 1);
          break;
        }
      }
      return obj;
    })
    question[name + "TextArr"] = contetArr;
    console.log(contetArr)
    this.save();
  }

  onBlock(item, vis) {
    item.block = vis;
    this.save();
  }


  customRadioChange(e, item, key) {
    item[key] = e;
    this.save();
  }

  onDragonBoneSave(e, item) {
    console.log(e);
    this.save();
  }

}