import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef} from '@angular/core';

import { WL04, Mission, MissionOption } from './WL04';
import { JsonPipe } from '@angular/common';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.scss']
})

export class FormComponent implements OnInit, OnChanges, OnDestroy {


  // 储存数据用
  saveKey = "WL04";
  // 储存对象
  item;


  @Output()
  update = new EventEmitter();
  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {

  }

  ngOnInit() {

    //this.item = new Course();
    this.item = new WL04();
    this.item.time = 3;
    this.item.missions = new Array<Mission>();
    // 获取存储的数据
    (<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 WL04();
      this.item.time = 3;
      this.item.missions = new Array<Mission>();
    }
  }



  initData() {
  }



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



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

  wordChange(item) {
    if(!item.wordColors){
      item.wordColors= [];
    }
    item.wordColors.length = 0;
    for(var i=0;i<item.word.length;i++){
      item.wordColors.push({color:"1"});
    }

  }
  clickSwitchColor(){
    // wordColors[wordsIndex] = value;
    // console.log(wordsIndex);
  }
  clickSwitch(missionsIndex,optionsIndex){

    let missionOption = new MissionOption();
    if(!this.item.missions[missionsIndex].missionOptions){
      this.item.missions[missionsIndex].missionOptions = new Array<MissionOption>();
    }
    for(var i=0;i< this.item.missions[missionsIndex].missionOptions.length; i++){
      this.item.missions[missionsIndex].missionOptions[i].isResult = (i === optionsIndex);
    }

    this.save();
  }
  addOption(data){
    let missionOption = new MissionOption();
    if(!data.missionOptions){
      data.missionOptions = new Array<MissionOption>();
    }

    data.missionOptions.push(missionOption);

    this.save();
  }
  delOption(array,index){
    if (index !== -1){
      array.splice(index, 1);;
      this.save();
    }
  }
  addMission(){
    let mission = new Mission();
    if (!this.item.missions)
      this.item.missions = new Array<Mission>();

    this.item.missions.push(mission);
    this.save();
  }

  delMission(index){
    if (index !== -1){
      this.item.missions.splice(index, 1);;
      this.save();
    }
  }
  doInputFocus(iIndex,optIndex){
    for(var i=0;i<this.item.missions.length;i++){
      for(var j=0;j<this.item.missions[i].missionOptions.length;j++){
        if(i===iIndex && j === optIndex){
          this.item.missions[i].missionOptions[j].isShow = true;
        }else{
          this.item.missions[i].missionOptions[j].isShow = false;
        }
      }
    }

  }

}

