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

/**
 * 墙item
 *
 * @author  郝建龙
 */
export class SWallItem extends SGraphItem {
    /** 墙数据 */
    data: Wall;
    /** 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;
    /** 墙轮廓线坐标list  */
    private readonly pointArr: SPoint[][] = [];
    /** path对象  */
    private path = new SPath();

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      墙数据
     */
    constructor(parent: SGraphItem | null, data: Wall) {
        super(parent);
        this.data = data;
        this.zOrder = ItemOrder.wallOrder;
        let tempArr = this.data.OutLine;
        if (tempArr && tempArr.length) {
            this.minX = tempArr[0][0].X;
            this.maxX = this.minX;
            this.minY = -tempArr[0][0].Y;
            this.maxY = this.minY;
            tempArr.forEach((t): void => {
                const temp = t.map(
                    (it): SPoint => {
                        let x = it.X,
                            y = -it.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);
                    }
                );
                this.path.polygon(temp);
            });
        }
    } // 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.wallColor;
        painter.pen.lineWidth = painter.toPx(1);
        painter.brush.color = ItemColor.wallColor;
        painter.drawPath(this.path);
    } // Function onDraw()
} // Class SWallItem
