package com.openosrs.client.plugins.examples;

import com.openosrs.client.plugins.*;
import com.openosrs.client.api.*;
import java.util.Arrays;
import java.util.ArrayList;

/**
 * Auto-healing plugin that automatically eats food when health is low.
 */
public class AutoHealPlugin extends AbstractPlugin {
    private static final int DEFAULT_HEAL_THRESHOLD = 50;
    private static final int[] FOOD_IDS = {373, 379, 385, 391}; // Lobster, Shark, etc.
    
    private int healThreshold = DEFAULT_HEAL_THRESHOLD;
    private volatile boolean monitoring = false;
    private Thread monitorThread;
    
    @Override
    protected void enable() {
        logger.info("Auto-heal plugin enabled with threshold: {}", healThreshold);
        
        monitoring = true;
        monitorThread = new Thread(this::monitorHealth);
        monitorThread.setDaemon(true);
        monitorThread.start();
        
        // Listen for combat events
        addEventListener("PLAYER_TOOK_DAMAGE", this::onPlayerDamaged);
    }
    
    @Override
    protected void disable() {
        logger.info("Auto-heal plugin disabled");
        
        monitoring = false;
        if (monitorThread != null) {
            monitorThread.interrupt();
        }
    }
    
    private void monitorHealth() {
        while (monitoring && !Thread.currentThread().isInterrupted()) {
            try {
                AgentAPI api = getAPI();
                if (api != null) {
                    int currentHp = api.getHitpoints();
                    int maxHp = api.getMaxHitpoints();
                    
                    if (currentHp <= healThreshold && currentHp < maxHp) {
                        tryHeal(api);
                    }
                }
                
                Thread.sleep(1000); // Check every second
                
            } catch (InterruptedException e) {
                break;
            } catch (Exception e) {
                logger.error("Error in health monitoring", e);
            }
        }
    }
    
    private void onPlayerDamaged(Object eventData) {
        // Immediate heal check when damaged
        try {
            AgentAPI api = getAPI();
            if (api != null && api.getHitpoints() <= healThreshold) {
                tryHeal(api);
            }
        } catch (Exception e) {
            logger.error("Error handling damage event", e);
        }
    }
    
    private void tryHeal(AgentAPI api) {
        try {
            for (int foodId : FOOD_IDS) {
                int slot = api.findItemSlot(foodId);
                if (slot != -1) {
                    logger.debug("Eating food at slot {}", slot);
                    api.useItem(slot);
                    Thread.sleep(1800); // Food delay
                    return;
                }
            }
            
            logger.warn("No food available for healing");
            
        } catch (Exception e) {
            logger.error("Error trying to heal", e);
        }
    }
    
    public void setHealThreshold(int threshold) {
        this.healThreshold = Math.max(1, Math.min(99, threshold));
        logger.info("Heal threshold set to: {}", this.healThreshold);
    }
    
    @Override
    public PluginMetadata getMetadata() {
        return new PluginMetadata(
            "Auto-Heal",
            "Automatically eats food when health is low",
            "OpenOSRS Agent",
            "1.0",
            new ArrayList<>(),
            Arrays.asList("healing", "automation", "combat")
        );
    }
}