package com.gregtechceu.gtceu.api.machine.trait.notifiable;

import com.gregtechceu.gtceu.api.machine.trait.recipe.RecipeLogic;

public class RecipeAmperageEnergyContainer extends NotifiableEnergyContainer {

    public RecipeAmperageEnergyContainer(long maxCapacity, long maxInputVoltage, long maxInputAmperage,
                                         long maxOutputVoltage, long maxOutputAmperage) {
        super(maxCapacity, maxInputVoltage, maxInputAmperage, maxOutputVoltage, maxOutputAmperage);
    }

    public static RecipeAmperageEnergyContainer emitterContainer(long maxCapacity,
                                                                 long maxOutputVoltage, long maxOutputAmperage) {
        return new RecipeAmperageEnergyContainer(maxCapacity, 0L, 0L, maxOutputVoltage, maxOutputAmperage);
    }

    public static RecipeAmperageEnergyContainer receiverContainer(long maxCapacity,
                                                                  long maxInputVoltage, long maxInputAmperage) {
        return new RecipeAmperageEnergyContainer(maxCapacity, maxInputVoltage, maxInputAmperage, 0L, 0L);
    }

    @Override
    public long getInputAmperage() {
        var recipeLogic = getMachine().getTrait(RecipeLogic.class);
        if (recipeLogic == null) return 1L;
        var lastRecipe = recipeLogic.getLastRecipe();
        long amperage;
        if (lastRecipe != null) {
            amperage = lastRecipe.getInputEUt().amperage();
        } else {
            amperage = super.getInputAmperage();
        }
        if (getEnergyCapacity() / 2 > getEnergyStored() && recipeLogic.isActive()) {
            return amperage + 1;
        } else {
            return amperage;
        }
    }

    @Override
    public long getOutputAmperage() {
        var recipeLogic = getMachine().getTrait(RecipeLogic.class);
        if (recipeLogic == null) return 1L;
        var lastRecipe = recipeLogic.getLastRecipe();
        if (lastRecipe != null) {
            return lastRecipe.getOutputEUt().amperage();
        } else {
            return super.getOutputAmperage();
        }
    }
}
