/**
 * Created by rockyl on 2018/11/6.
 */

import GraphicRenderer from "./GraphicRenderer";
import {decorators} from "scilla"

const {dirtyFieldDetector} = decorators;

/**
 * (圆角)矩形渲染器
 */
export default class RectRenderer extends GraphicRenderer {
	@dirtyFieldDetector
	width = 100;
	@dirtyFieldDetector
	height = 100;
	@dirtyFieldDetector
	cornerRadius = 0;

	protected getRenderSize(): any {
		const {width, height} = this;
		return {width, height};
	}

	/**
	 * @inheritDoc
	 */
	protected draw() {
		const {PI} = Math;
		const {currentCanvasContext, cornerRadius: r, bounds: {width, height}, _margin, _useCacheMode} = this;

		let offset = _useCacheMode ? _margin : 0;

		if (r) {
			currentCanvasContext.moveTo(offset + r, offset + 0);
			currentCanvasContext.lineTo(offset + width - r, offset + 0);
			currentCanvasContext.arc(offset + width - r, offset + r, r, PI * 3 / 2, PI * 2);
			currentCanvasContext.lineTo(offset + width, offset + height - r);
			currentCanvasContext.arc(offset + width - r, offset + height - r, r, 0, PI / 2);
			currentCanvasContext.lineTo(offset + r, offset + height);
			currentCanvasContext.arc(offset + r, offset + height - r, r, PI / 2, PI);
			currentCanvasContext.lineTo(offset + 0, offset + r);
			currentCanvasContext.arc(offset + r, offset + r, r, PI, PI * 3 / 2);
		} else {
			currentCanvasContext.rect(offset, offset, width, height);
		}

		super.draw();
	}
}
