local Layout = SUI:NewModule('Config.Layout.Unitframes')

function Layout:OnEnable()
    -- Database
    local db = SUI.db

    -- Data
    local Textures = SUI:GetModule("Data.Textures")
    local PlayerModule = SUI:GetModule("UnitFrames.Player", true)
    local TargetModule = SUI:GetModule("UnitFrames.Target", true)
    local CoreModule = SUI:GetModule("UnitFrames.Core", true)
    local NameplateModule = SUI:GetModule("NamePlates.Core", true)
    local HideModule = SUI:GetModule("UnitFrames.Hide", true)
    local CombatModule = SUI:GetModule("UnitFrames.Combat", true)
    local TextModule = SUI:GetModule("UnitFrames.Text", true)
    local ColorModule = SUI:GetModule("UnitFrames.Colors", true)

    local function refreshTargetAuras()
        if TargetModule and TargetModule.RefreshAuras then
            TargetModule:RefreshAuras()
        end
    end

    local function refreshPersonalBar()
        if NameplateModule and NameplateModule.RefreshPersonalResource then
            NameplateModule:RefreshPersonalResource()
        end
    end

    local function refreshUnitframeTextures()
        if PlayerModule and PlayerModule.RefreshTextures then
            PlayerModule:RefreshTextures()
        end
        if TargetModule and TargetModule.RefreshTextures then
            TargetModule:RefreshTextures()
        end
        if CoreModule and CoreModule.RefreshTextures then
            CoreModule:RefreshTextures()
        end
    end

    local function refreshPlayerVisibility()
        if PlayerModule and PlayerModule.RefreshVisibility then
            PlayerModule:RefreshVisibility()
        end
    end

    local function refreshPvPBadge()
        if HideModule and HideModule.RefreshVisibility then
            HideModule:RefreshVisibility()
        end
    end

    local function refreshCombatIcon()
        if CombatModule and CombatModule.RefreshVisibility then
            CombatModule:RefreshVisibility()
        end
    end

    local function refreshHitIndicator()
        if TextModule and TextModule.RefreshVisibility then
            TextModule:RefreshVisibility()
        end
    end

    local function refreshUnitframeColors()
        if ColorModule and ColorModule.RefreshColors then
            ColorModule:RefreshColors()
        end
    end

    -- Layout
    Layout.layout = {
        layoutConfig = { padding = { top = 15 } },
        database = db.profile,
        rows = {
            {
                header = {
                    type = 'header',
                    label = 'Player and Target'
                },
            },
            {
                style = {
                    key     = 'unitframes.style',
                    type    = 'dropdown',
                    label   = 'Style',
                    options = {
                        { value = 'Default', text = 'Default' },
                        { value = 'Classic', text = 'Classic' },
                        --{ value = 'Big', text = 'Big' },
                        --{ value = 'Small', text = 'Transparent' }
                    },
                    column  = 4,
                    order   = 1
                },
                texture = {
                    key = 'general.texture',
                    type = 'dropdown',
                    label = 'Texture',
                    options = Textures.data,
                    column = 4,
                    order = 3,
                    onChange = refreshUnitframeTextures
                },
            },
            {
                class = {
                    key = 'unitframes.classcolor',
                    type = 'checkbox',
                    label = 'Class Color',
                    tooltip = 'Change healthcolor to class color',
                    column = 4,
                    order = 1,
                    onChange = refreshUnitframeColors
                },
                pvp = {
                    key = 'unitframes.pvpbadge',
                    type = 'checkbox',
                    label = 'PvP Badge',
                    tooltip = 'Display PVP icon on Unit frames',
                    column = 4,
                    order = 2,
                    onChange = refreshPvPBadge
                },
                hitindicator = {
                    key = 'unitframes.hitindicator',
                    type = 'checkbox',
                    label = 'Hit indicator',
                    tooltip = 'Display numbers on Player Portrait',
                    column = 4,
                    order = 3,
                    onChange = refreshHitIndicator
                },
            },
            {
                combat = {
                    key = 'unitframes.combaticon',
                    type = 'checkbox',
                    label = 'Combat Icon',
                    tooltip = 'Display combat icon on Unitframes',
                    column = 4,
                    order = 1,
                    onChange = refreshCombatIcon
                },
                totemicons = {
                    key = 'unitframes.totemicons',
                    type = 'checkbox',
                    label = 'Totem Icons',
                    tooltip = 'Show Totem Icons (Consecration duration etc.) below the Player Unitframe',
                    column = 4,
                    order = 2,
                },
                classbar = {
                    key = 'unitframes.classbar',
                    type = 'checkbox',
                    label = 'Class Bar',
                    tooltip = 'Show ClassBar (Combopoints, HolyPower etc.)',
                    column = 4,
                    order = 3,
                }
            },
            {
                cornericon = {
                    key = 'unitframes.cornericon',
                    type = 'checkbox',
                    label = 'Corner Icon',
                    tooltip = 'Display corner icon on Unitframes',
                    column = 4,
                    order = 1,
                    onChange = refreshPlayerVisibility
                }
            },
            {
                header = {
                    type = 'header',
                    label = 'Personal Bar'
                },
            },
            {
                personalbarstyle = {
                    key = 'unitframes.personalbar.style',
                    type = 'dropdown',
                    label = 'Style',
                    options = {
                        { value = 'Default', text = 'Default' },
                        { value = 'Custom', text = 'Custom' }
                    },
                    column = 4,
                    order = 1,
                    onChange = refreshPersonalBar
                },
            },
            {
                personalbartexture = {
                    key = 'unitframes.personalbar.texture',
                    type = 'dropdown',
                    label = 'Texture',
                    options = Textures.data,
                    column = 4,
                    order = 1,
                    onChange = refreshPersonalBar
                },
                personalbarwidth = {
                    key = 'unitframes.personalbar.width',
                    type = 'slider',
                    label = 'Personal Nameplate Width',
                    min = 50,
                    max = 200,
                    column = 4,
                    order = 2,
                    onChange = refreshPersonalBar
                },
            },
            {
                personalbarheight = {
                    key = 'unitframes.personalbar.height',
                    type = 'slider',
                    label = 'Personal Height',
                    min = 1,
                    max = 35,
                    step = 0.1,
                    column = 4,
                    order = 1,
                    onChange = refreshPersonalBar
                },
                personalbarmanaheight = {
                    key = 'unitframes.personalbar.manaheight',
                    type = 'slider',
                    label = 'Mana Height',
                    min = 1,
                    max = 35,
                    step = 0.1,
                    column = 4,
                    order = 2,
                    onChange = refreshPersonalBar
                },
            },
            {
                header = {
                    type = 'header',
                    label = 'Raid and Party'
                },
            },
            {
                texture = {
                    key = 'raidframes.texture',
                    type = 'dropdown',
                    label = 'Texture',
                    options = Textures.data,
                    column = 4,
                    order = 1
                },
            },
            {
                header = {
                    type = 'header',
                    label = 'Target Auras'
                },
            },
            {
                buffsize = {
                    key = 'unitframes.buffs.size',
                    type = 'slider',
                    label = 'Target Buff Size',
                    max = 50,
                    column = 4,
                    order = 1,
                    onChange = refreshTargetAuras
                },
                debuffsize = {
                    key = 'unitframes.debuffs.size',
                    type = 'slider',
                    label = 'Target Debuff Size',
                    max = 50,
                    column = 4,
                    order = 2,
                    onChange = refreshTargetAuras
                },
            },
            {
                buffx = {
                    key = 'unitframes.buffs.targetx',
                    type = 'slider',
                    label = 'Buff X Offset',
                    min = -50,
                    max = 50,
                    column = 4,
                    order = 1,
                    onChange = refreshTargetAuras
                },
                buffy = {
                    key = 'unitframes.buffs.targety',
                    type = 'slider',
                    label = 'Buff Y Offset',
                    min = -50,
                    max = 50,
                    column = 4,
                    order = 2,
                    onChange = refreshTargetAuras
                },
            },
            {
                debuffx = {
                    key = 'unitframes.debuffs.targetx',
                    type = 'slider',
                    label = 'Debuff X Offset',
                    min = -50,
                    max = 50,
                    column = 4,
                    order = 1,
                    onChange = refreshTargetAuras
                },
                debuffy = {
                    key = 'unitframes.debuffs.targety',
                    type = 'slider',
                    label = 'Debuff Y Offset',
                    min = -50,
                    max = 50,
                    column = 4,
                    order = 2,
                    onChange = refreshTargetAuras
                },
            },
        },
    }
end
