/*
 * @Author: ls
 * @Date: 2022-05-21 21:59:51
 * @LastEditTime: 2022-06-13 10:34:48
 * @LastEditors: ls
 * @Description:
 * @FilePath: \ls_gramophone\assets\ls_gramophone\script\Item.ts
 */

import simpleLabel from './simpleLabel';

const { ccclass, property } = cc._decorator;

@ccclass
export default class Item extends cc.Component {
	@property(simpleLabel)
	label: simpleLabel = null;

	@property(cc.Node)
	tip: cc.Node = null;

	@property(cc.Node)
	record: cc.Node = null;

	@property(cc.Node)
	record_click: cc.Node = null;

	@property(cc.Node)
	myrecord: cc.Node = null;

	@property(cc.Node)
	myrecord_play: cc.Node = null;

	@property(cc.Node)
	low: cc.Node = null;

	// 录制中
	_recording = false;
	// 已录音
	_recorded = false;

	protected start(): void {
		// this.init('One day,her grandma got sick!');
	}

	/**
	 * 初始化
	 * @param str 字符串
	 */
	init(str: string) {
		this.label.setString(str, this.node.width);
		this.reset();

		this.scheduleOnce(() => {
			this.node.height = this.label.getRows() * 48; // 更新节点高度

			if (this.node.width - this.label.getLastStringLenght() >= 170 - 48) {
				this.record.getComponent(cc.Widget).enabled = false;
				this.record_click.getComponent(cc.Widget).enabled = false;
				this.myrecord.getComponent(cc.Widget).enabled = false;
				this.myrecord_play.getComponent(cc.Widget).enabled = false;

				this.record.y = 18;
				this.record_click.y = 18;
				this.myrecord.y = 18;
				this.myrecord_play.y = 18;

				this.record.x = this.label.getLastStringLenght() + 32;
				this.record_click.x = this.label.getLastStringLenght() + 32;
				this.myrecord.x = this.label.getLastStringLenght() + 32 + 86;
				this.myrecord_play.x = this.label.getLastStringLenght() + 32 + 86;
			}
			this.stop();
		});
	}

	/**
	 * 重置所有按钮
	 */
	reset() {
		this.tip.active = false;

		this.record.active = true;
		this.record_click.active = false;

		this.myrecord.active = false;
		this.myrecord_play.active = false;
	}

	/**
	 * 正在录制声音
	 */
	recording() {
		this._recording = true;
		this.record.active = false;
		this.record_click.active = true;
	}

	/**
	 * 结束录制
	 */
	recorded() {
		this._recording = false;
		this.record.active = true;
		this.record_click.active = false;
	}

	getRecording() {
		return this._recording;
	}

	/**
	 * 未录制音频
	 */
	hideRecord() {
		this._recorded = false;
		this.myrecord.active = false;
		this.myrecord_play.active = false;
	}

	/**
	 * 播放已录制声音
	 */
	playRecord() {
		this._recorded = true;
		this.myrecord.active = false;
		this.myrecord_play.active = true;
	}

	/**
	 * 停止已录制声音
	 */
	stopRecord() {
		this._recorded = true;
		this.myrecord.active = true;
		this.myrecord_play.active = false;
	}

	getRecorded() {
		return this._recorded;
	}

	/**
	 * 正在播放中
	 */
	playing() {
		this.tip.active = true;
		// this.label.node.color = cc.color(255, 96, 0);
		this.label.node.color = cc.color(108, 185, 0);
		this.hideLow();
	}

	/**
	 * 播放完成
	 */
	stop() {
		this.tip.active = false;
		this.label.node.color = cc.color(44, 77, 140);
		this.showLow();
	}

	hideLow() {
		this.label.node.children.forEach((e) => {
			e.active = false;
		});
	}

	showLow() {
		this.label.node.children.forEach((e) => {
			e.active = true;
		});
	}

	clearLow() {
		this.label.node.removeAllChildren();
	}

	setLow(str: string, index: number = 1) {
		let ps = this.label.getTargetStringPos(str);
		if (index > ps.length) {
			return;
		}

		let p = ps[index - 1];
		let low = cc.instantiate(this.low);
		low.parent = this.label.node;
		low.active = true;
		low.width = p.z;
		low.getComponentInChildren(cc.Label).string = str;
		low.setPosition(p.y, (p.x - 1) * 48);
	}
}
