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

import { Course, Picture, Color } from './Course';

import { NzMessageService } from 'ng-zorro-antd/message';

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

  // 储存数据用
  saveKey = "PU-17";
  // 储存对象
  item;

  colorArr = [];

  courseTypeArr = [];

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

  }


  ngOnInit() {

    this.item = new Course();
    this.item.type = '01';

    this.initColorArr();

    this.initCourseType();

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

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

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {

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

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

  initColorArr() {
    this.colorArr.push({ code: '01', name: '红色', color: '#f3440d', picUrl: 'assets/play/palette/red.png' });

    this.colorArr.push({ code: '02', name: '紫色', color: '#c80df3', picUrl: 'assets/play/palette/purple.png' });

    this.colorArr.push({ code: '03', name: '绿色', color: '#26eb45', picUrl: 'assets/play/palette/green.png' });

    this.colorArr.push({ code: '04', name: '蓝色', color: '#404dfb', picUrl: 'assets/play/palette/blue.png' });

    this.colorArr.push({ code: '05', name: '橘色', color: '#ff9600', picUrl: 'assets/play/palette/orange.png' });

    this.colorArr.push({ code: '06', name: '粉色', color: '#ff6073', picUrl: 'assets/play/palette/pink.png' });

    this.colorArr.push({ code: '07', name: '天蓝色', color: '#07d6ff', picUrl: 'assets/play/palette/skyBlue.png' });

    this.colorArr.push({ code: '08', name: '紫粉色', color: '#ff07ff', picUrl: 'assets/play/palette/purplePink.png' });
  }

  initCourseType() {

    this.courseTypeArr.push({ code: '01', name: '文本' });

    this.courseTypeArr.push({ code: '02', name: '音频' });

    this.courseTypeArr.push({ code: '03', name: '图片' });

  }


  /**
   * 储存图片数据
   * @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();
  }

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

  addPicItem() {

    const picItem = new Picture();

    this.item.picArr.push(picItem);

    this.save();
  }

  delPicItem(index) {
    if (index !== -1) {
      this.item.picArr.splice(index, 1);
      this.save();
    }
  }

  addColorItem() {
    const color = new Color();
    color.val = "#f3440d";
    this.item.colorArr = [...this.item.colorArr, color];
    this.save();
  }

  delColorItem(index) {
    if (index !== -1) {
      this.item.colorArr.splice(index, 1);
      this.item.colorArr = [...this.item.colorArr];
      this.save();
    }
  }

  onChange(event, item, key) {

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

  validatorMaxLength(value, length) {
    if (!value) {
      return true;
    }

    const chr = value.replace(' ', '');

    if (chr.length > length) {
      let id = this.message.error('超过' + length + '个字母', { nzDuration: 0 }).messageId;
      setTimeout(() => {
        this.message.remove(id);
      }, 3000);
      return false;
    }
    else {
      return true;
    }
  }

  saveContent(value) {

    let result = this.validatorMaxLength(value, 20);

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

