local DragAndDrop = require("InventoryTetris/System/DragAndDrop")

-- Tracks the current drag and drop operation for controller players.
local ControllerDragAndDrop = {}

ControllerDragAndDrop.dragging = {}
ControllerDragAndDrop.draggingTetris = {}
ControllerDragAndDrop.dragOwner = {}
ControllerDragAndDrop.rotateDrag = {}

ControllerDragAndDrop.ownersForCancel = {}

function ControllerDragAndDrop.isDragging(playerNum)
    return ControllerDragAndDrop.dragging[playerNum] ~= nil
end

function ControllerDragAndDrop.isDragOwner(playerNum, testOwner)
    return ControllerDragAndDrop.dragOwner[playerNum] == testOwner
end

function ControllerDragAndDrop.isDraggedItemRotated(playerNum)
    return ControllerDragAndDrop.rotateDrag[playerNum]
end

function ControllerDragAndDrop.rotateDraggedItem(playerNum)
    ControllerDragAndDrop.rotateDrag[playerNum] = not ControllerDragAndDrop.rotateDrag[playerNum]
    if REHUD_cfg and REHUD_cfg.uiClickSounds then
        if playOgg then playOgg("Click") end
    end
end


function ControllerDragAndDrop.getDraggedStack(playerNum)
    return ControllerDragAndDrop.dragging[playerNum]
end

function ControllerDragAndDrop.getDraggedTetrisStack(playerNum)
    return ControllerDragAndDrop.draggingTetris[playerNum]
end

function ControllerDragAndDrop.getDraggedItem(playerNum)
    local stack = ControllerDragAndDrop.dragging[playerNum]
    return DragAndDrop.convertItemStackToItem(stack)
end

function ControllerDragAndDrop.startDrag(playerNum, owner, tetrisStack, vanillaStack)
    ControllerDragAndDrop.dragging[playerNum] = vanillaStack
    ControllerDragAndDrop.draggingTetris[playerNum] = tetrisStack
    ControllerDragAndDrop.dragOwner[playerNum] = owner
    ControllerDragAndDrop.rotateDrag[playerNum] = vanillaStack[1] and vanillaStack[1].isRotated or vanillaStack.isRotated
    if REHUD_cfg and REHUD_cfg.uiClickSounds and vanillaStack and #vanillaStack > 0 then
        if playOgg then playOgg("Item") end
    end
end

function ControllerDragAndDrop.endDrag(playerNum)
    if REHUD_cfg and REHUD_cfg.uiClickSounds and ControllerDragAndDrop.dragging[playerNum] then
        if playOgg then playOgg("Item") end
    end

    ControllerDragAndDrop.dragging[playerNum] = nil
    ControllerDragAndDrop.draggingTetris[playerNum] = nil
    ControllerDragAndDrop.dragOwner[playerNum] = nil
    ControllerDragAndDrop.rotateDrag[playerNum] = nil
    ControllerDragAndDrop.ownersForCancel = {}
end

return ControllerDragAndDrop
