import { Utils } from '../common/Utils';
import { BaseModelObject3D } from './BaseModelObject3D';

function _getParam(width, height, depth, param) {
	if (Utils.isObject(width)) {
		return width;
	}

	if (Utils.isObject(height)) {
		return height;
	}

	if (Utils.isObject(depth)) {
		return depth;
	}

	return param;
}

function _buildParam(width, height, depth, param) {
	param = _getParam(width, height, depth, param);

	param['body'] = param['body'] || {};
	param['body']['localScale'] = param['body']['localScale'] || [
		Utils.parseFloat(width, Utils.parseFloat(param['width'], 1)),
		Utils.parseFloat(height, Utils.parseFloat(param['height'], 1)),
		Utils.parseFloat(depth, Utils.parseFloat(param['depth'], 1)),
	];
	param['instanceGroupName'] = param['instanceGroupName'] || 'Box';
	return param;
}

/**
 * Box - 盒子几何体
 *
 * 用于创建盒子(长方体)模型,支持自定义宽度、高度、深度和各个方向的分段数。
 * @class Box
 * @memberof THING
 * @extends THING.BaseModelObject3D
 * @public
 * @example
 * const box = new THING.Box(2, 1, 1, {
 *   widthSegments: 2,
 *   heightSegments: 2,
 *   depthSegments: 2,
 *   position: [0, 0.5, 0],
 * });
 */
class Box extends BaseModelObject3D {

	static defaultTagArray = ['Geometry'];

	/**
	 * 盒子
	 * @param {number} [width=1.0] 宽度(自身 x 轴方向),单位:米
	 * @param {number} [height=1.0] 高度(自身 y 轴方向),单位:米
	 * @param {number} [depth=1.0] 深度(自身 z 轴方向),单位:米
	 * @param {object} [param] 初始参数
	 * @param {number} [param.widthSegments=1] 宽度方向分段数
	 * @param {number} [param.heightSegments=1] 高度方向分段数
	 * @param {number} [param.depthSegments=1] 深度方向分段数
	 * @constructor
	 * @public
	 */
	constructor(width, height, depth, param = {}) {
		super(_buildParam(width, height, depth, param));
	}

	// #region Accessor

	/**
	 * 设置/获取 宽度(自身x轴方向长度)
	 * @type {number}
	 * @default 1.0
	 * @public
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.width == 1)
	 * box.width = 10;
	 * // @expect(box.width == 10)
	 */
	get width() {
		return this.body.scale[0];
	}
	set width(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [value, scale[1], scale[2]];
		});
	}

	/**
	 * 设置/获取 高度(自身y轴方向长度)
	 * @type {number}
	 * @default 1.0
	 * @public
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.height == 1)
	 * box.height = 10;
	 * // @expect(box.height == 10)
	 */
	get height() {
		return this.body.scale[1];
	}
	set height(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [scale[0], value, scale[2]];
		});
	}

	/**
	 * 设置/获取 深度(自身z轴方向长度)
	 * @type {number}
	 * @default 1.0
	 * @public
	 * @example
	 * let box = new THING.Box();
	 * // @expect(box.depth == 1)
	 * box.depth = 10;
	 * // @expect(box.depth == 10)
	 */
	get depth() {
		return this.body.scale[2];
	}
	set depth(value) {
		this._processBodyWithoutPivot((body) => {
			let scale = body.scale;
			body.scale = [scale[0], scale[1], value];
		});
	}

	// #endregion

	/**
	 * Check whether it's box object.
	 * @type {boolean}
	 * @example
	 * let box = new THING.Box();
	 * let ret = box.isBox;
	 * // @expect(ret == true)
	 */
	get isBox() {
		return true;
	}

}

export { Box }