/*
 *  Logic-Builder.
 *  Copyright (C) 2016 Jesse Prescott (BleedObsidian)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gmail.bleedobsidian.logicbuilder.screens.builder.managers;

import com.badlogic.gdx.assets.loaders.TextureLoader;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.Component;
import com.gmail.bleedobsidian.logicbuilder.screens.builder.managers.components.Component.ComponentIO;
import com.gmail.bleedobsidian.logicbuilder.utils.GameInterface;
import com.gmail.bleedobsidian.logicbuilder.utils.ResourceManager;
import com.gmail.bleedobsidian.logicbuilder.utils.ResourceManager.ResourceManagerInterface;
import java.util.ArrayList;
import java.util.List;

/**
 * This manager controls all placed components.
 * 
 * @author Jesse Prescott (BleedObsidian)
 */
public class ComponentManager implements GameInterface, ResourceManagerInterface {
    /**
     * The list of all placed components.
     */
    private final ArrayList<Component> components = new ArrayList<Component>();
    
    /**
     * List of components waiting to be removed.
     */
    private final List<Component> componentsToRemove = new ArrayList<Component>();
    
    @Override
    public void resume() {
    }

    @Override
    public void update(float delta) {
        this.components.removeAll(this.componentsToRemove);
        this.componentsToRemove.clear();
        
        for (Component component: this.components) {
            component.update(delta);
            
            for(ComponentIO componentIO : component.getInputs()) {
                componentIO.update(delta);
            }
            
            for(ComponentIO componentIO : component.getOutputs()) {
                componentIO.update(delta);
            }
        }
    }

    @Override
    public void render(SpriteBatch spriteBatch) {
        for (Component component: this.components) {
            component.render(spriteBatch);
            
            for(ComponentIO componentIO : component.getInputs()) {
                componentIO.render(spriteBatch);
            }
            
            for(ComponentIO componentIO : component.getOutputs()) {
                componentIO.render(spriteBatch);
            }
        }
    }

    @Override
    public void pause() {
    }
    
    /**
     * Add new component.
     * 
     * @param component Component.
     */
    public void addComponent(Component component) {
        this.components.add(component);
    }
    
    /**
     * Remove component.
     * 
     * @param component Component.
     */
    public void removeComponent(Component component) {
        this.componentsToRemove.add(component);
    }
    
    /**
     * If gridX/gridY is intersecting with another component.
     * 
     * @param gridX GridX.
     * @param gridY GridX.
     * @return If intersecting.
     */
    public boolean isIntersecting(int gridX, int gridY) {
        for (Component component: this.components) {
            if(component.isIntersecting(gridX, gridY)) {
                return true;
            }
        }
        
        return false;
    }

    @Override
    public void loadResources(ResourceManager resourceManager) {
        TextureLoader.TextureParameter filter = new TextureLoader.TextureParameter();
        filter.magFilter = Texture.TextureFilter.Linear;
        filter.minFilter = Texture.TextureFilter.Linear;
        resourceManager.loadTexture("Textures/IO-Bounding-Box.png", filter);
    }

    @Override
    public void onResourcesLoaded(ResourceManager resourceManager) {
    }
}
