/**
 * Created by rockyl on 2018-12-07.
 */

import {dirtyFieldTrigger} from "../tools/decorators";
import HashObject from "../core/HashObject";

/**
 * 尺寸类
 */
export default class Size extends HashObject{
	/**
	 * 宽度
	 */
	@dirtyFieldTrigger
	width: number;

	/**
	 * 高度
	 */
	@dirtyFieldTrigger
	height: number;

	onChange;

	constructor(width: number = NaN, height: number = NaN, onChange?: Function) {
		super();

		this.onChange = onChange;

		this.width = width;
		this.height = height;
	}

	/**
	 * 置空
	 */
	setNaN(){
		this.width = NaN;
		this.height = NaN;
	}

	/**
	 * 是否空尺寸
	 */
	get isEmpty(){
		return this.width === 0 && this.height === 0;
	}

	/**
	 * 设置宽高
	 * @param width
	 * @param height
	 */
	set(width?: number, height?: number) {
		if (width !== undefined) {
			this.width = width;
		}

		if (height !== undefined) {
			this.height = height;
		}
	}

	/**
	 * 克隆一个尺寸实例
	 */
	clone(): Size {
		return new Size(this.width, this.height);
	}

	/**
	 * 从一个尺寸实例拷贝
	 * @param target
	 */
	copyFrom(target) {
		this.width = target.width;
		this.height = target.height;
	}

	onModify(value, key, oldValue) {
		this.onChange && this.onChange(value, key, oldValue);
	}
}
