import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { NzMessageService, UploadXHRArgs, UploadFile } from 'ng-zorro-antd';
// import { defauleFormData } from '../../assets/default/formData/defaultData_LST01'
const defauleFormData = require('../../assets/default/formData/LST01/default.json')

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


export class FormComponent implements OnInit, OnChanges, OnDestroy {

  _item: any;
  KEY = 'DataKey_Cocos_01';
  uploadUrl
  uploadData
  fileList
  copyIndex
  pasteString

  ttttt = 'apple'

  set item(item) {
    this._item = item;
  }
  get item() {
    return this._item;
  }
  contentObj = {
    "version": "1.1",
    bgm_url: "",
    letter1: "",
    letter2: "",
    dataArray: []
  }
  /**
   * {
   *  text:'apple',
   *  shortAudio_url:'',
   *  image_url:'',
   *  isRight:false,
   *  letterIndex:-1
   * }
   */

  @Output()
  update = new EventEmitter();
  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {
    this.uploadUrl = (<any>window).courseware.uploadUrl();
    this.uploadData = (<any>window).courseware.uploadData();
    window['air'].getUploadCallback = (url, data) => {
      this.uploadUrl = url;
      this.uploadData = data;
    };
  }

  handleTextChange(text, index) {
    let item = this.contentObj.dataArray[index];
    item.text = text;
    this.saveItem()
    this.refresh();
  }

  handleTextChange2(text) {
    this.contentObj.letter1 = text;
    this.saveItem()
    this.refresh();
  }
  handleTextChange3(text) {
    this.contentObj.letter2 = text;
    this.saveItem()
    this.refresh();
  }

  ngOnInit() {
    this.item = {};
    this.item.contentObj = {};
    const getData = (<any>window).courseware.getData;
    getData((data) => {
      console.log("读取数据", data)
      if (data) {
        this.item = data;
      } else {
        this.item = {};
      }
      if (!this.item.contentObj) {
        this.item.contentObj = {};
      }
      this.init();
      this.refresh();
      setTimeout(() => {
        this.changeDetectorRef.markForCheck();
        this.changeDetectorRef.detectChanges();
      }, 100);
      // this.save()
    }, this.KEY);
  }
  ngOnChanges() {

  }

  ngOnDestroy() {
  }

  init() {
    if (Object.keys(this.item.contentObj).length != 0 && this.item.contentObj.version && this.item.contentObj.version == defauleFormData.version) {
      console.log("读取数据", this.item.contentObj)
      this.contentObj = this.item.contentObj;
    } else {
      this.contentObj = defauleFormData;
      console.log("使用演示数据", this.contentObj)
      this.item.contentObj = this.contentObj;
    }
  }

  initData() {

  }

  handleMoveItemUp(items, index) {
    if (index != 0) {
      items[index] = items.splice(index - 1, 1, items[index])[0];
    } else {
      items.push(items.shift());
    }
    this.save()
  }

  handleMoveItemDown(items, index) {
    if (index != items.length - 1) {
      items[index] = items.splice(index + 1, 1, items[index])[0];
    } else {
      items.unshift(items.splice(index, 1)[0]);
    }
    this.save()
  }

  setButtonType(item, j) {
    item.letterIndex = j;
    this.save()
  }

  getButtonType(item, j) {
    if (item.letterIndex == j) {
      return 'primary';
    }
    return 'text';
  }

  onSwitchChange() {
    this.save()
  }

  deleteItem(item, index) {
    item.splice(index, 1)
    this.save()
  }

  addItem(item) {
    item.push({ text: "", isRight: false, shortAudio_url: '', image_url: "", letterIndex: -1 })
    this.saveItem();
    setTimeout(() => {
      window.scrollTo(0, document.body.scrollHeight);
    }, 1)
  }

  onImageUploadSuccessByItem(e, item) {
    item.image_url = e.url
    this, this.refresh()
    this.save();
  }


  onAudioUploadSuccessByItem(e, item) {
    item.shortAudio_url = e.url
    this.save();
  }

  onBgmUploadSuccess(e) {
    this.contentObj.bgm_url = e.url;
    this.save();
  }

  handleChange(info: { type: string, file: UploadFile, event: any }, item, key) {
    console.log('info:', info);
    switch (info.type) {
      case 'success':
        this.uploadSuccess(info.file, item, key);
        break;
    }
  }

  uploadSuccess(file, item, key) {
    console.log(item);
    item[key] = file.response.url;
    item[key + '_name'] = file.name;
    this.save();
  }


  saveItem() {
    this.save();
  }

  save() {
    (<any>window).courseware.setData(this.item, null, this.KEY);
    this.refresh();
    console.log("保存", JSON.stringify(this.item))
  }

  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }

  onCopyData(index) {
    this.contentObj.dataArray.push(JSON.parse(JSON.stringify(this.contentObj.dataArray[index])));
    this.save();
    setTimeout(() => {
      window.scrollTo(0, document.body.scrollHeight);
    }, 1)
  }

}

