import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { getDefaultAnswer, getDefaultExercises } from '../bean/Exercises';
import { getDefaultTile } from '../bean/TitleBean';



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

  // 储存数据用
  saveKey = "YM5-16";
  // 储存对象
  item;

  length = 0;

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

  }


  ngOnInit() {

    this.item = {};

    this.item.title = getDefaultTile();

    this.item.exercisesArr = [];

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

      if (data) {
        this.item = data;
      }
      //console.log(JSON.stringify(this.item));
      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {
    if (!this.item.title) {
      this.item.title = getDefaultTile();
    }

    if (!this.item.exercisesArr) {
      this.item.exercisesArr = [];
    }

    let len = this.item.exercisesArr.length;

    for (let i = len; i < 1; ++i) {
      this.item.exercisesArr.push(getDefaultExercises());
    }

    this.length = this.item.exercisesArr.length;

    for(let i = 0; i < this.length; ++ i){
      if(!this.item.exercisesArr[i]){
        this.item.exercisesArr[i] = getDefaultExercises();
      }
    }
  }


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

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

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

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



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

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

  saveContent(item) {

    if (!this.check(item.val, 9)) {
      return;
    }

    let answerArr = [];

    if (item.val) {

      let chr = item.val.split(/\s+|\?|\.|\,|\!/);

      for (let c of chr) {
        if (c) {

          let answer = getDefaultAnswer();
          answer.val = c;
          answerArr.push(answer);
        }
      }
    }

    item.answerArr = [...answerArr];

    this.length = item.answerArr.length;

    this.save();

  }

  check(value, maxLength: number) {
    if (!value) {
      return true;
    }

    const chr = value.split(/\s+|\?|\.|\,|\!/);

    let count = 0;

    for (const c of chr) {
      if (c) {
        count += 1;
      }
    }

    if (count <= maxLength) {
      return true;
    }
    else {
      let id = this.message.error('超过' + maxLength + '个单词', { nzDuration: 0 }).messageId;
      setTimeout(() => {
        this.message.remove(id);
      }, 2000);
      return false;
    }
  }


  saveIndex(exersices, item) {
    
    let canSave = true;

    if (!item.index) {
      canSave = false;
    }
    else {
      for (let i = 0; i < exersices.answerArr.length; ++i) {
        let answer = exersices.answerArr[i];

        if (item == answer) {
          continue;
        }

        else if (answer.index == item.index) {

          canSave = false;
          break;
        }
      }
    }

    if (canSave) {
      this.save();
    }
    else {
      let id = this.message.error('请录入不重复的答题顺序', { nzDuration: 0 }).messageId;
      setTimeout(() => {
        this.message.remove(id);
      }, 2500);
    }
  }
}

