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)
        frame.ClassIcon:SetSize(43, 43)
        frame.DeathIcon:SetSize(layout.db.height * 0.8, layout.db.height * 0.8)
        frame.PowerBar:SetHeight(layout.db.powerBarHeight)
        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\\RaidFrame\\Raid-Bar-Hp-Fill")

    frame.PowerBar:SetStatusBarTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill")
    frame.PowerBar:SetHeight(self.db.powerBarHeight)

    -- CRITICAL FIX: Use consistent theme class icon size instead of variable db.height
    -- This prevents Arena 2 from having different size than Arena 1&3
    frame.ClassIcon:SetSize(43, 43)  -- Fixed theme size for all frames
    frame.ClassIcon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
    frame.ClassIcon:Show()

    -- DEBUG: Check runtime class icon bounds for all frames
    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

    -- Wait a frame then print actual runtime values
    C_Timer.After(0.1, function()
        if frame.ClassIcon then
            local fw, fh = frame:GetSize()
            local iw, ih = frame.ClassIcon:GetSize()
            local ft, fb = frame:GetTop(), frame:GetBottom()
            local it, ib = frame.ClassIcon:GetTop(), frame.ClassIcon:GetBottom()
            print("|cffFFFF00[SIMPLE CORE]|r", frameName, "(", frameIndex, ") frame:", fw.."x"..fh, "icon:", iw.."x"..ih, "frameT:", ft, "iconT:", it, "frameB:", fb, "iconB:", ib)
        end
    end)

    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
    -- This fixes player name sliders not working with Simple Core theme
    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 0
                nameY = (general.playerNameY ~= nil) and general.playerNameY or 2
                print(string.format("|cff00FFFF[SIMPLE CORE]|r Using slider values: X=%d, Y=%d", nameX, nameY))
            end
        end
    end
    
    f:SetPoint("BOTTOMLEFT", frame.HealthBar, "TOPLEFT", nameX, nameY)
    f:SetPoint("BOTTOMRIGHT", frame.HealthBar, "TOPRIGHT", nameX, nameY)
    f:SetHeight(12)

    f = frame.CastBar
    f:SetStatusBarTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill")

    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 = 0
    local nameY = 2
    
    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 0
                nameY = (general.playerNameY ~= nil) and general.playerNameY or 2
                print(string.format("|cff00FFFF[SIMPLE CORE UPDATE]|r Updating name position: X=%d, Y=%d", nameX, nameY))
            end
        end
    end
    
    frame.Name:ClearAllPoints()
    frame.Name:SetPoint("BOTTOMLEFT", frame.HealthBar, "TOPLEFT", nameX, nameY)
    frame.Name:SetPoint("BOTTOMRIGHT", frame.HealthBar, "TOPRIGHT", nameX, nameY)
end

if _G.sArenaMixin then
    _G.sArenaMixin.layouts[layoutName] = layout
    _G.sArenaMixin.defaultSettings.profile.layoutSettings[layoutName] = layout.defaultSettings
end