import { Utils, Object3D } from '@uino/thing';

/**
 * @class InstancedFont
 * @summary 批量字体对象,使用实例化渲染(Instanced Drawing)技术高性能渲染大量文本标签。
 * 通过一次性传入位置矩阵、文本内容和字体大小数组,即可批量生成大量文字标签,
 * 支持 Sprite(广告牌)和 Plane(平面)两种渲染模式,并支持实例级别的可见性和大小的动态控制。
 * 适用于大规模标签展示、楼层编号、设备标记、编号标注等需要大量文本渲染的场景。
 * @memberof THING
 * @extends THING.Object3D
 * @public
 * @example
 * // 创建批量字体标签
 * const instancedFont = new THING.EXTEND.InstancedFont({
 *   matrixs: [
 *     [1,0,0,0, 0,1,0,0, 0,0,1,0, 10,5,0,1],
 *     [1,0,0,0, 0,1,0,0, 0,0,1,0, 20,5,0,1],
 *     [1,0,0,0, 0,1,0,0, 0,0,1,0, 30,5,0,1]
 *   ],
 *   fontTexts: ['A栋', 'B栋', 'C栋'],
 *   fontSizes: [32, 32, 32],
 *   pickId: [1001, 1002, 1003],
 *   renderType: 'Sprite'
 * });
 * 
 * // 隐藏指定实例
 * instancedFont.setVisible(1002, false);
 * 
 * // 设置保持屏幕大小
 * instancedFont.setKeepSize(true);
 * instancedFont.setKeepSizeRange(12, 64);
 */
class InstancedFont extends Object3D {
	/**
	 * 创建实例化字体对象
	 * @param {object} param - 配置参数
	 * @param {Array} param.matrixs - 矩阵数组,用于定义每个实例的位置、旋转和缩放
	 * @param {Array<string>} param.fontTexts - 文本内容数组,对应每个实例显示的文本
	 * @param {Array<number>} param.fontSizes - 字体大小数组
	 * @param {Array<number>} param.pickId - 拾取ID数组,用于交互拾取
	 * @param {string} [param.renderType='Sprite'] - 渲染类型,可选值: 'Sprite' (广告牌), 'Plane' (平面)
	 * @example
	 * const instancedFont = new THING.EXTEND.InstancedFont({
	 *     matrixs: matrixs,
	 *     fontTexts: fontTexts,
	 *     fontSizes: fontSizes,
	 *     pickId: pickIds,
	 *     renderType: "Sprite"
	 * });
	 */
    constructor(param) {
		super(param);
	}

	/**
	 * 加载资源
	 * @param {object} options - 加载选项
	 * @param {Function} resolve - 成功回调
	 * @param {Function} reject - 失败回调
	 * @private
	 */
    onLoadResource(options, resolve, reject) {
		let dynamic = Utils.parseValue(options['dynamic'], false);
		if (!dynamic) {
			Utils.setTimeout(() => {
				if (this.destroyed) {
					reject(`The object had been destroyed, skip to load resources`);
				}
				else {
					let node = this.onCreateBodyNode(options);

					this.body.setNode(node);

					resolve();
				}
			});
		}
	}

	/**
	 * 创建节点主体
	 * @param {object} params - 创建参数
	 * @returns {object} 创建的节点对象
	 * @private
	 */
    onCreateBodyNode(params) {
		const node = Utils.createObject('InstancedFont', {
			external: {
				matrixs:params.matrixs || [],
                fontTexts:params.fontTexts || [],
                fontSizes:params.fontSizes ||[],
                pickId:params.pickId || [],
                renderType:params.renderType || "",
			}
		});
		return node;
	}

	/**
	 * 设置指定实例的可见性
	 * @param {number} id - 实例的拾取ID (pickId)
	 * @param {boolean} value - 是否可见
	 * @public
	 */
	setVisible(id, value){
		this.node.setVisible(id, value);
	}

	/**
	 * 获取指定实例的可见性
	 * @param {number} id - 实例的拾取ID (pickId)
	 * @returns {boolean} 是否可见
	 * @public
	 */
	getVisible(id){
		return this.node.getVisible(id);
	}
	
	/**
	 * 设置保持大小的范围
	 * @param {number} minSize - 最小尺寸
	 * @param {number} maxSize - 最大尺寸
	 * @public
	 */
	setKeepSizeRange(minSize, maxSize){
		this.node.setKeepSizeRange(minSize, maxSize);
	}

	/**
	 * 获取保持大小的范围
	 * @returns {Array<number>} [minSize, maxSize]
	 * @public
	 */
	getKeepSizeRange(){
		return this.node.getKeepSizeRange();
	}

	/**
	 * 设置是否保持屏幕大小
	 * @param {boolean} value - 是否保持屏幕大小
	 * @public
	 */
	setKeepSize(value){
		this.node.setKeepSize(value);
	}

	/**
	 * 获取是否保持屏幕大小
	 * @returns {boolean} 是否保持屏幕大小
	 * @public
	 */
	getKeepSize(){
		return this.node.getKeepSize();
	}

	/**
	 * 设置渲染类型
	 * @param {string} value - 渲染类型 ('Sprite' 或 'Plane')
	 * @public
	 */
	setRenderType(value){
		this.node.setRenderType(value);
	}

	/**
	 * 获取渲染类型
	 * @returns {string} 渲染类型
	 * @public
	 */
	getRenderType(){
		return this.node.getRenderType();
	}
}

export { InstancedFont }