import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';


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

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

  isHideVideo = false;


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

  }

  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() {

  }


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

  videoSeeked(e) {

  }

  videoError(e) {

  }

  videoLoaded(e, it) {

  }

  hideVideo() {
    this.isHideVideo = !this.isHideVideo;
  }


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

  }

  onVideoUploaded(res, key) {
    console.log('onVideoUploaded: ', res);

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

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

    this.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));
  }

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

}