package com.openosrs.client.plugins.examples;

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

/**
 * Anti-idle plugin that performs random actions to avoid logout.
 */
public class AntiIdlePlugin extends AbstractPlugin {
    private static final int IDLE_THRESHOLD_MS = 4 * 60 * 1000; // 4 minutes
    private volatile boolean monitoring = false;
    private Thread monitorThread;
    private long lastActionTime = System.currentTimeMillis();
    
    @Override
    protected void enable() {
        logger.info("Anti-idle plugin enabled");
        
        monitoring = true;
        monitorThread = new Thread(this::monitorIdle);
        monitorThread.setDaemon(true);
        monitorThread.start();
        
        // Listen for player actions
        addEventListener("PLAYER_MOVED", this::onPlayerAction);
        addEventListener("PLAYER_INTERACTED", this::onPlayerAction);
    }
    
    @Override
    protected void disable() {
        logger.info("Anti-idle plugin disabled");
        
        monitoring = false;
        if (monitorThread != null) {
            monitorThread.interrupt();
        }
    }
    
    private void monitorIdle() {
        while (monitoring && !Thread.currentThread().isInterrupted()) {
            try {
                long currentTime = System.currentTimeMillis();
                long idleTime = currentTime - lastActionTime;
                
                if (idleTime >= IDLE_THRESHOLD_MS) {
                    performAntiIdleAction();
                    lastActionTime = currentTime;
                }
                
                Thread.sleep(10000); // Check every 10 seconds
                
            } catch (InterruptedException e) {
                break;
            } catch (Exception e) {
                logger.error("Error in idle monitoring", e);
            }
        }
    }
    
    private void onPlayerAction(Object eventData) {
        lastActionTime = System.currentTimeMillis();
    }
    
    private void performAntiIdleAction() {
        try {
            AgentAPI api = getAPI();
            if (api == null) return;
            
            // Perform a random, harmless action
            int action = (int) (Math.random() * 3);
            
            switch (action) {
                case 0:
                    // Random camera movement (would need camera API)
                    logger.debug("Performing anti-idle camera movement");
                    break;
                    
                case 1:
                    // Open and close skills tab (would need interface API)
                    logger.debug("Performing anti-idle interface action");
                    break;
                    
                case 2:
                    // Small movement
                    AgentAPI.Position pos = api.getPlayerPosition();
                    AgentAPI.Position newPos = new AgentAPI.Position(
                        pos.getX() + (Math.random() > 0.5 ? 1 : -1),
                        pos.getY(),
                        pos.getPlane()
                    );
                    api.walkTo(newPos);
                    logger.debug("Performing anti-idle movement");
                    break;
            }
            
        } catch (Exception e) {
            logger.error("Error performing anti-idle action", e);
        }
    }
    
    @Override
    public PluginMetadata getMetadata() {
        return new PluginMetadata(
            "Anti-Idle",
            "Prevents logout by performing random actions",
            "OpenOSRS Agent",
            "1.0",
            new ArrayList<>(),
            Arrays.asList("automation", "idle", "logout")
        );
    }
}