package uebung05.logistics;

import uebung05.logistics.quantities.FloatUnit;
import uebung05.logistics.quantities.IntUnit;
import uebung05.logistics.quantities.NeedCollector;
import uebung05.logistics.quantities.Unit;

import java.util.ArrayList;
import java.util.List;

/**
 * The Manager class manages a list of Vehicle objects and provides methods for adding, filling up, and logging ticks for the vehicles.
 * It also includes methods for collecting and displaying the overall needs of the vehicles and the needs for specific IntUnit or FloatUnit objects.
 *
 * @author Nikolaus Köberlein
 * @version 1.0
 */
public class Manager {

    private List<Vehicle> vehicles;

    public Manager() {
        this.vehicles = new ArrayList<>();
    }

    /**
     * Add a Vehicle object to the list of vehicles
     *
     * @param vehicle The Vehicle object to be added
     */
    public void addVehicle(Vehicle vehicle) {
        vehicles.add(vehicle);
        System.out.println("Boris bought a new " + vehicle.getClass().getSimpleName() + " " + vehicle + ".");
        System.out.println();
    }

    /**
     * Fill up all the vehicles in the list
     */
    public void fillUpVehicles() {
        for (Vehicle vehicle : vehicles) {
            vehicle.fillUpAll();
        }
        System.out.println("All vehicles filled and ready for combat.");
        System.out.println();
    }

    /**
     * Log a tick for all the vehicles in the list, consuming their supplies based on the intensity rate
     *
     * @param intensityRate The intensity rate for the tick
     */
    public void logTick(int intensityRate) {
        for (Vehicle vehicle : vehicles) {
            vehicle.consumeAll(intensityRate);
        }
        System.out.println("All vehicles consumed because of " + intensityRate + " battle(s).");
        System.out.println();
    }

    /**
     * Collect the needs of all the vehicles in the list and return a NeedCollector object
     *
     * @return The NeedCollector object containing the needs of all the vehicles
     */
    public NeedCollector collectNeeds() {
        NeedCollector collector = new NeedCollector();
        for (Vehicle vehicle : vehicles) {
            vehicle.reportNeeds(collector);
        }
        return collector;
    }

    /**
     * Display the overall needs of all the vehicles in the list
     */
    public void showOverallNeed() {
        NeedCollector collector = collectNeeds();
        collector.show();
    }

    /**
     * Display the vehicles needing a specific IntUnit object
     *
     * @param unit The IntUnit object for which to display the vehicles
     */
    public void showNeed(IntUnit unit) {
        /*if (unit instanceof IntUnit) {
            IntUnit intUnit = (IntUnit) unit;
        } else if (unit instanceof FloatUnit) {
            FloatUnit floatUnit = (FloatUnit) unit;
        }*/
        System.out.println("Vehicles needing " + unit.toString() + ":");
        for (Vehicle vehicle : vehicles) {
            if (vehicle.need(unit)) {
                NeedCollector collector = new NeedCollector();
                vehicle.reportNeeds(collector);
                System.out.println("- " + vehicle + " needs " + collector.getNeed(unit) + " " + unit);
            }
        }
        System.out.println();
    }

    /**
     * Display the vehicles needing a specific FloatUnit object
     *
     * @param unit The FloatUnit object for which to display the vehicles
     */
    public void showNeed(FloatUnit unit) {
        System.out.println("Vehicles needing " + unit.toString() + ":");
        for (Vehicle vehicle : vehicles) {
            if (vehicle.need(unit)) {
                NeedCollector collector = new NeedCollector();
                vehicle.reportNeeds(collector);
                System.out.println("- " + vehicle + " needs " + collector.getNeed(unit) + " " + unit);
            }
        }
        System.out.println();
    }
}