-- // ✦ Made by maingvaldsen ✦ \\ --

local hand = context.hand
local particles = context.particles
local matrices = context.matrices

global.activeEditor = 1;
global.debounces = {};
global.rotate = false;
global.slowMovement = false;
global.nudge = false;

global.itemPosX = 0;
global.itemPosY = 0;
global.itemPosZ = 0;
global.itemRotX = 0;
global.itemRotY = 0;
global.itemRotZ = 0;

global.handPosX = 0;
global.handPosY = 0;
global.handPosZ = 0;
global.handRotX = 0;
global.handRotY = 0;
global.handRotZ = 0;

global.handRelativePosX = 0;
global.handRelativePosY = 0;
global.handRelativePosZ = 0;
global.handRelativeRotX = 0;
global.handRelativeRotY = 0;
global.handRelativeRotZ = 0;

global.particleSpace = "ITEM";
global.particlePosX = 0;
global.particlePosY = 0;
global.particlePosZ = 0;

-- // Keybinds \\ --    fuck respackopts
local editorForwardKey = 77
local editorBackKey
local slowMovementKey = 78
local toggleRotationKey = 82
local switchParticleSpaceKey = 82
local nudgeKey = 67
local positiveXKey = 89
local positiveYKey = 85
local positiveZKey = 73
local negativeXKey = 72
local negativeYKey = 74
local negativeZKey = 75
local resetKey = 88

-- // Misc ig, idk \\ --
local volume = 1
local slowMovementShouldToggle = false
local nudgeShouldToggle = false

-- // How much stuff rotates / moves by at a time \\ --
local moveAmount = slowMovement and 0.001 or 0.01
local rotateAmount = slowMovement and 0.1 or 1
local nudgeMoveAmount = slowMovement and 0.01 or 0.1
local nudgeRotateAmount = slowMovement and 1 or 10



local function slowMove()
    if slowMovementShouldToggle then
        if not debounces["slowMovement"] then
            debounces["slowMovement"] = true
            slowMovement = not slowMovement
            S:playSound("maing_positioner.menu_toggle", volume * 0.3)
        end
    else
        slowMovement = true
    end
end

local function toggleNudge()
    if nudgeShouldToggle then
        if not debounces["toggleNudge"] then
            debounces["toggleNudge"] = true
            nudge = not nudge
            S:playSound("maing_positioner.menu_toggle", volume * 0.3)
        end
    else
        nudge = true
    end
end

local function toggleRotate()
    if not debounces["rotate"] then
        debounces["rotate"] = true
        rotate = not rotate
        S:playSound("maing_positioner.menu_toggle", volume * 0.3)
    end
end

local function translate(position, direction)
    if nudge then
        if not debounces["nudge"] then
            debounces["nudge"] = true
            return position + nudgeMoveAmount * direction
        else
            return position
        end
    else
        return position + moveAmount * direction
    end
end

local function rotateItem(rotation, direction)
    if nudge then
        if not debounces["nudge"] then
            debounces["nudge"] = true
            return ((rotation + nudgeRotateAmount * direction) + 360) % 720 - 360
        else
            return rotation
        end
    else
        return ((rotation + rotateAmount * direction) + 360) % 720 - 360
    end
end

