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 = "op_video_ques_new";
  // 储存对象
  item;

  customTypeGroupArr = [
  {

      name: '选项区域',
      rect: true,
      isCopy: true,
      select: [
        {label: '正确答案', value: 'right'},
        {label: '错误答案', value: 'wrong'},
      ],
      // isShowPos: true,
      // label: '答案',
    },

    {

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

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

  stepArr = [
    {label: '问题阶段', value: 'ques'},
    {label: '待机阶段', value: 'wait'},
    {label: '正确阶段', value: 'right'},
    {label: '错误阶段', value: 'wrong'},
    {label: '结束阶段', value: 'end'},
  ];


  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 = {"lrcData":{"video_url":"http://staging-teach.cdn.ireadabc.com/a73a97faf1638403b36784a2141b92c1.mp4","lyrics":[{"time":0.7627950000000012,"data":"ant apple alligator axe.","newLine":false,"textArr":[{"data":"aaa"},{"isIcon":true,"url":"http://staging-teach.cdn.ireadabc.com/c8abf7383d65bcbdba8f390785cece60.png"},{"data":" bbbb"}]},{"time":4.957703,"data":"ant apple alligator axe.","newLine":false,"textArr":[{"data":"aa"}]},{"time":9.020296000000002,"data":"ant apple alligator axe.","newLine":false,"textArr":[{"isIcon":true,"url":"http://staging-teach.cdn.ireadabc.com/f6f4bbef497409fef3c8f1d375893829.png"}]},{"time":13.198415,"data":"ant apple alligator axe.","newLine":false,"textArr":[{"isIcon":true,"url":"http://staging-teach.cdn.ireadabc.com/7b18a7931d64f7b9e8b7ecd25cb5e3d4.png"},{"data":"bbbbb  "},{"isIcon":true,"url":"http://staging-teach.cdn.ireadabc.com/e12d13282a10f92d65ac41b4aecaad4c.png"}]},{"time":17.376999,"data":"","newLine":false,"textArr":[]}]},"audio_url":"http://staging-teach.cdn.ireadabc.com/6db45327fad91c086de94e9d96cb8c7d.mp3","mute_video_url":"http://staging-teach.cdn.ireadabc.com/1200e283244f6f62f311e96ed1dabf04.mp4","begin_audio_url":"http://staging-teach.cdn.ireadabc.com/83513f8d6083ebe072e3f9e47a21558a.mp3","record_audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3","fight_audio_url":"http://staging-teach.cdn.ireadabc.com/440cf9d57752b30691d90ed565e8c924.mp3","compose_audio_url":"http://staging-teach.cdn.ireadabc.com/a5fa20e0c6024421b0d9a7007c303503.mp3","finish_audio_url":"http://staging-teach.cdn.ireadabc.com/a41f0f2bf190396e2641ec8dada8edfe.mp3","song_video_url":"http://staging-teach.cdn.ireadabc.com/78734cdbe1cf0b8d665a976e7bbf5f65.mp4"}

      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 = [];
    }
  }


  addBtnClick() {
    this.item.quesArr.push({
      itemType: 'sound'
    });
    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();
  }

 

  /**
   * 储存数据
   */
  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);
  }

}