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



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

  picArr = [];

  _item: any;

  stopPlayBgAudio = false;


  KEY = 'hw_003';

  answerType = 'A';
  showGuideAudio;



  // @Input()
  set item(item) {
    this._item = item;
    // this.init();

  }
  get item() {
    return this._item;
  }

  @Output()
  update = new EventEmitter();



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

  }


  ngOnInit() {

    this.item = {};
    this.item.contentObj = {};

    const getData = (<any> window).courseware.getData;
    getData((data) => {

      if (data) {
        this.item = data;
      } else {
        this.item = {};
      }

      if ( !this.item.contentObj ) {
        this.item.contentObj = {};
      }

      console.log('~data:', data);
      this.init();

      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();

      this.refresh();

    }, this.KEY);

    // this.initData();
  }
  ngOnChanges() {

  }

  ngOnDestroy() {
  }


  init() {

    if (this.item.contentObj.picArr) {
      this.picArr = this.item.contentObj.picArr;
    } else {
      this.picArr = this.getDefaultPicArr();
      this.item.contentObj.picArr = this.picArr;
    }

    if (!this.item.contentObj.optionType) {
      this.item.contentObj.optionType = 'A';
    }

    this.stopPlayBgAudio = this.item.contentObj.stopPlayBgAudio;

    let tmpFlag = false;

    for (let i = 0; i < this.picArr.length; i++) {
      const data = this.picArr[i];
      if (!data.optionType || !data.answerType) {
        tmpFlag = true;
      }

      if (!data.optionType) {
        if (data.pic_url) {
          data.optionType = 'B';
        } else {
          data.optionType = 'A';
        }
      }

      if (!data.answerType) {
        if (data.question_pic_url) {
          data.answerType = 'B';
        } else {
          data.answerType = 'A';
        }
      }
    }

    if (tmpFlag) {
      this.save();
    }

    console.log('item:' , this.item);
    // this.picArr = this.getDefaultPicArr();
    // this.item.contentObj.picArr = this.picArr;
    // console.log('this.item:;', this.picArr);

  }

  getDefaultPicArr() {
    const arr = [];
    // for (let i = 0; i < 4; i ++) {
    //   const data = {};
    //   data['pic_url'] = '';
    //
    //   const soundArr = [];
    //   for (let i = 0; i < 3; i++) {
    //     const tmpData = {};
    //     tmpData['answer'] = false;
    //     tmpData['audio_url'] = '';
    //     soundArr.push(tmpData);
    //   }
    //   data['soundArr'] = soundArr;
    //
    //   arr.push(data);
    // }

    return arr;
  }



  initData() {


  }


  deleteItem(data) {
    const index = this.picArr.indexOf(data);
    if (index !== -1) {
      this.picArr.splice(index, 1);
    }

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







  onImageUploadSuccessByItem(e, item, id = null) {

    if (id != null) {
      item[id + '_pic_url'] = e.url;
    } else {
      item.pic_url = e.url;
    }

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

    // console.log('this.item: ', this.item);
  }




  onAudioUploadSuccessByItem(e, item, id = null) {

    if (id != null) {
      item[id + '_audio_url'] = e.url;
    } else {
      item.audio_url = e.url;
    }
    // this.update.emit(this.item);
    this.save();
  }



  addPic() {

    const data = {
      pic_url: '',
      audio_url: '',
      answerType: 'A',
      optionType: 'A',
      // text: '',
      // radioValue: 'A'
    }

    const answerArr = [];
    const idArr = ['A', 'B', 'C', 'D'];
    for (let i = 0; i < 4; i++) {
      answerArr.push({id: idArr[i]});
    }
    data['answerArr'] = answerArr;
    data['answerId'] = '0';

    this.picArr.push(data);

    this.saveItem();
  }

  radioClick(it, radioValue) {
    it.radioValue = radioValue;

    this.saveItem();
  }

  clickCheckBox() {
    console.log(' in clickCheckBox');

    this.item.contentObj.stopPlayBgAudio = !this.item.contentObj.stopPlayBgAudio;
    this.saveItem();
  }



  saveItem() {

    console.log(' in saveItem');
    // this.update.emit(this.item);

    console.log('this.item' + JSON.stringify(this.item));
    this.save();
  }


  save() {
    (<any> window).courseware.setData(this.item, null, this.KEY);
    this.refresh();
  }

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


  guideBtnClick(value) {
    this.showGuideAudio = value;
  }
}

