local Unitframes = mUI:NewModule("mUI.Config.Layouts.Unitframes")

function Unitframes:OnInitialize()
    -- Get LSM
    Unitframes.LSM = LibStub("LibSharedMedia-3.0")

    -- Get Modules
    Unitframes.Module = mUI:GetModule("mUI.Modules.Unitframes")
    Unitframes.Theme = mUI:GetModule("mUI.Modules.General.Theme")

    -- Initialize Layout
    Unitframes.layout = {
        type = "group",
        args = {
            enable = {
                name = function()
                    if mUI.db.profile.unitframes.enabled then
                        return "|cff00ff00Enabled|r"
                    else
                        return "|cffff0000Disabled|r"
                    end
                end,
                desc = "Enable / Disable Module\n\n|cffffff00Info:|r Requires Reload",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.enabled = val

                    if val then
                        Unitframes.Module:Enable()
                        mUI:Reload('Enable Unitframes Module')
                    else
                        Unitframes.Module:Disable()
                        mUI:Reload('Disable Unitframes Module')
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.enabled
                end,
                order = 1
            },
            header1 = {
                name = "Textures",
                type = "header",
                order = 2
            },
            textures_unitframes = {
                name = "Unitframes",
                desc = "Select a Texture for the Unitframes (Player, Target, Focus, etc.)",
                type = "select",
                values = Unitframes.LSM:HashTable("statusbar"),
                dialogControl = 'mUI_LSM30_Status',
                set = function(_, val)
                    mUI.db.profile.unitframes.textures.unitframes = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val == "None" then
                        Unitframes.Module.UF_Textures:Disable()
                        Unitframes.Module.UF_Textures:Update()
                    else
                        Unitframes.Module.UF_Textures:Enable()
                        Unitframes.Module.UF_Textures:Update()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.textures.unitframes
                end,
                order = 3
            },
            dropdown_space = {
                name = " ",
                type = "description",
                order = 4
            },
            textures_raidframes = {
                name = "Party / Raidframes",
                desc = "Select a Texture for the Party / Raidframes",
                type = "select",
                values = Unitframes.LSM:HashTable("statusbar"),
                dialogControl = 'mUI_LSM30_Status',
                set = function(_, val)
                    mUI.db.profile.unitframes.textures.raidframes = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val == "None" then
                        Unitframes.Module.RF_Textures:Disable()
                        Unitframes.Module.RF_Textures:Update()
                    else
                        Unitframes.Module.RF_Textures:Enable()
                        Unitframes.Module.RF_Textures:Update()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.textures.raidframes
                end,
                order = 5
            },
            header2 = {
                name = "Options",
                type = "header",
                order = 6
            },
            color = {
                name = "Class/Reaction Colors",
                desc = "Show Healthbars in Class/Reaction Colors (Neutral etc.)",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.color = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Color:Enable()
                    else
                        Unitframes.Module.Color:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.color
                end,
                order = 7
            },
            combatindicator = {
                name = "Combat Indicator",
                desc = "Show a Combat Icon on Unitframes when Target/Focus in combat",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.combatindicator = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Combatindicator:Enable()
                    else
                        Unitframes.Module.Combatindicator:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.combatindicator
                end,
                order = 8
            },
            pvpbadge = {
                name = "Hide PVP Badge",
                desc = "Hide PVP Badge on Unitframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.pvpbadge = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Pvpbadge:Enable()
                    else
                        Unitframes.Module.Pvpbadge:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.pvpbadge
                end,
                order = 9
            },
            hitindicator = {
                name = "Hide Hit Indicator",
                desc = "Hide Hit Indicator (damage/healing numbers on the Player Portrait)",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.hitindicator = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Hitindicator:Enable()
                    else
                        Unitframes.Module.Hitindicator:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.hitindicator
                end,
                order = 10
            },
            restingtextures = {
                name = "Hide Rest Textures",
                desc = "Hide Resting Textures on Player Unitframe",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.restingtextures = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Restingtextures:Enable()
                    else
                        Unitframes.Module.Restingtextures:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.restingtextures
                end,
                order = 13
            },
            name = {
                name = "Hide Name",
                desc = "Hide Names on Unitframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.name = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.Name:Enable()
                    else
                        Unitframes.Module.Name:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.name
                end,
                order = 14
            },
            debuffcolors = {
                name = "Debuff Colors",
                desc = "Color borders of Debuffs by their type on Target/Focus Frames",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.buffsdebuffs.debuffcolors = val
                end,
                get = function()
                    return mUI.db.profile.unitframes.buffsdebuffs.debuffcolors
                end,
                order = 15
            },
            header3 = {
                name = "Buffs & Debuffs",
                type = "header",
                order = 17
            },
            enablebuffdebuff = {
                name = function()
                    if mUI.db.profile.unitframes.buffsdebuffs.enabled then
                        return "|cff00ff00Enabled|r"
                    else
                        return "|cffff0000Disabled|r"
                    end
                end,
                desc = "Enable / Disable Buffs & Debuffs re-sizing on Unitframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.buffsdebuffs.enabled = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.BuffsDebuffs:Enable()
                    else
                        Unitframes.Module.BuffsDebuffs:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.buffsdebuffs.enabled
                end,
                order = 18
            },
            buffsize = {
                name = "Buff Size",
                desc = "Set the Size of Buffs on Unitframes",
                type = "range",
                min = 0,
                max = 30,
                step = 1,
                set = function(_, val)
                    mUI.db.profile.unitframes.buffsdebuffs.buffsize = val
                end,
                get = function()
                    return mUI.db.profile.unitframes.buffsdebuffs.buffsize
                end,
                order = 19
            },
            debuffsize = {
                name = "Debuff Size",
                desc = "Set the Size of Debuffs on Unitframes",
                type = "range",
                min = 0,
                max = 30,
                step = 1,
                set = function(_, val)
                    mUI.db.profile.unitframes.buffsdebuffs.debuffsize = val
                end,
                get = function()
                    return mUI.db.profile.unitframes.buffsdebuffs.debuffsize
                end,
                order = 20
            },
            header4 = {
                name = "Party / Raidframes Resize",
                type = "header",
                order = 21
            },
            enablesizing = {
                name = function()
                    if mUI.db.profile.unitframes.raidframes.size.enabled then
                        return "|cff00ff00Enabled|r"
                    else
                        return "|cffff0000Disabled|r"
                    end
                end,
                desc = "Enable / Disable Raidframes re-sizing",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.size.enabled = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_Size:Enable()
                    else
                        Unitframes.Module.RF_Size:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.size.enabled
                end,
                order = 22
            },
            width = {
                name = "Width",
                desc = "Set the Width of Raidframes",
                type = "range",
                min = 0,
                max = 200,
                step = 1,
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.size.width = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end
                    if not Unitframes.Module.RF_Size:IsEnabled() then
                        return
                    end

                    Unitframes.Module.RF_Size:Update()
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.size.width
                end,
                order = 23
            },
            height = {
                name = "Height",
                desc = "Set the Height of Raidframes",
                type = "range",
                min = 0,
                max = 200,
                step = 1,
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.size.height = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end
                    if not Unitframes.Module.RF_Size:IsEnabled() then
                        return
                    end

                    Unitframes.Module.RF_Size:Update()
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.size.height
                end,
                order = 24
            },
            header5 = {
                name = "Raidframes",
                type = "header",
                order = 25
            },
            darkmode = {
                name = "Dark Mode",
                desc = "Make the Healthbars of Party/Raidframes dark",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.darkmode = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_Colors:Enable()
                    else
                        Unitframes.Module.RF_Colors:Disable()
                        mUI:Reload('Disable Raidframes Dark Mode')
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.darkmode
                end,
                order = 26
            },
            roleicons = {
                name = "Role Icons",
                desc = "Hide Role Icons on Party/Raidframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.roleicons = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_RoleIcons:Enable()
                    else
                        Unitframes.Module.RF_RoleIcons:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.roleicons
                end,
                order = 27
            },
            healthcolor = {
                name = "Classcolor Health",
                desc = "Show Health in Classcolor on Party/Raidframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.healthcolor = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        if not Unitframes.Module.RF_Health:IsEnabled() then
                            Unitframes.Module.RF_Health:Enable()
                        else
                            Unitframes.Module.RF_Health:Update()
                        end
                    else
                        if (not val) and (not mUI.db.profile.unitframes.raidframes.health) then
                            Unitframes.Module.RF_Health:Disable()
                        else
                            Unitframes.Module.RF_Health:Update()
                        end
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.healthcolor
                end,
                order = 29
            },
            names = {
                name = "Classcolor Names",
                desc = "Show Names in Classcolor on Party/Raidframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.names = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_Name:Enable()
                    else
                        Unitframes.Module.RF_Name:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.names
                end,
                order = 30
            },
            hidenames = {
                name = "Hide Names",
                desc = "Hide Names on Party/Raidframes",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.hidenames = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_HideNames:Enable()
                    else
                        Unitframes.Module.RF_HideNames:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.hidenames
                end,
                order = 31
            },
            solo = {
                name = "Solo Partyframes",
                desc = "Show Partyframes even when not in a group",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.unitframes.raidframes.solo = val

                    if not Unitframes.Module:IsEnabled() then
                        return
                    end

                    if val then
                        Unitframes.Module.RF_Solo:Enable()
                    else
                        Unitframes.Module.RF_Solo:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.unitframes.raidframes.solo
                end,
                order = 32
            }
        }
    }

    function Unitframes:GetOptions()
        return Unitframes.layout
    end
end
