/*
 * @Author: ls
 * @Date: 2022-05-21 18:06:20
 * @LastEditTime: 2022-06-14 17:52:50
 * @LastEditors: ls
 * @Description:
 * @FilePath: \ls_gramophone\assets\ls_gramophone\script\simpleLabel.ts
 */

const { ccclass, property } = cc._decorator;

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

	// 源字符串
	private _sourceString = '';
	// 源字符串数组
	private _sourceArray = [];

	// 目标字符串
	private _targetString = '';
	// 目标字符串的最后一行字符串
	private _lastString = '';
	// 目标字符串行数
	private _rows = 0;

	// 目标宽度
	private _targetWidget = 0;

	// TODO 字符数组(后期处理标点符号)
	private _characters = [',', '.'];

	protected start(): void {
		// 测试代码
		// this.setString('One day,her grandma got sick.', 562);
	}

	/**
	 * 设置字符串
	 * @param sourceString 源字符串
	 * @param targetWidget 目标宽度
	 * @param splitter 分割器(默认使用空格)
	 */
	public setString(sourceString: string, targetWidget: number, splitter: string = ' ') {
		if (!this.label) {
			this.label = this.node.getComponent(cc.Label);
			if (!this.label) {
				return;
			}
		}

		this._targetWidget = targetWidget;
		// 保存源字符串
		this._sourceString = sourceString;
		// 保存源字符串数组(根据空格分割)
		this._sourceArray = sourceString.split(splitter);

		// 重置目标字符串
		this._targetString = '';
		// 重置目标字符串的最后一行
		this._lastString = '';
		for (let index = 0; index < this._sourceArray.length; index++) {
			const element = this._sourceArray[index]; // 分割后每个字符串
			// 临时字符串
			const tempString = this._lastString;

			if (this._lastString === '') {
				this._lastString = element; // 设置字符串
			} else {
				this._lastString = this._lastString + ' ' + element; // 添加字符串
			}

			// 如果字符串长度 >= 设计字符串长度
			if (this.getLetterWidth(this.label, this._lastString) >= targetWidget) {
				// 给字符串添加换行符
				this._targetString = this._targetString + '\n' + tempString;
				// 增加行数
				this._rows++;
				// 重置临时字符串
				this._lastString = element;
			}
		}
		// 给字符串添加换行符
		this._targetString = this._targetString + '\n' + this._lastString;
		// 增加行数
		this._rows++;
		// 设置目标字符串
		this.label.string = this._targetString;
	}

	/**
	 * 获取目标字符串的最后一行字符串的长度
	 */
	public getLastStringLenght() {
		if (!this.label) {
			this.label = this.node.getComponent(cc.Label);
			if (!this.label) {
				return 0;
			}
		}

		if (this._lastString === '') {
			return 0;
		}
		return this.getLetterWidth(this.label, this._lastString);
	}

	/**
	 * 获取目标字符串的行数
	 */
	public getRows() {
		if (!this.label) {
			this.label = this.node.getComponent(cc.Label);
			if (!this.label) {
				return 0;
			}
		}
		return this._rows;
	}

	/**
	 * 获取字符串的宽度
	 * @param letter 字符串
	 * @returns
	 */
	private getLetterWidth(label: cc.Label, letter: string) {
		let comp = label;
		let assemblerData = comp['_assemblerData'];
		let ctx = assemblerData.context;
		let metric = ctx.measureText(letter);
		let width = (metric && metric.width) || 0;
		return width;
	}

	/**
	 * 获取所有字符串的位置
	 * @param str
	 * @returns
	 */
	getTargetStringPos(str: string) {
		let poss = [];

		let rows = this._rows;
		let lastString = '';
		if (this._sourceArray && this._sourceArray.length) {
			for (let index = 0; index < this._sourceArray.length; index++) {
				let element = this._sourceArray[index];

				// 临时字符串
				let tempString = lastString;

				if (lastString === '') {
					lastString = element; // 设置字符串
				} else {
					lastString = lastString + ' ' + element; // 添加字符串
				}

				// 如果字符串长度 >= 设计字符串长度
				if (this.getLetterWidth(this.label, lastString) >= this._targetWidget) {
					// 增加行数
					lastString = element;
					tempString = '';
					rows--;
				}

				let temp = element;
				// TODO 处理标点符号
				// if (element.indexOf(',') !== -1 || element.indexOf('.') !== -1 || element.indexOf('!') !== -1 || element.indexOf('?') !== -1) {
				// 	temp = temp.substring(0, temp.length - 1);
				// }

				if (temp === str) {
					let x = this.getLetterWidth(this.label, tempString);
					if (x !== 0) {
						x += this.getLetterWidth(this.label, ' ');
					}
					poss.push(cc.v3(rows, x, this.getLetterWidth(this.label, str)));
				}
			}
		}

		return poss;
	}
}