local editors = {
    -- // ITEM EDITOR \\ --
    [1] = function ()
        if KeyBindManager:isKeyPressed(positiveXKey) then
            if rotate then
                itemRotX = rotateItem(itemRotX, 1)
            else
                itemPosX = translate(itemPosX, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveYKey) then
            if rotate then
                itemRotY = rotateItem(itemRotY, 1)
            else
                itemPosY = translate(itemPosY, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveZKey) then
            if rotate then
                itemRotZ = rotateItem(itemRotZ, 1)
            else
                itemPosZ = translate(itemPosZ, 1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeXKey) then
            if rotate then
                itemRotX = rotateItem(itemRotX, -1)
            else
                itemPosX = translate(itemPosX, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeYKey) then
            if rotate then
                itemRotY = rotateItem(itemRotY, -1)
            else
                itemPosY = translate(itemPosY, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeZKey) then
            if rotate then
                itemRotZ = rotateItem(itemRotZ, -1)
            else
                itemPosZ = translate(itemPosZ, -1)
            end
        elseif KeyBindManager:isKeyPressed(resetKey) then
            if rotate then
                itemRotX = 0
                itemRotY = 0
                itemRotZ = 0
            else
                itemPosX = 0
                itemPosY = 0
                itemPosZ = 0
            end
        else
            debounces["nudge"] = nil
        end

        if rotate then
            debugger:out("Editor: Item - Rotation")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Rotation: " .. itemRotX)
            debugger:out("Y Rotation: " .. itemRotY)
            debugger:out("Z Rotation: " .. itemRotZ)
        else
            debugger:out("Editor: Item - Position")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Position: " .. itemPosX)
            debugger:out("Y Position: " .. itemPosY)
            debugger:out("Z Position: " .. itemPosZ)
        end
    end,

    -- // HAND EDITOR \\ --
    [2] = function ()
        if KeyBindManager:isKeyPressed(positiveXKey) then
            if rotate then
                handRotX = rotateItem(handRotX, 1)
            else
                handPosX = translate(handPosX, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveYKey) then
            if rotate then
                handRotY = rotateItem(handRotY, 1)
            else
                handPosY = translate(handPosY, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveZKey) then
            if rotate then
                handRotZ = rotateItem(handRotZ, 1)
            else
                handPosZ = translate(handPosZ, 1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeXKey) then
            if rotate then
                handRotX = rotateItem(handRotX, -1)
            else
                handPosX = translate(handPosX, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeYKey) then
            if rotate then
                handRotY = rotateItem(handRotY, -1)
            else
                handPosY = translate(handPosY, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeZKey) then
            if rotate then
                handRotZ = rotateItem(handRotZ, -1)
            else
                handPosZ = translate(handPosZ, -1)
            end
        elseif KeyBindManager:isKeyPressed(resetKey) then
            if rotate then
                handRotX = 0
                handRotY = 0
                handRotZ = 0
            else
                handPosX = 0
                handPosY = 0
                handPosZ = 0
            end
        else
            debounces["nudge"] = nil
        end

        if rotate then
            debugger:out("Editor: Hand - Rotation")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Rotation: " .. handRotX)
            debugger:out("Y Rotation: " .. handRotY)
            debugger:out("Z Rotation: " .. handRotZ)
        else
            debugger:out("Editor: Hand - Position")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Position: " .. handPosX)
            debugger:out("Y Position: " .. handPosY)
            debugger:out("Z Position: " .. handPosZ)
        end
    end,

    -- // HAND RELATIVE EDITOR \\ --
    [3] = function ()
        if KeyBindManager:isKeyPressed(positiveXKey) then
            if rotate then
                handRelativeRotX = rotateItem(handRelativeRotX, 1)
            else
                handRelativePosX = translate(handRelativePosX, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveYKey) then
            if rotate then
                handRelativeRotY = rotateItem(handRelativeRotY, 1)
            else
                handRelativePosY = translate(handRelativePosY, 1)
            end
        elseif KeyBindManager:isKeyPressed(positiveZKey) then
            if rotate then
                handRelativeRotZ = rotateItem(handRelativeRotZ, 1)
            else
                handRelativePosZ = translate(handRelativePosZ, 1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeXKey) then
            if rotate then
                handRelativeRotX = rotateItem(handRelativeRotX, -1)
            else
                handRelativePosX = translate(handRelativePosX, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeYKey) then
            if rotate then
                handRelativeRotY = rotateItem(handRelativeRotY, -1)
            else
                handRelativePosY = translate(handRelativePosY, -1)
            end
        elseif  KeyBindManager:isKeyPressed(negativeZKey) then
            if rotate then
                handRelativeRotZ = rotateItem(handRelativeRotZ, -1)
            else
                handRelativePosZ = translate(handRelativePosZ, -1)
            end
        elseif KeyBindManager:isKeyPressed(resetKey) then
            if rotate then
                handRelativeRotX = 0
                handRelativeRotY = 0
                handRelativeRotZ = 0
            else
                handRelativePosX = 0
                handRelativePosY = 0
                handRelativePosZ = 0
            end
        else
            debounces["nudge"] = nil
        end

        if rotate then
            debugger:out("Editor: Hand Relative - Rotation")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Rotation: " .. handRelativeRotX)
            debugger:out("Y Rotation: " .. handRelativeRotY)
            debugger:out("Z Rotation: " .. handRelativeRotZ)
        else
            debugger:out("Editor: Hand Relative - Position")
            debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
            debugger:out("X Position: " .. handRelativePosX)
            debugger:out("Y Position: " .. handRelativePosY)
            debugger:out("Z Position: " .. handRelativePosZ)
        end
    end,

    -- // PARTICLE EDITOR \\ --
    [4] = function ()
        if KeyBindManager:isKeyPressed(positiveXKey) then
            particlePosX = translate(particlePosX, 1)
        elseif KeyBindManager:isKeyPressed(positiveYKey) then
            particlePosY = translate(particlePosY, 1)
        elseif KeyBindManager:isKeyPressed(positiveZKey) then
            particlePosZ = translate(particlePosZ, 1)
        elseif  KeyBindManager:isKeyPressed(negativeXKey) then
            particlePosX = translate(particlePosX, -1)
        elseif  KeyBindManager:isKeyPressed(negativeYKey) then
            particlePosY = translate(particlePosY, -1)
        elseif  KeyBindManager:isKeyPressed(negativeZKey) then
            particlePosZ = translate(particlePosZ, -1)
        elseif KeyBindManager:isKeyPressed(resetKey) then
            particlePosX = 0
            particlePosY = 0
            particlePosZ = 0
        else
            debounces["nudge"] = nil
        end

        if KeyBindManager:isKeyPressed(switchParticleSpaceKey) then
            if not debounces["particleSpace"] then
                debounces["particleSpace"] = true
                S:playSound("maing_positioner.menu_toggle", volume * 0.3)

                if particleSpace == "ITEM" then
                    particleSpace = "SCREEN"
                elseif particleSpace == "SCREEN" then
                    particleSpace = "ITEM"
                end
            end
        else
            debounces["particleSpace"] = nil
        end

        particleManager:addParticle(particles,
            false,
            particlePosX, --posX
            particlePosY, --posY
            particlePosZ, --posZ
            0, --deltaPosX
            0, --deltaPosY
            0, --deltaPosZ
            0, --rotX
            0, --rotY
            0, --rotZ
            0, --deltaRotX
            0, --deltaRotY
            0, --deltaRotZ
            0.2, --size
            Texture:of("minecraft", "holdmyitems/particles/particle_positioner_x.png"), particleSpace, hand, "SPAWN", "CUTOUT_L", 0, 255
        )

        debugger:out("Editor: Particle - " .. particleSpace)
        debugger:out("Slow Movement: " .. tostring(slowMovement) .. "     Nudge: " .. tostring(nudge))
        debugger:out("X Position: " .. particlePosX)
        debugger:out("Y Position: " .. particlePosY)
        debugger:out("Z Position: " .. particlePosZ)
    end,
}

