package dev.bytevibe.hyperpoint;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.Serializable;

/**
 * 页面内容容器，用于保存页面上的所有可绘制对象
 */
public class PageContent implements Serializable {
    private ObservableList<DrawableObject> drawableObjects;

    public PageContent() {
        this.drawableObjects = FXCollections.observableArrayList();
    }

    public ObservableList<DrawableObject> getDrawableObjects() {
        return drawableObjects;
    }

    /**
     * 添加可绘制对象
     */
    public void addObject(DrawableObject object) {
        if (object != null) {
            drawableObjects.add(object);
        }
    }

    /**
     * 移除可绘制对象
     */
    public void removeObject(DrawableObject object) {
        drawableObjects.remove(object);
    }

    /**
     * 查找包含指定点的对象（从后到前，越后面的优先级越高）
     */
    public DrawableObject findObjectAt(double x, double y) {
        for (int i = drawableObjects.size() - 1; i >= 0; i--) {
            DrawableObject obj = drawableObjects.get(i);
            if (obj.contains(x, y)) {
                return obj;
            }
        }
        return null;
    }

    /**
     * 获取对象列表大小
     */
    public int size() {
        return drawableObjects.size();
    }

    /**
     * 清空所有对象
     */
    public void clear() {
        drawableObjects.clear();
    }
}

