import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef, ViewChild, ElementRef} 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 = "ym-23";
  // 储存对象
  item;

  wordArr = [];

num
  constructor(private appRef: ApplicationRef,private changeDetectorRef: ChangeDetectorRef) {

  }


  ngOnInit() {

    this.item = {};

      this.item.title = {
        t_val1:"",
        t_val2:"",
        audio_url:"",
        allmusic_audio:""
       };
       this.item.wordArr=[];




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

      if (data) {
        console.log('~data:', data);
        this.item = data;
      }

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

      }


      console.log('~data:', data);
      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();
      console.log('~data:', data);

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {

    //标题
    if(!this.item.title){
      this.item.title = {
        t_val1:"",
        t_val2:"",
        audio_url:"",
        allmusic_audio:"",

      };
    }

    if (this.item.wordArr) {
      this.wordArr = this.item.wordArr;
    } else {
      this.wordArr = this.getDefaultWordArr();
      this.item.wordArr = this.wordArr;
    }
  }


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

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

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

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

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

  deleteItem(index) {
    if (index !== -1) {
      this.wordArr.splice(index, 1);
    }

    // this.update.emit(this.item);
    this.save();
  }


  addItem() {
    if(this.wordArr.length<4){
      let item = this.wordItemData();
    this.wordArr.push(item);

    }
    this.saveItem();
  /*暂时注销
    let item = this.wordItemData();
    this.wordArr.push(item);

    this.saveItem();*/
  }

  // addLetterItem(letters){

  //   let letter = this.letterData();

  //   letters.push(letter);

  //   this.save();
  // }

  deleteLetterItem(letters, index){
    console.log(letters);
    if (index !== -1) {
      letters.splice(index, 1);
    }
    this.save();
  }


  wordItemData() {
    return {
      word_val: '',
      letters: [],
      word_pic_url:'',
      num:'Y',
      audio_url: '',
    };
  }

  // letterData() {
  //   return {
  //     letter_val: '',
  //     letter_color: 'C04',
  //     is_: false
  //   };
  // }

  saveWordItem(index) {
    let val = this.wordArr[index].word_val.split(" ");
    this.wordArr[index].letters = [];
    for(let i=0;i<val.length;i++){
      if(val[i]){
        this.wordArr[index].letters.push({
          letter_val: val[i],
          letter_color: 'C04',
          letter_font: 'BRLNSDB',
          is_: '0'
        });
      }
    }
    this.save();
  }

  saveItem() {
    this.save();
  }

  getDefaultWordArr() {
    let arr = [];
    return arr;
  }

}

