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


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

	// 储存数据用
	saveKey = "op68_plus_new";
	// 储存对象
	item: ObjData

	init() {
		if (!this.item.questions) {
			this.item = new ObjData();
		}
		if (!this.item.finishAudioList) {
			this.item.finishAudioList = [];
		}
	}
	/**
	 * 储存图片数据
	 * @param e
	 */
	onAssetUploadSuccess2(e, key, item?) {
		if (!item)
			item = this.item
		item[key] = e.url;
		this.save();
	}

	onFinishAudioUploadSuccess(e, idx) {
		this.item.finishAudioList[idx] = e.url;
		this.save();
	}
	addQuestion() {
		if (this.item.questions.length >= 6)
			return
		let question = new Question();
		this.item.questions.push(question);
		this.save();
	}
	copyQuestion(idx) {
		let data = this.item.questions[idx]
		this.item.questions.push(JSON.parse(JSON.stringify(data)));
		this.save();
	}

	delQuestion(idx) {
		this.item.questions.splice(idx, 1);
		this.save();
	}

	addOption(idx) {
		let option = new Option();
		this.item.questions[idx].options.push(option);
		this.save();
	}
	addGroup(idx) {
		let option = new Group();
		this.item.questions[idx].group_arr.push(option);
		this.save();
	}
	removeGroup(questionIdx, optionIdx) {
		this.item.questions[questionIdx].group_arr.splice(optionIdx, 1);
		this.save();
	}
	copyGroupData: any;
	copyGroup(i, j) {
		let data = this.item.questions[i].group_arr[j];
		this.copyGroupData = JSON.parse(JSON.stringify(data));
	}
	putGroup(i, j) {
		if (!this.copyGroupData) {
			alert("请先复制分组内容")
			return;
		}
		this.item.questions[i].group_arr[j] = this.copyGroupData;
		this.save();
	}

	delOption(questionIdx, optionIdx) {
		this.item.questions[questionIdx].options.splice(optionIdx, 1);
		this.save();
	}
	onAudioUploadSuccess(e, key, item) {
		item[key] = e.url;
		this.save();
	}
	ngChange() {
		this.save();
	}
	copyData: any;
	copyoption(i, j) {
		let data = this.item.questions[i].options[j];
		this.copyData = JSON.parse(JSON.stringify(data));
	}
	putoption(i, j) {
		if (!this.copyData) {
			alert("请先复制卡牌内容")
			return;
		}
		this.item.questions[i].options[j] = this.copyData;
		this.save();
	}
}

export class ObjData {
	guideBegin_audio_url: string = "";
	endAudio: string = "";
	startLoadingAudio: string = "";
	// guideEnd_audio_url: string = "";
	guideRestart_audio_url: string = "";
	questions: Question[] = [];
	finishAudioList = [];
}

export class Question {
	question_audio_url: string = "";
	question_audio_url_end: string = "";
	// group_arr_1: Group = new Group();
	// group_arr_2: Group = new Group();
	options: Option[] = [];
	group_arr: Array<Group> = [new Group(), new Group()];
	type = 'txt';
}

export class Group {
	audio: string = "";
	text: string = "";
	image = ""
}

export class Option {
	text: string = "";
	image: string = "";
	audio: string = "";
	idx: number = 0;
}

