-- ============================================================================
-- ARENACORE minimalist theme
-- Modified to integrate with ArenaCore's slider system and theme isolation
-- ============================================================================

local layoutName = "Flat"
local layout = {}

layout.defaultSettings = {
    posX = 281,
    posY = 3,
    scale = 1,
    classIconFontSize = 14,
    spacing = 25,
    growthDirection = 1,
    specIcon = {
        posX = 44,
        posY = 0,
        scale = 1,
    },
    trinket = {
        posX = 120,
        posY = 0,
        scale = 1,
        fontSize = 14,
    },
    racial = {
        posX = 154,
        posY = 0,
        scale = 1,
        fontSize = 14,
    },
    castBar = {
        posX = -126,
        posY = 0,
        scale = 1.3,
        width = 90,
    },
    dr = {
        posX = 190,
        posY = 0,
        size = 28,
        borderSize = 2.5,
        fontSize = 12,
        spacing = 6,
        growthDirection = 3,
    },

    -- custom layout settings
    width = 200,
    height = 43,
    powerBarHeight = 9,
    mirrored = false,
}

local function getSetting(info)
    return layout.db[info[#info]]
end

local function setSetting(info, val)
    layout.db[info[#info]] = val

    for i = 1,3 do
        local frame = info.handler["arena"..i]
        -- CRITICAL FIX: Ensure frame height accommodates 43px class icon
        local frameHeight = math.max(layout.db.height, 43)
        frame:SetSize(layout.db.width, frameHeight)
        -- CRITICAL FIX: Use consistent theme class icon size instead of variable db.height
        -- This prevents Arena 2 from reverting to 32x32 when settings change
        frame.ClassIcon:SetSize(43, 43)  -- Fixed theme size for all frames
        frame.DeathIcon:SetSize(layout.db.height * 0.8, layout.db.height * 0.8)
        frame.PowerBar:SetHeight(layout.db.powerBarHeight)

        -- DEBUG: Check what happens in setSetting
        local frameName = frame:GetName() or "unknown"
        local frameIndex = "unknown"
        if frameName == "ArenaCoreFrame1" then frameIndex = "Arena 1 (TOP)"
        elseif frameName == "ArenaCoreFrame2" then frameIndex = "Arena 2 (MIDDLE)"
        elseif frameName == "ArenaCoreFrame3" then frameIndex = "Arena 3 (BOTTOM)"
        end
        print("|cffFF0000[SETTING CHANGE]|r", frameName, "(", frameIndex, ") ClassIcon set to 43x43, db.height was:", layout.db.height)

        layout:UpdateOrientation(frame)
    end
end

local function setupOptionsTable(self)
    layout.optionsTable = self:GetLayoutOptionsTable(layoutName)

    layout.optionsTable.arenaFrames.args.positioning.args.mirrored = {
        order = 5,
        name = "Mirrored Frames",
        type = "toggle",
        width = "full",
        get = getSetting,
        set = setSetting,
    }

    layout.optionsTable.special = {
        order = 7,
        name = "Special",
        type = "group",
        get = getSetting,
        set = setSetting,
        args = {
            width = {
                order = 1,
                name = "Width",
                type = "range",
                min = 40,
                max = 400,
                step = 1,
            },
            height = {
                order = 2,
                name = "Height",
                type = "range",
                min = 2,
                max = 100,
                step = 1,
            },
            powerBarHeight = {
                order = 2,
                name = "Power Bar Height",
                type = "range",
                min = 1,
                max = 50,
                step = 1,
            },
        },
    }
end

function layout:Initialize(frame)
    self.db = frame.parent.db.profile.layoutSettings[layoutName]

    if ( not self.optionsTable ) then
        setupOptionsTable(frame.parent)
    end

    if ( frame:GetID() == 3 ) then
        frame.parent:UpdateCastBarSettings(self.db.castBar)
        frame.parent:UpdateDRSettings(self.db.dr)
        frame.parent:UpdateFrameSettings(self.db)
        frame.parent:UpdateSpecIconSettings(self.db.specIcon)
        frame.parent:UpdateTrinketSettings(self.db.trinket)
        frame.parent:UpdateRacialSettings(self.db.racial)
    end

    self:UpdateOrientation(frame)

    -- CRITICAL FIX: Ensure frame height matches class icon size for consistent appearance
    -- If frame height is less than 43, force it to 43 to accommodate class icon
    local frameHeight = math.max(self.db.height, 43)
    frame:SetSize(self.db.width, frameHeight)
    frame.SpecIcon:SetSize(18, 18)
    frame.Trinket:SetSize(32, 32)
    frame.Racial:SetSize(32, 32)

    frame.HealthBar:SetStatusBarTexture("Interface\\AddOns\\ArenaCore\\modules\\FlatTheme\\statusbar")

    frame.PowerBar:SetStatusBarTexture("Interface\\AddOns\\ArenaCore\\modules\\FlatTheme\\statusbar")
    frame.PowerBar:SetHeight(self.db.powerBarHeight)

    -- CRITICAL FIX: Use consistent theme class icon size for ALL frames
    -- This prevents Arena 2 from being smaller (32) when Arena 1&3 are larger (43)
    local themeSize = 43  -- Fixed theme size for consistent appearance across all frames
    frame.ClassIcon:SetSize(themeSize, themeSize)
    frame.ClassIcon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    
        frame.ClassIcon:Show()

    frame.SpecIcon.Texture:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    frame.Trinket.Texture:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    frame.Racial.Texture:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    frame.CastBar.Icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)

    local f = frame.Name
    f:SetJustifyH("LEFT")
    f:SetJustifyV("BOTTOM")
    f:SetFontObject("SystemFont_Shadow_Med3")
    
    -- CRITICAL FIX: Read from ArenaCore slider settings instead of hardcoding
    local AC = _G.ArenaCore
    local nameX = 0  -- Default X offset
    local nameY = 2  -- Default Y offset
    
    if AC and AC.DB and AC.DB.profile then
        -- Try theme-specific settings first
        if AC.ArenaFrameThemes then
            local currentTheme = AC.ArenaFrameThemes:GetCurrentTheme()
            local themeData = AC.DB.profile.themeData and AC.DB.profile.themeData[currentTheme]
            local general = themeData and themeData.arenaFrames and themeData.arenaFrames.general
            
            -- Fallback to global settings if theme settings don't exist
            if not general then
                general = AC.DB.profile.arenaFrames and AC.DB.profile.arenaFrames.general
            end
            
            if general then
                nameX = (general.playerNameX ~= nil) and general.playerNameX or 52
                nameY = (general.playerNameY ~= nil) and general.playerNameY or 0
            end
        end
    end
    
    f:SetPoint("TOPLEFT", frame, "TOPLEFT", nameX, nameY)
    f:SetPoint("TOPRIGHT", frame, "TOPRIGHT", nameX, nameY)
    f:SetHeight(12)

    f = frame.CastBar
    f:SetStatusBarTexture("Interface\\AddOns\\ArenaCore\\modules\\FlatTheme\\statusbar")

    f = frame.DeathIcon
    f:ClearAllPoints()
    f:SetPoint("CENTER", frame.HealthBar, "CENTER")
    f:SetSize(self.db.height * 0.8, self.db.height * 0.8)

    frame.HealthText:SetPoint("CENTER", frame.HealthBar)
    frame.HealthText:SetShadowOffset(0, 0)

    frame.PowerText:SetPoint("CENTER", frame.PowerBar)
    frame.PowerText:SetShadowOffset(0, 0)

    local bg = frame.TexturePool:Acquire()
    bg:SetDrawLayer("BACKGROUND", 1)
    bg:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background")
    bg:SetPoint("TOPLEFT", frame, "TOPLEFT", -2, 2)
    bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 2, -2)
    bg:SetVertexColor(0, 0, 0, 1)
    bg:Show()
end

function layout:UpdateOrientation(frame)
    local healthBar = frame.HealthBar
    local powerBar = frame.PowerBar
    local classIcon = frame.ClassIcon

    healthBar:ClearAllPoints()
    powerBar:ClearAllPoints()
    classIcon:ClearAllPoints()

    if ( self.db.mirrored ) then
        classIcon:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)

        healthBar:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
        healthBar:SetPoint("BOTTOMLEFT", powerBar, "TOPLEFT", 0, 2)

        powerBar:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
        powerBar:SetPoint("LEFT", classIcon, "RIGHT", 2, 0)
    else
        classIcon:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)

        healthBar:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
        healthBar:SetPoint("BOTTOMRIGHT", powerBar, "TOPRIGHT", 0, 2)

        powerBar:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
        powerBar:SetPoint("RIGHT", classIcon, "LEFT", -2, 0)
    end
    
    -- CRITICAL FIX: Also update player name positioning when orientation changes
    self:UpdateNamePosition(frame)
