/*
 * @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_ls_gramophone_2';

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)
	richText: cc.Node = null;

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

	// 录制中
	_recording = false;
	// 已录音
	_recorded = false;
	private _str = "";

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

	/**
	 * 初始化
	 * @param str 字符串
	 */
	init(str: string) {
		this._str = str;
		// this.label.setString(str, 580);//this.node.width  632
		this.richText.getComponent(cc.RichText).string = `<color=#ffffff>` + str + `</c>`
		this.reset();

		// this.scheduleOnce(() => {
		// this.node.height = this.label.getRows() * 48; // 更新节点高度
		// let lineHeight = this.richText.height / this.richText.getComponent(cc.RichText).lineHeight;
		this.node.height = this.richText.height;
		// let c = this.node.width - this.label.getLastStringLenght();
		console.log(str);
		// console.log("c: " + c);
		if (this.node.width - this.label.getLastStringLenght() >= 118) {//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 = 27;
			this.record_click.y = 27;
			this.myrecord.y = 27;
			this.myrecord_play.y = 27;
			// 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();
		// }, 0.1);
	}

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

		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;
	}
	private intervalId;
	private stopPlay = false;
	public playAction() {
		let count = 0;
		if (this.intervalId) clearInterval(this.intervalId);
		let icon_myrecord1 = cc.find("icon_myrecord1", this.myrecord_play);
		let icon_myrecord2 = cc.find("icon_myrecord2", this.myrecord_play);
		let icon_myrecord3 = cc.find("icon_myrecord3", this.myrecord_play);
		this.intervalId = setInterval(() => {
			count++;
			// icon_myrecord1.active = count % 3 == 2;
			icon_myrecord2.active = count % 3 == 0;
			icon_myrecord3.active = count % 3 == 1;
			if (this.stopPlay) {
				// btn_laba1.active = false;
				// btn_laba2.active = false;
				clearInterval(this.intervalId);
			}
		}, 150)
	}
	public stopAction() {
		if (this.intervalId) clearInterval(this.intervalId);
		let icon_myrecord1 = cc.find("icon_myrecord1", this.myrecord_play);
		let icon_myrecord2 = cc.find("icon_myrecord2", this.myrecord_play);
		let icon_myrecord3 = cc.find("icon_myrecord3", this.myrecord_play);
		icon_myrecord1.active = true;
		icon_myrecord2.active = true;
		icon_myrecord3.active = true;
	}

	getRecorded() {
		return this._recorded;
	}

	/**
	 * 正在播放中
	 */
	playing() {
		// this.tip.active = true;
		this.tip.opacity = 255;
		// this.label.node.color = cc.color(255, 96, 0);
		this.label.node.color = cc.color(251, 255, 177);
		this.richText.getComponent(cc.RichText).string = `<color=#FBFFB1>` + this._str + `</c>`
		this.hideLow();
	}

	/**
	 * 播放完成
	 */
	stop(noRecord = true) {
		// this.tip.active = false;
		this.tip.opacity = 0;
		this.label.node.color = cc.color(255, 255, 255);
		if (noRecord) {
			this.richText.getComponent(cc.RichText).string = `<color=#ffffff>` + this._str + `</c>`;
		} else {
			this.richText.getComponent(cc.RichText).string = this.changStr;
		}
		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();
	}
	cleanText() {
		this.changStr = "";
		this.richText.getComponent(cc.RichText).string = "";
	}
	public changStr = '';
	changColor(str, isChange = false) {

		if (isChange) this.changStr += `<color=#ff8f8f>` + str + `</color>`;
		else this.changStr += `<color=#ffffff>` + str + `</color>`;

		this.richText.getComponent(cc.RichText).string = this.changStr;
	}
}

