import { SPainter, SPoint, SRect } from "@persagy-web/draw/lib";
import { SGraphItem } from "@persagy-web/graph/lib";
import {Casement} from "@persagy-web/big/lib/types/floor/Casement";
import {ItemColor, ItemOrder} from "@persagy-web/big/lib";

/**
 * 窗户item
 *
 * @author  郝建龙
 */
export class SWindowItem extends SGraphItem {
    /** 窗户数据    */
    data: Casement;
    /** 窗户轮廓线坐标list  */
    private readonly pointArr: SPoint[] = [];
    /** X坐标最小值  */
    private minX = Number.MAX_SAFE_INTEGER;
    /** X坐标最大值  */
    private maxX = Number.MIN_SAFE_INTEGER;
    /** Y坐标最小值  */
    private minY = Number.MAX_SAFE_INTEGER;
    /** Y坐标最大值  */
    private maxY = Number.MIN_SAFE_INTEGER;

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      窗户数据
     */
    constructor(parent: SGraphItem | null, data: Casement) {
        super(parent);
        this.data = data;
        this.zOrder = ItemOrder.windowOrder;
        if (this.data.OutLine.length) {
            this.pointArr = this.data.OutLine[0].map(
                (t): SPoint => {
                    let x = t.X,
                        y = -t.Y;
                    if (x < this.minX) {
                        this.minX = x;
                    }
                    if (y < this.minY) {
                        this.minY = y;
                    }
                    if (x > this.maxX) {
                        this.maxX = x;
                    }
                    if (y > this.maxY) {
                        this.maxY = y;
                    }
                    return new SPoint(x, y);
                }
            );
        }
    } // Constructor

    /**
     * Item对象边界区域
     *
     * @return SRect
     */
    boundingRect(): SRect {
        return new SRect(
            this.minX,
            this.minY,
            this.maxX - this.minX,
            this.maxY - this.minY
        );
    } // Function boundingRect()

    /**
     * Item绘制操作
     *
     * @param   painter       painter对象
     */
    onDraw(painter: SPainter): void {
        painter.pen.color = ItemColor.windowColor;
        painter.pen.lineWidth = painter.toPx(1);
        painter.drawPolyline(this.pointArr);
    } // Function onDraw()
} // Class SWindowItem
