--Data.lua

local addonName, addon = ...
addon.DB = {}
local DB = addon.DB

DB.VALORSTONE_ID = 3008
DB.CREST_IDS = { [2914] = true, [2915] = true, [2916] = true, [2917] = true }

DB.costs = {
    [2914] = { currencyCost = 0, slotCost = 0 },
    [2915] = { currencyCost = 0, slotCost = 0 },
    [2916] = { currencyCost = 0, slotCost = 0 },
    [2917] = { currencyCost = 0, slotCost = 0 },
    [3008] = { currencyCost = 0, slotCost = 0 },
}

local function initializeCostTable()
    return {
        [2914] = { cost = 0 }, [2915] = { cost = 0 },
        [2916] = { cost = 0 }, [2917] = { cost = 0 },
        [3008] = { cost = 0 }
    }
end

DB.totalCurrencyCosts = initializeCostTable()
DB.totalSlotCost = initializeCostTable()

DB.itemLevelsAndCrests = {
    [558] = { CrestID = nil, CrestCost = 0 },
    [561] = { CrestID = nil, CrestCost = 0 },
    [564] = { CrestID = nil, CrestCost = 0 },
    [567] = { CrestID = nil, CrestCost = 0 },
    [571] = { CrestID = nil, CrestCost = 0 },
    [574] = { CrestID = nil, CrestCost = 0 },
    [577] = { CrestID = nil, CrestCost = 0 },
    [580] = { CrestID = nil, CrestCost = 0 },
    [584] = { CrestID = 2914, CrestCost = 15 },
    [587] = { CrestID = 2914, CrestCost = 15 },
    [590] = { CrestID = 2914, CrestCost = 15 },
    [593] = { CrestID = 2914, CrestCost = 15 },
    [597] = { CrestID = 2915, CrestCost = 15 },
    [600] = { CrestID = 2915, CrestCost = 15 },
    [603] = { CrestID = 2915, CrestCost = 15 },
    [606] = { CrestID = 2915, CrestCost = 15 },
    [610] = { CrestID = 2916, CrestCost = 15 },
    [613] = { CrestID = 2916, CrestCost = 15 },
    [616] = { CrestID = 2916, CrestCost = 15 },
    [619] = { CrestID = 2916, CrestCost = 15 },
    [623] = { CrestID = 2917, CrestCost = 15 },
    [626] = { CrestID = 2917, CrestCost = 15 },
    [629] = { CrestID = 2917, CrestCost = 15 },
    [632] = { CrestID = 2917, CrestCost = 15 },
    [636] = { CrestID = 2917, CrestCost = 15 },
    [639] = { CrestID = 2917, CrestCost = 15 },
}

DB.valorCostsBySlot = {
    [1] = { 50, 75, 120, 145, 170, 195, 220 }, -- head
    [2] = { 25, 40, 65, 100, 120, 140, 160 }, -- neck
    [3] = { 40, 65, 100, 120, 140, 160, 180 }, -- shoulders
    [5] = { 50, 75, 120, 145, 170, 195, 220 }, -- chest
    [6] = { 40, 65, 100, 120, 140, 160, 180 }, -- belt
    [7] = { 50, 75, 120, 145, 170, 195, 220 }, -- legs
    [8] = { 40, 65, 100, 120, 140, 160, 180 }, -- feet
    [9] = { 25, 40, 65, 100, 120, 140, 160 }, -- wrist
    [10] = { 25, 40, 65, 100, 120, 140, 160 }, -- hands
    [11] = { 25, 40, 65, 100, 120, 140, 160 }, -- finger 1
    [12] = { 25, 40, 65, 100, 120, 140, 160 }, -- finger 2
    [13] = { 40, 65, 100, 120, 140, 160, 180 }, -- trinket 1
    [14] = { 40, 65, 100, 120, 140, 160, 180 }, -- trinket 2
    [15] = { 25, 40, 65, 100, 120, 140, 160 }, -- back
    [16] = { 75, 120, 130, 180, 210, 250, 300 }, -- main hand
    [17] = { 25, 40, 65, 75, 90, 140, 160 }, -- off hand
}

