import { SGraphyItem, SMouseEvent } from '@sybotan-web/graphy'
import { SRect, SSize } from "@sybotan-web/base";
import { SPen, SPainter, SColor } from "@sybotan-web/draw";

/**
 * 图形Item类  todo
 *
 */
export class SGraphyImgItem extends SGraphyItem {
	imgSource: string; //可以为url,base64
	X: number;
	Y: number;
	width: number = 0;
	height: number = 0;
	img: CanvasImageSource

	/**
	 * 构造函数
	 * 
	 * @param width        		图片宽度
	 * @param height   				图片高度
	 * @param imgSource     	图片源
	 * @param X        				图片向x轴的偏移量
	 * @param Y        				图片向y轴的偏移量
	 * @param parent   				指向父元素
	 */
	constructor(
		parent: SGraphyItem | null,
		imgSource: string,
		X: number,
		Y: number,
		width: number,
		height: number,
	) {
		super(parent);
		this.imgSource = imgSource;
		this.X = X;
		this.Y = Y;
		this.width = width;
		this.height = height;
		this.img = new Image();
		this.img.src = this.imgSource;
	} // Constructor()

	/**
	 * Item对象边界区域
	 * 
	 * @return SRect
	 */
	boundingRect(): SRect {
		return new SRect(
			this.X,
			this.Y,
			this.width,
			this.height
		);
	} // Function boundingRect()
	// contains(x: number, y: number): boolean {
	// 	return this.boundingRect().contains(x - this.X, y - this.Y);
	// }
	onClick(event: SMouseEvent): boolean {
		this.$emit('click', event);
		return true;
	} // Function onClick()
	/**
  * 鼠标双击事件
  *
  * @param   event   保存事件参数
  * @return  boolean
  */
	onDoubleClick(event: SMouseEvent): boolean {
		this.$emit('doubleClick', event);
		return true;
	}
	/**
  * 鼠标按下事件
  *
  * @param   event   保存事件参数
  * @return  boolean
  */
	onMouseDown(event: SMouseEvent): boolean {
		this.$emit('mouseDown', event);
		return true;
	} // Function onMouseDown()

	/**
	 * 鼠标移动事件
	 *
	 * @param   event   保存事件参数
	 * @return  boolean
	 */
	onMouseMove(event: SMouseEvent): boolean {
		this.$emit('mouseMove', event);
		return true;
	} // Function onMouseMove()

	/**
	 * 释放鼠标事件
	 *
	 * @param   event   保存事件参数
	 * @return  boolean
	 */
	onMouseUp(event: SMouseEvent): boolean {
		this.$emit('mouseUp', event);
		return true;
	}
	/**
	 * Item绘制操作
	 *
	 * @param   painter       painter对象
	 * @param   rect          绘制区域
	 */
	onDraw(painter: SPainter, rect?: SRect): void {
		painter.drawImage(this.img, this.X, this.Y, this.width, this.height);
	}
} // Class SGraphyImgItem
