package com.openosrs.client.scripting.examples;

import com.openosrs.client.scripting.*;
import com.openosrs.client.api.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Banking script utility.
 */
public class BankingScript extends AbstractScript {
    private final List<Integer> itemsToDeposit;
    private final List<Integer> itemsToWithdraw;
    private final Map<Integer, Integer> withdrawQuantities;
    
    public BankingScript(List<Integer> itemsToDeposit, Map<Integer, Integer> itemsToWithdraw) {
        this.itemsToDeposit = itemsToDeposit;
        this.itemsToWithdraw = itemsToWithdraw.keySet().stream().collect(Collectors.toList());
        this.withdrawQuantities = itemsToWithdraw;
    }
    
    @Override
    protected void run(ScriptContext context) throws Exception {
        AgentAPI api = context.getAPI();
        
        logger.info("Starting banking script");
        
        // Find a bank
        GameObject bank = findNearestBank(api);
        if (bank == null) {
            logger.error("No bank found");
            return;
        }
        
        // Walk to bank
        Position playerPos = api.getPlayerPosition();
        if (bank.distanceToPlayer(playerPos) > 2.0) {
            logger.debug("Walking to bank");
            api.walkTo(bank.getPosition()).get();
            sleep(2000);
        }
        
        // Open bank
        logger.debug("Opening bank");
        api.interactWithObject(bank, "Bank").get();
        sleep(2000);
        
        // Deposit items
        for (int itemId : itemsToDeposit) {
            if (api.hasItem(itemId)) {
                logger.debug("Depositing item: {}", itemId);
                // Bank interface interaction would go here
                sleep(600);
            }
        }
        
        // Withdraw items
        for (int itemId : itemsToWithdraw) {
            int quantity = withdrawQuantities.getOrDefault(itemId, 1);
            logger.debug("Withdrawing {} x {}", quantity, itemId);
            // Bank interface interaction would go here
            sleep(600);
        }
        
        // Close bank
        sleep(1000);
        
        logger.info("Banking completed");
    }
    
    private GameObject findNearestBank(AgentAPI api) {
        // Common bank object IDs
        int[] bankIds = {10060, 2213, 11758, 14367, 10517};
        
        for (int bankId : bankIds) {
            GameObject bank = api.getClosestGameObject(bankId);
            if (bank != null) {
                return bank;
            }
        }
        
        return null;
    }
    
    @Override
    public ScriptMetadata getMetadata() {
        return new ScriptMetadata(
            "Banking Utility",
            "Deposits and withdraws items from bank",
            "OpenOSRS Agent",
            "1.0",
            Arrays.asList("Banking", "Utility", "Helper")
        );
    }
}