// SMART CHEAT SCRIPT
// No refresh needed! Just paste and wait 2 seconds.

(function () {
    console.clear();
    console.log("%c Magic Garden Teleporter Loaded ", "background: #222; color: #bada55; font-size: 20px");
    console.log("Waiting for game heartbeat to capture socket...");

    window.gameSocket = null;
    const OriginalSend = WebSocket.prototype.send;

    // Hook 'send' to catch the existing socket instance!
    WebSocket.prototype.send = function (data) {
        // Capture if we don't have one, or if the current one is closed/closing
        if (!window.gameSocket || window.gameSocket.readyState >= 2) {
            window.gameSocket = this;
            console.log("%c Socket Captured/Reconnected! ", "color: green; font-weight: bold; font-size: 16px");
            console.log("👉 Try: teleport(15, 15)");

            // Detect when this specific socket closes
            this.addEventListener('close', () => {
                console.log("%c Socket Disconnected! Waiting for game to reconnect... ", "color: red; font-weight: bold; font-size: 16px");
            });
        }
        return OriginalSend.apply(this, arguments);
    };

    // --- COMMANDS ---

    window.teleport = function (x, y) {
        if (!window.gameSocket) {
            console.error("Socket not captured yet! Wait for a 'Ping' (takes ~2 seconds).");
            return;
        }

        const msg = {
            type: 'Teleport',
            position: { x: x, y: y },
            scopePath: ["Room", "Quinoa"] // Hardcoded based on your logs
        };

        console.log("Teleporting to:", x, y);
        window.gameSocket.send(JSON.stringify(msg));
    };

    window.plant = function (slot, species = 'Tomato') {
        if (!window.gameSocket) return;
        const msg = {
            type: 'PlantSeed',
            slot: parseInt(slot),
            species: species,
            scopePath: ["Room", "Quinoa"]
        };
        window.gameSocket.send(JSON.stringify(msg));
    };

    window.harvest = async function (startSlot, endSlot, count = 1, delayMs = 20) {
        if (!window.gameSocket) return;

        const start = parseInt(startSlot);
        const end = endSlot ? parseInt(endSlot) : start;
        const iterations = count;
        const delay = delayMs;

        console.log(`Harvesting slots ${start} to ${end}, ${iterations} times each with ${delay}ms delay.`);

        let sentCount = 0;
        for (let slot = start; slot <= end; slot++) {
            for (let i = 0; i < iterations; i++) {
                const msg = {
                    type: 'HarvestCrop',
                    slot: slot,
                    slotsIndex: i, // Iterate through slot indexes
                    scopePath: ["Room", "Quinoa"]
                };
                window.gameSocket.send(JSON.stringify(msg));
                sentCount++;
                // Small delay to prevent flood disconnects
                await new Promise(r => setTimeout(r, delay));
            }
        }
        console.log(`Finished sending ${sentCount} harvest messages.`);
    };

    window.sell = function () {
        if (!window.gameSocket) return;
        const msg = {
            type: 'SellAllCrops',
            scopePath: ["Room", "Quinoa"]
        };
        window.gameSocket.send(JSON.stringify(msg));
        console.log("Selling all crops!");
    };

})();
