import { Utils } from '../common/Utils';
import { MathUtils } from '../math/MathUtils';
import { BaseLine } from './BaseLine';

/**
 * FatLine - 宽线
 * 
 * 可以设置像素级宽度的线(面向相机),支持 UV 流动和拐角控制。
 * 
 * @class FatLine
 * @memberof THING
 * @extends THING.BaseLine
 * @public
 * 
 * @example
 * const line = new THING.FatLine({
 *   points: [[0, 0, 0], [5, 0, 0], [5, 0, 5]],
 *   width: 10,
 *   style: { color: '#ff0000' },
 * });
 */
class FatLine extends BaseLine {

	static defaultTagArray = ['Line'];

	/**
	 * 构造函数
	 * @param {object} param 初始化参数
	 * @param {Array<number[]>} param.points 路径坐标的数组
	 * @param {number} [param.width=1] 线宽度(像素)
	 * @param {number} [param.cornerThreshold=0.4] 转角系数(详见属性说明)
	 * @param {boolean} [param.uvScroll=false] 是否开启贴图流动
	 * @param {number[]} [param.uvScrollSpeed=[1,0]] 贴图流动速度
	 * @param {object} [param.style] 线样式
	 * @constructor
	 * @public
	 */
	constructor(param = {}) {
		super(param);
	}

	// #region Overrides

	onSetupResource(param) {
		this._width = Utils.parseNumber(param['width'], 1);
		this._cornerThreshold = Utils.parseNumber(param['cornerThreshold'], 0.4);

		super.onSetupResource(param);
	}

	onCreateBodyNode(param) {
		const node = Utils.createObject('FatLine', { app: this.app });
		return node;
	}

	onSetupBodyNode(node) {
		super.onSetupBodyNode(node);

		node.setWidth(this._width);
		node.setCornerThreshold(this._cornerThreshold);
	}

	onRefreshAttributes() {
		let bodyNode = this.bodyNode;
		bodyNode.setWidth(this._width);
		bodyNode.setCornerThreshold(this._cornerThreshold);
	}

	onExportExternalData() {
		let external = Object.assign({}, super.onExportExternalData());
		Utils.setAttributeIfExist(external, 'width', this);
		Utils.setAttributeIfExist(external, 'cornerThreshold', this);
		return external;
	}

	// #endregion

	// #region Accessor

	/**
	 * 线宽度(像素)
	 * @type {number}
	 * @default 1.0
	 * @example
	 * // 获取宽度
	 * const width = this.width;
	 * // 设置宽度
	 * this.width = 2.0;
	 * @public
	 */
	get width() {
		return this._width;
	}
	set width(value) {
		this._width = value;

		this.bodyNode.setWidth(this._width);
	}

	/**
	 * 线方向改变的控制转角效果的一个参数 值越小,转角越尖锐,但是值较大时,拐弯处可能会有间断
	 * @type {number}
	 * @default 0.4
	 * @example
	 * // 获取转角系数
	 * const cornerThreshold = this.cornerThreshold;
	 * // 设置转角系数
	 * this.cornerThreshold = 0.8;
	 * @public
	 */
	get cornerThreshold() {
		return this._cornerThreshold;
	}
	set cornerThreshold(value) {
		this._cornerThreshold = MathUtils.clamp(value, 0.1, 1);

		this.bodyNode.setCornerThreshold(this._cornerThreshold);
	}

	// #endregion

	get isFatLine() {
		return true;
	}

}

export { FatLine }