local _,ns = ...
local L = ns.L

function ns.widgets:CreateCustomSlider(parent, width, height, minValue, maxValue, step, initialValue, onValueChanged, titleText)
    minValue = minValue or 1
    maxValue = maxValue or 100
    step = step or 1
    initialValue = initialValue or ((minValue + maxValue) / 2)
    
    local slider = CreateFrame("Frame", nil, parent)
    slider:SetSize(width or 300, height or 30)
    
    -- Título encima
    slider.title = slider:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    slider.title:SetPoint("BOTTOM", slider, "TOP", 0, 4)
    slider.title:SetText(titleText or "")
    
    -- Barra de fondo (línea horizontal)
    slider.bar = slider:CreateTexture(nil, "BACKGROUND")
    slider.bar:SetColorTexture(0.2, 0.2, 0.2, 1)
    slider.bar:SetPoint("TOPLEFT", 0, - height / 2 + 2)
    slider.bar:SetSize(slider:GetWidth(), 4)
    
    -- Marcador (handle)
    slider.handle = CreateFrame("Frame", nil, slider)
    slider.handle:SetSize(12, 24)
    slider.handle.texture = slider.handle:CreateTexture(nil, "ARTWORK")
    slider.handle.texture:SetColorTexture(0.8, 0.3, 0.3, 1)
    slider.handle.texture:SetAllPoints(true)
    
    -- Texto debajo (valor actual)
    slider.valueText = slider:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    slider.valueText:SetPoint("TOP", slider, "BOTTOM", 0, - 4)
    
    -- Texto mínimo y máximo
    slider.minText = slider:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    slider.minText:SetPoint("TOPLEFT", slider, "BOTTOMLEFT", 0, - 4)
    slider.minText:SetText(tostring(minValue))
    
    slider.maxText = slider:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    slider.maxText:SetPoint("TOPRIGHT", slider, "BOTTOMRIGHT", 0, - 4)
    slider.maxText:SetText(tostring(maxValue))
    
    local dragging = false
    local lastValue = nil
    
    -- Convertir valor a posición X relativa
    local function ValueToPosition(value)
        return ((value - minValue) / (maxValue - minValue)) * slider:GetWidth()
    end
    
    -- Convertir posición X a valor ajustado
    local function PositionToValue(x)
        local pct = math.min(math.max(x / slider:GetWidth(), 0), 1)
        local raw = minValue + (maxValue - minValue) * pct
        local adjusted = math.floor((raw + step / 2) / step) * step
        return math.min(adjusted, maxValue)
    end
    
    -- Actualizar visual del marcador y texto
    function slider:SetValue(newValue, skipCallback)
        newValue = math.min(math.max(newValue, minValue), maxValue)
        if lastValue ~= newValue then
            lastValue = newValue
            slider.currentValue = newValue
            slider.handle:SetPoint("CENTER", slider, "LEFT", ValueToPosition(newValue), 0)
            slider.valueText:SetText(tostring(newValue))
            if onValueChanged and not skipCallback then
                onValueChanged(newValue)
            end
        end
    end
    
    slider:SetValue(initialValue, true)
    
    -- Función para actualizar mientras se arrastra
    local function UpdateDragging()
        if not dragging then return end
        local x = GetCursorPosition() / UIParent:GetEffectiveScale() - slider:GetLeft()
        slider:SetValue(PositionToValue(x))
    end
    
    -- Evento global OnUpdate para mover suavemente mientras arrastra
    slider:SetScript("OnUpdate", function()
        if dragging then
            UpdateDragging()
        end
    end)
    
    -- Drag manual del handle
    slider.handle:EnableMouse(true)
    slider.handle:SetScript("OnMouseDown", function(_, button)
        if button == "LeftButton" then
            dragging = true
            UpdateDragging()
        end
    end)
    slider.handle:SetScript("OnMouseUp", function()
        dragging = false
    end)
    
    -- Click en la barra también inicia arrastre
    slider:EnableMouse(true)
    slider:SetScript("OnMouseDown", function(_, button)
        if button == "LeftButton" then
            dragging = true
            UpdateDragging()
        end
    end)
    slider:SetScript("OnMouseUp", function()
        dragging = false
    end)
    
    return slider
end

-- -- USO DE EJEMPLO:
-- local mySlider = ns.widgets:CreateCustomSlider(UIParent, 100, 30, 1, 100, 5, 50, function(value)
--     print("Nuevo valor del slider:", value)
-- end, "Volumen")

-- mySlider:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
