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



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

  // 储存数据用
  saveKey = 'cocos_et_22';
  // 储存对象
  item;

  bgItem = {};
  hotZoneItemArr = [];
  animaArr = [];

  picArr;

  customTypeGroupArr = [
    {
      name: '基础资源',
      rect: true,
      anima: true,
      audio: true,
      pic: true,
      text: true,
    },
    {
      name: '图片',
      audio: true,
      pic: true
    },
    {
      name: '歌词区域',
      rect: true,
    },
    {
      name: '播放按钮',
      rect: true,
    },
    {
      name: '模式切换按钮',
      rect: true,
    },
    {
      name: '动画',
      rect: true,
      anima: true,
      audio: true,
    }
  ]


  bgColorArr = [
    {color: '#f9bd03', text: '橙色'},
    {color: '#0000ff', text: '蓝色'},
    {color: '#00ff00', text: '绿色'},
  ];


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

  }


  ngOnInit() {

    this.item = {};

    // 获取存储的数据
    (<any> window).courseware.getData((data) => {
      // console.log(1, data);
      if (data) {
        this.item = data;
        // console.log(2, data);
      }

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

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {
    this.bgItem = this.item.bgItem || {};
    this.hotZoneItemArr = this.item.hotZoneItemArr || [];

    this.animaArr = this.item.animaArr || [];
    this.picArr = this.item.picArr || [];

    this.refreshAnimaArr();
  }


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

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

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, key) {

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

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

    this.item.bgItem = bgItem;
    this.item.hotZoneItemArr = hotZoneItemArr;

    this.refreshAnimaArr();

    this.save();

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

  refreshAnimaArr() {
    const tmpArr = [];
    this.hotZoneItemArr.forEach((item) => {
      let data = this.animaArr.find((anima) => {
        return anima.id === item.id;
      });

      if (!data) {
        data = this.createAnimaData(item);
      }
      tmpArr.push(data);
    });

    this.animaArr = tmpArr;
  }


  createAnimaData(hotZoneItem) {

    const animaData = {};
    animaData['id'] = hotZoneItem.id;
    return animaData;
  }


  addPic() {
    this.picArr.push({
      pic_url: '',
      audio_url: '',
      text: ''
    });

    this.item.picArr = this.picArr;
    this.save();
  }

  deletePic(i) {
    this.picArr.splice(i, 1);
    this.item.picArr = this.picArr;
    this.save();
  }



  saveBgColor(i) {

    this.item.bgColorId = i;
    this.item.bgColor = this.bgColorArr[i].color;
    this.save();
  }

  /**
   * 储存数据
   */
  save() {

    console.log('this.item: ', this.item);
    console.log('this.item json: ', JSON.stringify(this.item) );
    (<any> window).courseware.setData(this.item, null, this.saveKey);
    this.refresh();
  }

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

  }
}

