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

  hasError = false;
  cardsArray: Array<{
    pic_id: string,
    word: string
  }>;

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

  }


  ngOnInit() {

    this.item = {
      contentObj: {}
    };
    if (this.item.contentObj.cards) {
      this.cardsArray = this.item.contentObj.cards;
    } else {
      this.cardsArray = [];
    }
    // 获取存储的数据
    (window as any).courseware.getData((data) => {

      if (data) {
        this.item = data;
        if (this.item.contentObj.cards) {
          this.cardsArray = this.item.contentObj.cards;
        } else {
          this.cardsArray = [];
        }
      }

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

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {

  }


  // /**
  //  * 储存图片数据
  //  * @param e
  //  */
  // onImageUploadSuccess(e, key) {
  //
  //     this.item[key] = e.url;
  //     this.save();
  // }
  // /**
  //  * 储存音频数据
  //  * @param e
  //  */
  // onAudioUploadSuccess(e, key) {
  //
  //   this.item[key] = e.url;
  //   this.save();
  // }



  /**
   * 储存数据
   */
  save() {
    (window as any).courseware.setData(this.item, null, this.saveKey);
    this.refresh();
  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }
  checkValidSts() {
    for (const item of this.cardsArray) {
      if (!item.pic_id || !(item.word && item.word.trim())) {
        return true;
      }
    }
    return false;
  }
  addCard(evt) {
    this.hasError = this.checkValidSts();
    if (this.hasError) {
      return;
    }
    if (this.cardsArray.length >= 8 ) {
      this.nzMessageService.info('you can only add eight item!');
      return;
    }
    this.cardsArray.push({pic_id: '', word: ''});
  }
  removeCard(control, evt , i) {
    this.cardsArray.splice(i, 1);
    this.item.contentObj.cards = this.cardsArray;
    this.save();
  }
  addCardWord(control, evt, i) {
    const val = evt.target.value.trim();
    control.word = val;
    if (control.word && control.pic_id) {
      this.item.contentObj.cards = this.cardsArray;
      this.save();
      this.hasError = false;
    } else {
      this.hasError = true;
    }
  }
  onImageUploadSuccess(control, info) {
    control.pic_id = info.url;
    if (control.word && control.pic_id) {
      this.item.contentObj.cards = this.cardsArray;
      this.save();
      this.hasError = true;
    } else {
      this.hasError = false;
    }
  }
}