end

function layout:UpdateNamePosition(frame)
    if not frame or not frame.Name then return end
    
    -- Read from ArenaCore slider settings
    local AC = _G.ArenaCore
    local nameX = 131  -- Default for The 1500 Special
    local nameY = -3   -- Default for The 1500 Special
    
    if AC and AC.DB and AC.DB.profile then
        if AC.ArenaFrameThemes then
            local currentTheme = AC.ArenaFrameThemes:GetCurrentTheme()
            local themeData = AC.DB.profile.themeData and AC.DB.profile.themeData[currentTheme]
            local general = themeData and themeData.arenaFrames and themeData.arenaFrames.general
            
            if not general then
                general = AC.DB.profile.arenaFrames and AC.DB.profile.arenaFrames.general
            end
            
            if general then
                nameX = (general.playerNameX ~= nil) and general.playerNameX or 131
                nameY = (general.playerNameY ~= nil) and general.playerNameY or -3
            end
        end
    end
    
    frame.Name:ClearAllPoints()
    frame.Name:SetPoint("TOPLEFT", frame, "TOPLEFT", nameX, nameY)
    frame.Name:SetPoint("TOPRIGHT", frame, "TOPRIGHT", nameX, nameY)
end

-- Register with ArenaCore
-- Legacy registration removed