import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { Star, Page, Kind, Option, East10 } from './east10';
import { ThrowStmt } from '@angular/compiler';
import { JsonPipe } from '@angular/common';


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

  // 储存数据用
  saveKey = "EAST-10";
  // 储存对象
  item;


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

  }


  ngOnInit() {

    //this.item = new Course();
    this.item = new East10();

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

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

    }, this.saveKey);
    console.log(this.item);
  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {
    if(!this.item){
      this.item = new East10();
    }
  }


   /**
   * 储存图片数据
   * @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();
    console.log('this.item = ' + JSON.stringify(this.item));
  }

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

  randomChar() {
    var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
    var tmp = "";
    var timestamp = new Date().getTime();
    for (var i = 0; i < 6; i++) {
      tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
    }

    return timestamp + tmp;
  }

  addStar(){
    let star = new Star();
    star.id = this.randomChar();
    this.item.stars.push(star);
    this.save();
  }

  delStar(idx){
    if (idx !== -1){
      this.item.stars.splice(idx, 1);
      this.save();
    }
  }

  addPage(){
    let page = new Page();
    page.id = this.randomChar();
    this.item.pages.push(page);
    this.save();
  }

  delPage(idx){
    if (idx !== -1){
      this.item.pages.splice(idx, 1);
      this.save();
    }
  }

  addKind(){
    let kind = new Kind();
    kind.id = this.randomChar();
    this.item.kinds.push(kind);
    this.save();
  }

  delKind(idx){
    if (idx !== -1){
      this.item.kinds.splice(idx, 1);;
      this.save();
    }
  }

  addOption(){
    let option = new Option();
    this.item.options.push(option);
    this.save();
  }

  delOption(idx){
    if (idx !== -1){
      this.item.options.splice(idx, 1);
      this.save();
    }
  }

  setStarID(e, item){
    item.star_id = e;
    this.save();
  }

  setPageID(e, item){
    item.page_id = e;
    this.save();
  }

  setKindID(e, item){
    item.kind_id = e;
    this.save();
  }

  // getIndexInArray(ar, id){
  //   for(let i = 0; i < ar.length; i++){
  //     if (ar[i].id == id){
  //       return i;
  //     }
  //   }
  //   return -1;
  // }

  // getStarPage(kind){
  //   let star_id = -1, page_id = this.getIndexInArray(this.item.pages, kind.page_id);
  //   if (page_id >= 0)
  //     star_id = this.getIndexInArray(this.item.stars, this.item.pages[page_id].star_id);
  // }
}