function DB:BuildUpgradeTable()
    self.upgradeCostsBySlot = {}

    -- Get the keys and sort them
    local itemLevels = {}
    for itemLevel in pairs(self.itemLevelsAndCrests) do
        table.insert(itemLevels, itemLevel)
    end
    table.sort(itemLevels)

    for slot, valorCosts in pairs(self.valorCostsBySlot) do
        local upgradeCosts = self.upgradeCostsBySlot[slot] or { iLevel = {} }
        local costIndex = 1
        local iterationCount = 0

        for _, itemLevel in ipairs(itemLevels) do
            local data = self.itemLevelsAndCrests[itemLevel]
            
            if valorCosts[costIndex] then
                upgradeCosts.iLevel[itemLevel] = {
                    CrestID = data.CrestID,
                    CrestCost = data.CrestCost,
                    ValorStoneCost = valorCosts[costIndex]
                }
                
                iterationCount = iterationCount + 1

                if iterationCount % 4 == 0 then
                    costIndex = costIndex + 1
                end
            end
        end

        self.upgradeCostsBySlot[slot] = upgradeCosts
    end
    return self.upgradeCostsBySlot
end

function addon.printTable(node, recursive, indent, visited)
    visited = visited or {}
    indent = indent or ""
    recursive = recursive or false

    -- Initial check for empty table
    if type(node) ~= "table" then
        print(indent .. "Provided node is not a table.")
        return
    end

    -- Check if the table is empty
    local isEmpty = true
    for _ in pairs(node) do
        isEmpty = false
        break
    end

    if isEmpty then
        print(indent .. "Table is empty.")
        return
    end

    -- Continue with existing logic
    print("printTable called ")
    for key, value in pairs(node) do
        if type(key) == "number" then
            key = "[" .. key .. "]"
        end
        
        if type(value) == "table" then
            if visited[value] then
                print(indent .. key .. ": table (already visited) " .. tostring(value))
            elseif recursive then
                visited[value] = true
                print(indent .. key .. ": table " .. tostring(value))
                addon.printTable(value, recursive, indent .. "  ", visited)
            else
                print(indent .. key .. ": table " .. tostring(value))
            end
        elseif type(value) == "function" then
            print(indent .. key .. ": function")
        elseif type(value) == "userdata" then
            print(indent .. key .. ": userdata " .. tostring(value))
        else
            print(indent .. key .. " " .. tostring(value))
        end
    end
end

local LogLevel = {
    [1] = {name = "ERROR", color = "|cffff0000"}, -- Red
    [2] = {name = "WARN", color = "|cffffff00"}, -- Yellow
    [3] = {name = "INFO", color = "|cff00ff00"}, -- Green
    [4] = {name = "DEBUG", color = "|cff0000ff"}, -- Blue
}

-- Set the global log level to DEBUG (or another level if desired)
local globalLogLevel = 0

function addon.HexLog(message, level, module)
    level = level or 4 -- Default to DEBUG if level is not provided
    local moduleLogLevel = module and module.logLevel or 4 -- Default module level to DEBUG if not provided
    local effectiveLogLevel = math.min(globalLogLevel, moduleLogLevel)
    if level <= effectiveLogLevel then
        local logLevelData = LogLevel[level]
        local coloredMessage = logLevelData.color .. message .. "|r" -- |r resets the color
        DEFAULT_CHAT_FRAME:AddMessage(coloredMessage)
    end
end

-- Example usage in Core.lua
addon.HexLog("This is a global error message", 1)
addon.HexLog("This is a global warning message", 2)
addon.HexLog("This is a global info message", 3)
addon.HexLog("This is a global debug message", 4)
