package com.openosrs.client.core;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * InterfaceState - Manages game interface state and interactions.
 *
 * Tracks:
 * - Currently open interfaces
 * - Interface hierarchy
 * - Widget states
 */
public class InterfaceState {
    private static final Logger logger = LoggerFactory.getLogger(InterfaceState.class);

    private final EventSystem eventSystem;
    private final AtomicInteger currentInterface = new AtomicInteger(-1);
    private final AtomicReference<String> chatboxText = new AtomicReference<>("");

    public InterfaceState(EventSystem eventSystem) {
        this.eventSystem = eventSystem;
    }

    public void initialize() {
        logger.debug("InterfaceState initialized");
    }

    public void shutdown() {
        logger.debug("InterfaceState shutdown");
    }

    public void tick() {
        // Interface state updates would happen here
        // For now, this is a stub
    }

    /**
     * Update the currently open interface.
     */
    public void setCurrentInterface(int interfaceId) {
        int oldInterface = currentInterface.getAndSet(interfaceId);
        if (oldInterface != interfaceId) {
            logger.debug("Interface changed: {} -> {}", oldInterface, interfaceId);
            eventSystem.fireInterfaceChanged(oldInterface, interfaceId);
        }
    }

    /**
     * Get the currently open interface ID.
     */
    public int getCurrentInterface() {
        return currentInterface.get();
    }

    /**
     * Check if a specific interface is open.
     */
    public boolean isInterfaceOpen(int interfaceId) {
        return currentInterface.get() == interfaceId;
    }

    /**
     * Check if any interface is open.
     */
    public boolean isAnyInterfaceOpen() {
        return currentInterface.get() != -1;
    }

    /**
     * Close the current interface.
     */
    public void closeInterface() {
        setCurrentInterface(-1);
    }

    public void setChatboxText(String text) {
        chatboxText.set(text != null ? text : "");
    }

    public String getChatboxText() {
        return chatboxText.get();
    }
}
