package net.lakksam.clone.gui;

import java.util.ArrayList;

public abstract class MultiChildComponent extends Component implements ISizeSumProvider {
    private ArrayList<Component> children = new ArrayList<>();

    public MultiChildComponent(Dimensions dimensions) {
        super(dimensions);
    }

    protected boolean addChild(Component child) {
        child.setParent(this);
        return children.add(child);
    }

    protected boolean addChild(Component child, int x, int y) {
        child.setX(getX() + x);
        child.setY(getY() + y);
        child.setParent(this);
        return children.add(child);
    }

    protected boolean removeChild(Component child) {
        return children.remove(child);
    }

    protected Component removeChild(int index) {
        return children.remove(index);
    }

    protected boolean hasChild(Component child) {
        return children.contains(child);
    }

    protected ArrayList<Component> getChildren() {
        return children;
    }

    public int getHeightSum() {
        int sum = 0;
        for (int i = 0; i < getChildren().size()-1; i++){
            Component child = getChildren().get(i);
            sum += child.getHeight();
        }
        return sum;
    }

    public int getWidthSum() {
        int sum = 0;
        for (int i = 0; i < getChildren().size()-1; i++){
            Component child = getChildren().get(i);
            sum += child.getWidth();
        }
        return sum;
    }
}
