import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd/message';


@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit, OnChanges, OnDestroy {

  // 储存数据用
  saveKey = "MRBR_video";
  // 储存对象
  item;

  curSelectItem;
  isShowKK=false;
  curSelectKK;
  KKArray = [
    "i",
    "ɪ",
    "u",
    "ʊ",
    "ɑ",
    "ʌ",
    "ɝ",
    "ə",
    "ɚ",
    "ɔ",
    "æ",
    "ɛ",
    "e",
    "aɪ",
    "ɔɪ",
    "o",


    "aʊ",
    "p",
    "b",
    "t",
    "d",
    "k",
    "g",
    "tʃ",
    "dʒ",
    "f",
    "v",
    "θ",
    "ð",
    "s",
    "z",
    "ʃ",
    "ʒ",
    "h",
    "m",
    "n",
    "ŋ",
    "l",
    "r",
    "w",
    "j",
  ]

  customTypeGroupArr = [
    {
      name: '小手区域',
      rect: true,
    },
    // {
    //   name: '动画',
    //   rect: true,
    //   anima: true,
    // },
    // {
    //   name: '图片',
    //   pic: true,
    // },

    // {

    //   name: '选项区域',
    //   rect: true,
    //   isCopy: true,
    //   select: [
    //     {label: '正确答案', value: 'right'},
    //     {label: '错误答案', value: 'wrong'},
    //   ],
    //   labelArr: [
    //     {title:"开始时间"},
    //     {title:"结束时间"},
    //   ]
    // },
    
    // {

    //   name: '选项图（默认，选中）',
    //   // rect: true,
    //   isCopy: true,
    //   pic: true,
    //   pic2: true,
    //   audio: true,
    //   select: [
    //     {label: '正确答案', value: 'right'},
    //     {label: '错误答案', value: 'wrong'},
    //   ],
    //   // isShowPos: true,
    //   // label: '答案',
    // },

    // {
    //   name: '提交按钮',
    //   rect: true,
    // },

    // {

    //   name: '音频区域',
    //   rect: true,
    //   isCopy: true,
    //   audio: true,
    // },
    // {
    //   name: '拖拽物',
    //   drag: true,
    //   pic: true,
    //   isCopy: true,
    // },
    // {
    //   name: '拖拽结束区',
    //   rect: true,
    //   isCopy: true,
    // },
  ];
 

  stepArr = [
    {label: '问题阶段', value: 'ques'},
  ];

  videoSizeArr = [
    {key: '1920_1080', value: '1920 x 1080'},
    {key: '1280_720', value: '1280 x 720'},
    {key: '940_600', value: '940 x 600'},
  ]


  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef, 
    private message: NzMessageService) {

  }

  createShell() {
    this.item.wordList.push({
      word: '',
      audio: '',
      backWord: '',
      backWordAudio: '',
    });
    this.save();
  }

  removeShell(idx) {
    this.item.wordList.splice(idx, 1);
    this.save();
  }

  ngOnInit() {

    this.item = {};

    // 获取存储的数据
    (<any>window).courseware.getData((data) => {

      if (data) {
        this.item = data;
      }

      
      // this.item = {"quesArr":[{"itemType":"sound","ques_start":"0","ques_end":"42.14","wait_start":"43","wait_end":"44.5","right_start":"45","right_end":"50","wrong_start":"51","wrong_end":"55","text":"b"},{"itemType":"sound","ques_start":"54.02","ques_end":"55.17","wait_start":"55.18","wait_end":"57.18","right_start":"57.20","right_end":"61","wrong_start":"61.02","wrong_end":"63.16","end_start":"64.15","text":"c","end_end":"69.10"},{"itemType":"sound","ques_start":"69.10","ques_end":"138","wait_start":"138","wait_end":"139.08","right_start":"139.08","right_end":"141.21","wrong_start":"142.04","wrong_end":"144.17","text":"ə"},{"itemType":"sound","ques_start":"144.17","ques_end":"157.18","wait_start":"157.18","wait_end":"159.15","right_start":"159.16","right_end":"162.08","wrong_start":"162.11","wrong_end":"164.13","text":"b"},{"itemType":"sound","ques_start":"164.13","ques_end":"174.12","wait_start":"174.12","wait_end":"176.07","right_start":"176.10","right_end":"178.19","wrong_start":"178.23","wrong_end":"180.16","text":"k"},{"itemType":"submit","ques_start":"180.16","ques_end":"222.35","wait_start":"222.35","wait_end":"223.25","right_start":"225.04","right_end":"235.1","wrong_start":"238.24","wrong_end":"250","bgItem":{"url":"http://teach.cdn.ireadabc.com/05e470d38e56290ca03f7907d4cb20dc.png","rect":{"x":71.94,"y":0,"width":1079.11,"height":607}},"hotZoneItemArr":[{"id":"1658728795418","index":0,"pic_url":"http://teach.cdn.ireadabc.com/8bae7375867d869b6bbcff6ae66a6484.png","audio_url":"http://teach.cdn.ireadabc.com/e0bb4643b06aae7088ffa4ec2bf8d8c6.mp3","itemType":"pic","fontScale":0.95546875,"imgScale":0.6423406070864864,"imgSizeW":209,"imgSizeH":202,"mapScale":0.95546875,"dragDot":{"x":611.5,"y":252.32494230943945},"gIdx":"1","selectType":"right","pic2_url":"http://teach.cdn.ireadabc.com/ba1d7f0c719c6dcf0cf0cd8b99f5f2c1.png","rect":{"x":367.44,"y":431.86,"width":134.25,"height":129.75}},{"id":"1658728866876","index":1,"pic_url":"http://teach.cdn.ireadabc.com/8bae7375867d869b6bbcff6ae66a6484.png","audio_url":"http://teach.cdn.ireadabc.com/3be15cf32433fa563bd360a983949086.mp3","itemType":"pic","fontScale":0.95546875,"imgScale":0.6423406070864864,"imgSizeW":209,"imgSizeH":202,"mapScale":0.95546875,"dragDot":{"x":611.5,"y":321.14805487417095},"gIdx":"1","selectType":"wrong","pic2_url":"http://teach.cdn.ireadabc.com/ba1d7f0c719c6dcf0cf0cd8b99f5f2c1.png","rect":{"x":527.15,"y":431.85,"width":134.25,"height":129.75}},{"id":"1658728918648","index":2,"itemType":"rect","fontScale":0.95546875,"imgScale":1,"imgSizeW":0,"imgSizeH":0,"mapScale":0.95546875,"dragDot":{"x":611.5,"y":321.14805487417095},"gIdx":"2","posX":831.5,"posY":660.5,"rect":{"x":684.71,"y":458.35,"width":97.94,"height":97.94}}]}],"video_url":"http://teach.cdn.ireadabc.com/479e01b24d06478227c77b7d7e5d1bc8.mp4","jumpTime":"0"}

      console.log("this.item: ", JSON.stringify(this.item));

      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {
    if (!this.item.quesArr) {
      this.item.quesArr = [];
    }
  }

  videoTypeChange(e) {
    console.log('e: ', e);
    this.item.videoType = e;
    this.save();
  }

  addBtnClick() {
    this.item.quesArr.push({
      itemType: 'jump'
    });
    this.save();
  }

  deleteBtnClick(i) {
    this.item.quesArr.splice(i, 1);
    this.save();
  }

  radioChange(e, item) {
    console.log('e: ', e);
    item.itemType = e;
    this.save();
  }

  onVideoUploadSuccess(e) {
    console.log('e: ', e);
    this.item.video_url = e.url;
    this.save();
  }

  videoSeeked(e) {

  }

  videoError(e) {

  }

  videoLoaded(e, it) {

  }


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

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

    this.save();
  }




  getLRC(evt) {
    this.item.lrcData = evt;
    this.save();

  }

  onVideoUploaded(res, key, item) {
    if (!item) {
      item = this.item;
    }
    console.log('onVideoUploaded: ', res);

    item[key] = res.url;
    this.save();
  }
  

  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, key, item) {

    if (!item) {
      item = this.item;
    }
    item[key] = e.url;
    this.save();
  }

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, key, it) {
    it[key] = e.url;
    this.save();
  }

  KKBtnClick(it) {
    this.curSelectItem = it;
    this.isShowKK = true
  }

  KKHandleCancel() {
    this.isShowKK = false;
    this.curSelectKK = null;
    this.curSelectItem = null;
  }

  KKHandleOk() {

    // if (!this.curSelectItem.KKText) {
    //   this.curSelectItem.KKText = '';
    // }
    this.curSelectItem.KKText = this.curSelectKK;
   
    this.KKHandleCancel();

    this.save();
  }
 

  /**
   * 储存数据
   */
  save() {
    (<any>window).courseware.setData(this.item, null, this.saveKey);

    this.refresh();
    console.log('this.item = ' + JSON.stringify(this.item));

    this.message.create('success', "保存成功");

  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }

}