import { SGraphItem } from "@persagy-web/graph/lib";
import { SColor, SLinearGradient, SPainter, SRadialGradient, SRect } from "@persagy-web/draw/lib";

/**
 * 渐变
 *
 * @author 郝洁 <haojie@persagy.com>
 */
export class GradRect extends SGraphItem {
    /** X 坐标最小值  */
    minX = 0;
    /** Y 坐标最小值  */
    minY = 0;
    /** Y 坐标最大值  */
    maxY = 1000;
    /** X 坐标最大值  */
    maxX = 1000;
    /** 实例化  */
    gradient: SRadialGradient | SLinearGradient | null = null;

    /**
     * 构造函数
     *
     * @param parent     Item 图像引擎
     * @param grad      渐变类
     */
    constructor(parent: SGraphItem | null, grad: SRadialGradient | SLinearGradient) {
        super(parent);
        this.gradient = grad;
        this.gradient.addColorStop(0, new SColor('#ff483b'));
        this.gradient.addColorStop(0.5, new SColor('#04ff00'));
        this.gradient.addColorStop(1, new SColor('#3d4eff'));
    }

    /**
     * 矩形数据类型绘制
     *
     * @return 边界区域
     */
    boundingRect() {
        return new SRect(
            this.minX,
            this.minY,
            this.maxX - this.minX,
            this.maxY - this.minY
        );
    }

    /**
     * Item 绘制操作
     * @param painter   绘制对象
     */
    onDraw(painter: SPainter) {
        painter.pen.color = SColor.Black;
        painter.brush.gradient = this.gradient;
        painter.drawRect(0, 0, 1000, 1000);
        if (this.gradient instanceof SRadialGradient) { //实例在 SRadialGradient 构造函数中
            painter.brush.color = SColor.Transparent;
            painter.drawCircle(this.gradient.x1, this.gradient.y1, this.gradient.r1);
            painter.drawCircle(this.gradient.x2, this.gradient.y2, this.gradient.r2);
        }
    }
}
