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

/**
 * 柱子item
 *
 * @author  郝建龙
 */
export class SColumnItem extends SGraphItem {
    /** 柱子数据    */
    data: Column;
    /** 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[][] = [];

    /**
     * 构造函数
     *
     * @param parent    指向父对象
     * @param data      柱子数据
     */
    constructor(parent: SGraphItem | null, data: Column) {
        super(parent);
        this.data = data;
        this.zOrder = ItemOrder.columnOrder;
        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;
            this.pointArr = tempArr.map((t): SPoint[] => {
                return 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);
                    }
                );
            });
        }
    } // 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.columnColor;
        painter.pen.lineWidth = painter.toPx(1);
        painter.brush.color = ItemColor.columnColor;
        this.pointArr.forEach((t): void => {
            painter.drawPolygon(t);
        });
    } // Function onDraw()
} // Class SColumnItem
