local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local UI = addonTable.UI
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

local CLOCK_FORMAT_OPTIONS = {
    { value = "24", label = "24-hour" },
    { value = "12", label = "12-hour" },
}

WipedQoL:RegisterTabBuilder("datapanel", function(addon, parent)
    local frame = CreateFrame("Frame", nil, parent)
    local settings = addon:GetDatapanelSettings()
    local datapanelCheckbox
    local clockCheckbox
    local clockFormatLabel
    local clockFormatDropdown
    local durabilityCheckbox
    local lootSpecCheckbox
    local autoLootRollCheckbox
    local combatPotionCheckbox
    local fpsCheckbox
    local fpsFixedCheckbox
    local lockCheckbox

    local function SetCheckBoxEnabled(checkbox, enabled)
        if not checkbox then
            return
        end

        checkbox:SetAlpha(enabled and 1 or 0.45)
        checkbox:EnableMouse(enabled)
    end

    local function RefreshDatapanelOptionsState()
        local datapanelEnabled = settings.enabled == true
        local datapanelVisible = datapanelEnabled
            and (settings.clock.enabled == true
                or settings.durability.enabled == true
                or settings.lootSpec.enabled == true
                or settings.autoLootRoll.enabled == true
                or settings.combatPotion.enabled == true)
        local clockEnabled = datapanelEnabled and settings.clock.enabled == true

        if datapanelCheckbox and datapanelCheckbox.Refresh then
            datapanelCheckbox.Refresh()
        end
        if fpsCheckbox and fpsCheckbox.Refresh then
            fpsCheckbox.Refresh()
        end
        SetCheckBoxEnabled(clockCheckbox, datapanelEnabled)
        SetCheckBoxEnabled(durabilityCheckbox, datapanelEnabled)
        SetCheckBoxEnabled(lootSpecCheckbox, datapanelEnabled)
        SetCheckBoxEnabled(autoLootRollCheckbox, datapanelEnabled)
        SetCheckBoxEnabled(combatPotionCheckbox, datapanelEnabled)
        SetCheckBoxEnabled(fpsFixedCheckbox, datapanelVisible and settings.fps.enabled == true)
        SetCheckBoxEnabled(lockCheckbox, datapanelEnabled)

        if clockFormatLabel then
            clockFormatLabel:SetTextColor(clockEnabled and 0.78 or 0.38, clockEnabled and 0.78 or 0.38, clockEnabled and 0.78 or 0.38)
        end

        if clockFormatDropdown then
            clockFormatDropdown:SetAlpha(clockEnabled and 1 or 0.45)
            if clockFormatDropdown._wqolClickOverlay then
                clockFormatDropdown._wqolClickOverlay:EnableMouse(clockEnabled)
            end
        end

        if fpsFixedCheckbox and fpsFixedCheckbox.Refresh then
            fpsFixedCheckbox.Refresh()
        end
    end

    local datapanelSection = UI.CreateSection(
        frame,
        TRANSLATE("Datapanel"),
        TRANSLATE("Shows a movable bar with the selected information.")
    )
    datapanelSection:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -14)
    datapanelSection:SetPoint("TOPRIGHT", frame, "TOP", -6, -14)
    datapanelSection:SetHeight(104)

    local fpsSection = UI.CreateSection(
        frame,
        TRANSLATE("FPS panel"),
        TRANSLATE("Shows a small FPS panel.")
    )
    fpsSection:SetPoint("TOPLEFT", frame, "TOP", 6, -14)
    fpsSection:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -14, -14)
    fpsSection:SetHeight(104)

    local contentSection = UI.CreateSection(
        frame,
        TRANSLATE("Datapanel elements"),
        TRANSLATE("Choose which information appears in the datapanel.")
    )
    contentSection:SetPoint("TOPLEFT", datapanelSection, "BOTTOMLEFT", 0, -12)
    contentSection:SetPoint("TOPRIGHT", fpsSection, "BOTTOMRIGHT", 0, -12)
    contentSection:SetHeight(161)

    datapanelCheckbox = UI.CreateSectionToggle(datapanelSection, function()
        return settings.enabled == true
    end, function(value)
        addon:SetDatapanelEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)

    lockCheckbox = UI.CreateCheckBox(datapanelSection, TRANSLATE("Lock datapanel"), function()
        return settings.locked == true
    end, function(value)
        addon:SetDatapanelLocked(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    lockCheckbox:SetPoint("TOPLEFT", datapanelSection.description, "BOTTOMLEFT", 0, -8)
    lockCheckbox:SetSize(220, 24)

    fpsCheckbox = UI.CreateSectionToggle(fpsSection, function()
        return settings.fps.enabled == true
    end, function(value)
        addon:SetDatapanelFpsEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)

    fpsFixedCheckbox = UI.CreateCheckBox(fpsSection, TRANSLATE("Lock FPS to datapanel"), function()
        return settings.fps.fixed == true
    end, function(value)
        addon:SetDatapanelFpsFixed(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    fpsFixedCheckbox:SetPoint("TOPLEFT", fpsSection.description, "BOTTOMLEFT", 0, -8)
    fpsFixedCheckbox:SetSize(240, 24)

    lootSpecCheckbox = UI.CreateCheckBox(contentSection, TRANSLATE("Show loot specialization"), function()
        return settings.lootSpec.enabled == true
    end, function(value)
        addon:SetDatapanelLootSpecEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    lootSpecCheckbox:SetPoint("TOPLEFT", contentSection.description, "BOTTOMLEFT", 0, -10)
    lootSpecCheckbox:SetSize(250, 24)

    autoLootRollCheckbox = UI.CreateCheckBox(contentSection, TRANSLATE("Show auto loot roll"), function()
        return settings.autoLootRoll.enabled == true
    end, function(value)
        addon:SetDatapanelAutoLootRollEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    autoLootRollCheckbox:SetPoint("TOPLEFT", lootSpecCheckbox, "TOPLEFT", 280, 0)
    autoLootRollCheckbox:SetSize(250, 24)

    clockCheckbox = UI.CreateCheckBox(contentSection, TRANSLATE("Show time"), function()
        return settings.clock.enabled == true
    end, function(value)
        addon:SetDatapanelClockEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    clockCheckbox:SetPoint("TOPLEFT", lootSpecCheckbox, "BOTTOMLEFT", 0, -10)
    clockCheckbox:SetSize(220, 24)

    clockFormatLabel = UI.CreateLabel(contentSection, TRANSLATE("Time format"), 12)
    clockFormatLabel:SetPoint("LEFT", clockCheckbox, "LEFT", 280, 0)
    clockFormatLabel:SetTextColor(0.78, 0.78, 0.78)

    clockFormatDropdown = UI.CreateDropdown(contentSection, 86, CLOCK_FORMAT_OPTIONS, function()
        return settings.clock.format or "24"
    end, function(value)
        addon:SetDatapanelClockFormat(value)
        settings = addon:GetDatapanelSettings()
    end)
    clockFormatDropdown:SetPoint("LEFT", clockFormatLabel, "RIGHT", -12, 0)

    durabilityCheckbox = UI.CreateCheckBox(contentSection, TRANSLATE("Show armor durability"), function()
        return settings.durability.enabled == true
    end, function(value)
        addon:SetDatapanelDurabilityEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    durabilityCheckbox:SetPoint("TOPLEFT", clockCheckbox, "BOTTOMLEFT", 0, -10)
    durabilityCheckbox:SetSize(220, 24)

    combatPotionCheckbox = UI.CreateCheckBox(contentSection, TRANSLATE("Show combat potion toggle"), function()
        return settings.combatPotion.enabled == true
    end, function(value)
        addon:SetDatapanelCombatPotionEnabled(value)
        settings = addon:GetDatapanelSettings()
        RefreshDatapanelOptionsState()
    end)
    combatPotionCheckbox:SetPoint("TOPLEFT", durabilityCheckbox, "TOPLEFT", 280, 0)
    combatPotionCheckbox:SetSize(250, 24)

    RefreshDatapanelOptionsState()

    return frame
end)
