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


  customTypeGroupArr = [


    // {
    //   name: '基础资源',
    //   rect: true,
    //   anima: true,
    //   pic: true,
    //   text: true,
    //   audio: true,
    // },
    {
      name: '动画',
      rect: true,
      anima: true,
      audio: true,
    },
    {
      name: '图片',
      pic: true,
      audio: true,
    },


    // {

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

  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.groupArr) {
      this.item.groupArr = [];
    }
  }


  resetGrid(grid) {
    for (let i=0; i<5; i++) {
      grid[i] = [{}, {}, {}, {}, {}];
    }
  }
  addOptionBtnClick(group) {
    if (!group.optionArr) {
      group.optionArr = [];
    }
    group.optionArr.push({})

  }

  subOptionBtnClick(group) {
    if (!group.optionArr) {
      return;
    }
    group.optionArr.pop();
  }

  addBtnClick() {

    const grid = [];
    for (let i=0; i<5; i++) {
      grid[i] = [{}, {}, {}, {}, {}];
    }
    
    this.item.groupArr.push({
      grid
    });
    this.save();
  }

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

  

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

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

}