package leigh.minecraft.link;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;

/**
 * This class handles powered flight mechanics for New Eden.
 *
 * @author Alex Leigh
 */
public class Flight {
    // This depends on the player objects from Spigot always being the same
    private static final int FLIGHT_TIME_SECS = 30;
    private final HashMap<Player, Instant> fuel = new HashMap<>();
    private final ItemStack fuelUnit = new ItemStack(Material.POPPED_CHORUS_FRUIT, 1);

    public void tick() {
        for (Player p : Bukkit.getServer().getOnlinePlayers()) {
            // If the player is not survival, then we don't care about them at all.
            if (p.getGameMode() == GameMode.SURVIVAL) {
                // Is never null
                ItemStack offHand = p.getInventory().getItemInOffHand();
                if (offHand.getType() == Material.ELYTRA) {
                    if(!p.getAllowFlight()) {
                        enableFlight(p);
                    } else {
                        if(!consumeFuelIfNeeded(p)) {
                            disableFlight(p);
                        }
                    }
                } else {
                    if(p.getAllowFlight()) {
                        disableFlight(p);
                    }
                }
            }
        }
    }

    private void disableFlight(Player p) {
        if(p.getAllowFlight()) {
            p.setAllowFlight(false);
            p.sendMessage("Elytra disengaged.");
        }
    }

    /**
     * Initially enable flying for the user. For a zero-endurance user, this will consume 1 unit of fuel and reset
     * the endurance time.
     */
    private void enableFlight(Player p) {
        if(consumeFuelIfNeeded(p)) {
            p.setAllowFlight(true);
            p.sendMessage("Elytra engaged. [fuel: " + FLIGHT_TIME_SECS + " seconds per 1 chorus fruit.]");
        } else {
            p.sendMessage("Unable to engage Elytra. No fuel available.");
        }
    }

    private boolean hasFuelAvailable(Player p) {
        for(ItemStack stack : p.getInventory()) {
            if(fuelUnit.isSimilar(stack)) return true;
        }
        return false;
    }

    /**
     * Search the players inventory for a fuel item, and consume 1 if found. Returns <code>true</code> if a fuel
     * item was successfully consumed.
     */
    private boolean consumeFuelIfNeeded(Player p) {
        Instant endurance = fuel.get(p);
        if(endurance!=null && endurance.isAfter(Instant.now())) return true;

        if(hasFuelAvailable(p)) {
            p.getInventory().removeItem(fuelUnit);
            fuel.put(p, Instant.now().plus(60, ChronoUnit.SECONDS));
            p.sendMessage("1 unit of chorus fuel consumed by Elytra.");
            return true;
        } else {
            return false;
        }
    }
}