local function switchEditor(direction)
    if not debounces["editor"] then
        debounces["editor"] = true
        activeEditor = (activeEditor - 1 + direction) % #editors + 1
        S:playSound("maing_positioner.menu_switch", volume)
    end
end



if KeyBindManager:isKeyPressed(editorForwardKey) then
    switchEditor(1)
elseif KeyBindManager:isKeyPressed(editorBackKey) then
    switchEditor(-1)
elseif KeyBindManager:isKeyPressed(toggleRotationKey) then
    if activeEditor ~= 4 then
        toggleRotate()
    end
elseif KeyBindManager:isKeyPressed(nudgeKey) then
    toggleNudge()
elseif KeyBindManager:isKeyPressed(slowMovementKey) then
    slowMove()
else
    debounces["editor"] = nil
    debounces["rotate"] = nil
    if slowMovementShouldToggle then
        debounces["slowMovement"] = nil
    else
        slowMovement = false
    end
    if nudgeShouldToggle then
        debounces["toggleNudge"] = nil
    else
        nudge = false
    end
end

editors[activeEditor]()

M:translate(matrices, itemPosX, itemPosY, itemPosZ)
M:rotateX(matrices, itemRotX)
M:rotateY(matrices, itemRotY)
M:rotateZ(matrices, itemRotZ)