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

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

  picArr;

  isShowPicPanel = false;

  customTypeGroupArr = [
    {
      name: '基础资源',
      rect: true,
      anima: true,
      audio: true,
      pic: true,
      text: true,
    },
    {
      name: '图片',
      pic: true
    }
  ]

  groupArr;
  curGroup;
  letters = [
    'a','b', 'c','d','e','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
  ]
  wordData = {
    'a': [
      'abc',
      'aab'
    ],

    'b': [
      'bbb',
      'bcc',
      'bcd'
    ]
  }

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

  }


  ngOnInit() {

    this.item = {};

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

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

      this.init();
      // this.changeDetectorRef.reattach();
      // 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.groupArr = this.item.groupArr || [];

    this.refreshAnimaArr();
  }


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

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

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

    it[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() {
    if (!this.curGroup) {
      return;
    }

    this.curGroup.picArr.push({
      letter: '',
      word: '',
      pic_url: '',
      audio_url: '',
      text: ''
    });
    // this.item.picArr = this.picArr;
    this.save();
  }

  addGroup() {
    console.log('in addGroup');
    this.groupArr.push(this.createGroupData());
    this.item.groupArr = this.groupArr;
    this.save();
  }

  deleteGroup(i) {
    console.log('in deleteGroup');
    this.groupArr.splice(i, 1);
    this.item.groupArr = this.groupArr;
    this.save();
  }

  createGroupData() {
    return {
      title: '',
      letter: '',
      audio_url: '',
      bg: '1',
      picArr: [],
    };
  }

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

  editorPicArr(i) {
    this.curGroup = this.groupArr[i];
    this.isShowPicPanel = true;
    console.log('editorPicArr');
    this.refresh();
  }

  handleOk() {
    this.isShowPicPanel = false;

  }

  handleCancel() {
    this.isShowPicPanel = false;
  }

  lettersChange(e) {
    console.log('lettersChange: ', e);

  }


  /**
   * 储存数据
   */
  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.changeDetectorRef.reattach();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();

      this.appRef.tick();
    }, 1);
  }

}

