package examples;

import com.openosrs.client.ModernizedClient;
import com.openosrs.client.api.AgentAPI;
import com.openosrs.client.api.Position;
import com.openosrs.client.api.GameObject;
import com.openosrs.client.scripting.ScriptingFramework;
import com.openosrs.client.plugins.PluginManager;

/**
 * Example Agent - Demonstrates how an AI agent can use the modernized client
 * to interact with RuneScape.
 */
public class ExampleAgent {
    
    public static void main(String[] args) throws Exception {
        System.out.println("=== Example AI Agent for RuneScape ===");
        
        // 1. Initialize the modernized client
        ModernizedClient client = new ModernizedClient();
        client.start().get(); // Wait for startup
        
        // 2. Get the Agent API for game interaction
        AgentAPI api = client.getAgentAPI();
        
        // 3. Basic agent behavior - demonstrate API usage
        demonstrateBasicUsage(api);
        
        // 4. Use the scripting framework
        demonstrateScripting(client.getScriptingFramework());
        
        // 5. Use the plugin system
        demonstratePlugins(client.getPluginManager());
        
        // 6. Keep running for a while then shutdown
        System.out.println("Agent will run for 30 seconds then shutdown...");
        Thread.sleep(30000);
        
        client.stop();
        System.out.println("Agent shutdown complete");
    }
    
    /**
     * Demonstrate basic Agent API usage.
     */
    private static void demonstrateBasicUsage(AgentAPI api) {
        System.out.println("\\n--- Basic Agent API Usage ---");
        
        try {
            // Get player information
            Position playerPos = api.getPlayerPosition();
            System.out.println("Player Position: " + playerPos);
            
            int hitpoints = api.getHitpoints();
            int maxHp = api.getMaxHitpoints();
            System.out.println("Hitpoints: " + hitpoints + "/" + maxHp);
            
            int combatLevel = api.getCombatLevel();
            System.out.println("Combat Level: " + combatLevel);
            
            // Check if in combat
            boolean inCombat = api.isInCombat();
            System.out.println("In Combat: " + inCombat);
            
            // Get inventory info
            boolean inventoryFull = api.isInventoryFull();
            int emptySlots = api.getEmptySlots();
            System.out.println("Inventory Full: " + inventoryFull + ", Empty Slots: " + emptySlots);
            
            // Find nearby objects (example)
            var objects = api.getGameObjects();
            System.out.println("Nearby Objects: " + objects.size());
            
            // Find nearby NPCs
            var npcs = api.getNPCs();
            System.out.println("Nearby NPCs: " + npcs.size());
            
            // Example interaction (if there are objects nearby)
            if (!objects.isEmpty()) {
                GameObject firstObject = objects.get(0);
                System.out.println("Found object: " + firstObject.getName() + " at " + firstObject.getPosition());
                
                // Could interact with it:
                // api.interactWithObject(firstObject, "Examine").get();
            }
            
        } catch (Exception e) {
            System.err.println("Error in basic usage demo: " + e.getMessage());
        }
    }
    
    /**
     * Demonstrate the scripting framework.
     */
    private static void demonstrateScripting(ScriptingFramework scripting) {
        System.out.println("\\n--- Scripting Framework Demo ---");
        
        try {
            // Show available scripts
            System.out.println("Available scripts: woodcutting, combat-training, banking");
            
            // Could start a script:
            // String executionId = scripting.startScript("woodcutting");
            // System.out.println("Started woodcutting script: " + executionId);
            
            // Monitor script status:
            // ScriptStatus status = scripting.getScriptStatus(executionId);
            // System.out.println("Script status: " + status);
            
            // For demo, just show that scripting is available
            boolean enabled = scripting.isEnabled();
            System.out.println("Scripting framework enabled: " + enabled);
            
            var activeScripts = scripting.getActiveScripts();
            System.out.println("Active scripts: " + activeScripts.size());
            
        } catch (Exception e) {
            System.err.println("Error in scripting demo: " + e.getMessage());
        }
    }
    
    /**
     * Demonstrate the plugin system.
     */
    private static void demonstratePlugins(PluginManager plugins) {
        System.out.println("\\n--- Plugin System Demo ---");
        
        try {
            // Show available plugins
            var allPlugins = plugins.getAllPlugins();
            System.out.println("Available plugins: " + allPlugins.size());
            
            for (var entry : allPlugins.entrySet()) {
                String name = entry.getKey();
                var info = entry.getValue();
                System.out.println("  - " + name + ": " + info.getState() + 
                                 " (enabled: " + info.isEnabled() + ")");
            }
            
            // Enable a useful plugin for agents
            if (plugins.getAllPlugins().containsKey("Performance Monitor")) {
                boolean success = plugins.enablePlugin("Performance Monitor");
                System.out.println("Enabled Performance Monitor: " + success);
            }
            
            if (plugins.getAllPlugins().containsKey("Experience Tracker")) {
                boolean success = plugins.enablePlugin("Experience Tracker");
                System.out.println("Enabled Experience Tracker: " + success);
            }
            
            // Show enabled plugins
            var enabled = plugins.getEnabledPlugins();
            System.out.println("Enabled plugins: " + enabled);
            
        } catch (Exception e) {
            System.err.println("Error in plugin demo: " + e.getMessage());
        }
    }
}