package com.openosrs.client.plugins.examples;

import com.openosrs.client.plugins.*;
import com.openosrs.client.api.*;
import com.openosrs.client.api.types.Skill;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Experience tracker plugin that monitors XP gains.
 */
public class ExperienceTrackerPlugin extends AbstractPlugin {
    private final Map<Skill, Long> baseExperience = new ConcurrentHashMap<>();
    private final Map<Skill, Long> sessionGains = new ConcurrentHashMap<>();
    private final Map<Skill, Long> lastXpTime = new ConcurrentHashMap<>();
    private volatile boolean tracking = false;
    private Thread trackerThread;
    
    @Override
    protected void enable() {
        logger.info("Experience tracker plugin enabled");
        
        // Initialize base experience levels
        AgentAPI api = getAPI();
        if (api != null) {
            for (Skill skill : Skill.values()) {
                int xp = api.getSkillExperience(skill);
                baseExperience.put(skill, (long) xp);
                sessionGains.put(skill, 0L);
            }
        }
        
        tracking = true;
        trackerThread = new Thread(this::trackExperience);
        trackerThread.setDaemon(true);
        trackerThread.start();
    }
    
    @Override
    protected void disable() {
        logger.info("Experience tracker plugin disabled");
        
        tracking = false;
        if (trackerThread != null) {
            trackerThread.interrupt();
        }
        
        logSessionReport();
    }
    
    private void trackExperience() {
        while (tracking && !Thread.currentThread().isInterrupted()) {
            try {
                updateExperienceGains();
                Thread.sleep(1000); // Check every second
                
            } catch (InterruptedException e) {
                break;
            } catch (Exception e) {
                logger.error("Error tracking experience", e);
            }
        }
    }
    
    private void updateExperienceGains() {
        AgentAPI api = getAPI();
        if (api == null) return;
        
        long currentTime = System.currentTimeMillis();
        
        for (Skill skill : Skill.values()) {
            int currentXp = api.getSkillExperience(skill);
            long baseXp = baseExperience.get(skill);
            long newGain = currentXp - baseXp;
            
            if (newGain > sessionGains.get(skill)) {
                long xpGained = newGain - sessionGains.get(skill);
                sessionGains.put(skill, newGain);
                lastXpTime.put(skill, currentTime);
                
                logger.info("XP gained in {}: {} (Total session: {})", 
                    skill.name(), xpGained, newGain);
            }
        }
    }
    
    private void logSessionReport() {
        logger.info("=== Experience Session Report ===");
        
        for (Skill skill : Skill.values()) {
            long gains = sessionGains.get(skill);
            if (gains > 0) {
                logger.info("{}: {} XP gained", skill.name(), gains);
            }
        }
    }
    
    public Map<Skill, Long> getSessionGains() {
        return new ConcurrentHashMap<>(sessionGains);
    }
    
    public void resetSession() {
        AgentAPI api = getAPI();
        if (api != null) {
            for (Skill skill : Skill.values()) {
                int xp = api.getSkillExperience(skill);
                baseExperience.put(skill, (long) xp);
                sessionGains.put(skill, 0L);
            }
        }
        
        logger.info("Experience session reset");
    }
    
    @Override
    public PluginMetadata getMetadata() {
        return new PluginMetadata(
            "Experience Tracker",
            "Tracks experience gains during play sessions",
            "OpenOSRS Agent",
            "1.0",
            new ArrayList<>(),
            Arrays.asList("tracking", "experience", "skills")
        );
    }
}