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

function Chat:OnInitialize()
    -- Load LSM
    Chat.LSM = LibStub("LibSharedMedia-3.0")

    -- Get Modules
    Chat.Module = mUI:GetModule("mUI.Modules.Chat")

    -- Initialize Layout
    Chat.layout = {
        type = "group",
        args = {
            enabled = {
                name = function()
                    if mUI.db.profile.chat.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.chat.enabled = val
                    if val then
                        Chat.Module:Enable()
                        mUI:Reload('Enable Chat Module')
                    else
                        Chat.Module:Disable()
                        mUI:Reload("Disable Chat Module")
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.enabled
                end,
                order = 1
            },
            header1 = {
                name = "Chat",
                type = "header",
                order = 2
            },
            style = {
                name = "Style",
                desc = "Select a Style for the Chat\n\n|cffffff00Info:|r Requires Reload",
                type = "select",
                style = "radio",
                values = {
                    ["Default"] = "Default",
                    ["mUI"] = "mUI Style"
                },
                sorting = {"Default", "mUI"},
                width = 0.5,
                set = function(_, val)
                    if mUI.db.profile.chat.style == val then
                        return
                    end

                    mUI.db.profile.chat.style = val

                    if not mUI.db.profile.chat.enabled then
                        return
                    end

                    if val == "mUI" then
                        mUI:Reload("Change Chat Style")
                    else
                        mUI:Reload("Change Chat Style")
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.style
                end,
                order = 3
            },
            header2 = {
                name = "EditBox",
                type = "header",
                order = 4
            },
            position = {
                name = "Position",
                desc = "Set the Position of the Chat EditBox\n\n|cffffff00Info:|r Requires mUI Style",
                type = "select",
                values = {
                    ["top"] = "Top",
                    ["bottom"] = "Bottom"
                },
                sorting = {"top", "bottom"},
                width = 0.5,
                set = function(_, val)
                    mUI.db.profile.chat.settings.edit.position = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxPosition()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.edit.position
                end,
                order = 5
            },
            offset = {
                name = "Offset",
                desc = "Set the Offset for the Chat EditBox\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 0,
                max = 64,
                step = 1,
                set = function(_, val)
                    if mUI.db.profile.chat.settings.edit.offset == val then
                        return
                    end
                    mUI.db.profile.chat.settings.edit.offset = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxPosition()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.edit.offset
                end,
                order = 6
            },
            editalpha = {
                name = "Background Alpha",
                desc = "Set the Background Alpha for the Chat EditBox\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 0,
                max = 1,
                step = 0.1,
                set = function(_, val)
                    mUI.db.profile.chat.settings.edit.alpha = val
                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxAlpha()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.edit.alpha
                end,
                order = 7
            },
            editfontsize = {
                name = "Font Size",
                desc = "Set the Font Size for the Chat EditBox\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 10,
                max = 20,
                step = 1,
                get = function(info)
                    return mUI.db.profile.chat.settings.edit.font.size
                end,
                set = function(_, val)
                    if mUI.db.profile.chat.settings.edit.font.size == val then
                        return
                    end
                    mUI.db.profile.chat.settings.edit.font.size = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    -- Apply font settings after WoW updates fonts internally
                    C_Timer.After(0, function()
                        Chat.Module.Style:UpdateEditBoxFont()
                    end)
                end,
                order = 8
            },
            editfontoutline = {
                name = "Font Outline",
                desc = "Enable/Disable EditBox Font Outline\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                width = 0.7,
                set = function(_, val)
                    mUI.db.profile.chat.settings.edit.font.outline = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxFont()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.edit.font.outline
                end,
                order = 9
            },
            editfontshadow = {
                name = "Font Shadow",
                desc = "Enable/Disable EditBox Font Shadow\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                width = 0.7,
                set = function(_, val)
                    mUI.db.profile.chat.settings.edit.font.shadow = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxFont()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.edit.font.shadow
                end,
                order = 10
            },
            header3 = {
                name = "Chat Frame",
                type = "header",
                order = 11
            },
            chatalpha = {
                name = "Chat Background Alpha",
                desc = "Set the Background Alpha for the Chat\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 0,
                max = 1,
                step = 0.1,
                set = function(_, val)
                    if mUI.db.profile.chat.settings.chat.alpha == val then
                        return
                    end
                    mUI.db.profile.chat.settings.chat.alpha = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateChatBackgroundAlpha()

                    for i = 1, 10 do
                        Chat.Module.Style:ForMessageLinePool(i, "UpdateGradientBackgroundAlpha")
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.settings.chat.alpha
                end,
                order = 12
            },
            chatfontsize = {
                name = "Chat Font Size",
                desc = "Set the Font Size for the Chat\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 10,
                max = 20,
                step = 1,
                set = function(_, val)
                    if mUI.db.profile.chat.settings.chat.font.size == val then
                        return
                    end
                    mUI.db.profile.chat.settings.chat.font.size = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    for i = 1, 10 do
                        Chat.Module.Style:ForMessageLinePool(i, "UpdateHeight")
                    end

                    -- Apply font settings after WoW updates fonts internally
                    C_Timer.After(0, function()
                        Chat.Module.Style:UpdateMessageFonts()
                    end)
                end,
                get = function()
                    return mUI.db.profile.chat.settings.chat.font.size
                end,
                order = 13
            },
            spacer = {
                name = " ",
                type = "description",
                order = 14
            },
            chatfontoutline = {
                name = "Font Outline",
                desc = "Enable/Disable Chat Font Outline\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.chat.font.outline = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxFont()
                    Chat.Module.Style:UpdateMessageFonts()

                    for i = 1, 10 do
                        Chat.Module.Style:ForMessageLinePool(i, "UpdateHeight")
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.settings.chat.font.outline
                end,
                order = 15
            },
            chatfontshadow = {
                name = "Font Shadow",
                desc = "Enable/Disable Chat Font Shadow\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.chat.font.shadow = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateEditBoxFont()
                    Chat.Module.Style:UpdateMessageFonts()

                    for i = 1, 10 do
                        Chat.Module.Style:ForMessageLinePool(i, "UpdateHeight")
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.settings.chat.font.shadow
                end,
                order = 16
            },
            tooltips = {
                name = "Mouseover Tooltips",
                desc = "Enable/Disable Mouseover for Chat Tooltips\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.tooltips = val
                end,
                get = function()
                    return mUI.db.profile.chat.settings.tooltips
                end,
                order = 17
            },
            scroll = {
                name = "Scroll Buttons",
                desc = "Enable/Disable Chat Scroll Buttons\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.buttons.up_and_down = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    for i = 1, 10 do
                        Chat.Module.Style:ForChatFrame(i, "ToggleScrollButtons")
                    end
                    Chat.Module.Style:UpdateAllScrollButtons()
                end,
                get = function()
                    return mUI.db.profile.chat.settings.buttons.up_and_down
                end,
                order = 18
            },
            header4 = {
                name = "Fading",
                type = "header",
                order = 19
            },
            fading = {
                name = "Message Fading",
                desc = "Enable/Disable fading Chat Messages\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.fade.enabled = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    -- Apply fading setting to all chat frames
                    for i = 1, 10 do
                        Chat.Module.Style:ForChatFrame(i, "SetFading", val)
                        if val then
                            Chat.Module.Style:ForChatFrame(i, "SetTimeVisible", mUI.db.profile.chat.settings.fade.out_delay)
                        end
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.settings.fade.enabled
                end,
                order = 20
            },
            fadetabs = {
                name = "Tabs & Buttons Fading",
                desc = "Enable/Disable Chat Fading\n\n|cffffff00Info:|r Requires mUI Style",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.settings.dock.fade.enabled = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    Chat.Module.Style:UpdateTabAndButtonFading(val)
                end,
                get = function()
                    return mUI.db.profile.chat.settings.dock.fade.enabled
                end,
                order = 21
            },
            fadingtime = {
                name = "Fade Out Delay",
                desc = "Set the Fade Out Delay for the Chat Messages\n\n|cffffff00Info:|r Requires mUI Style",
                type = "range",
                min = 10,
                max = 240,
                step = 1,
                set = function(_, val)
                    mUI.db.profile.chat.settings.fade.out_delay = val

                    if not (mUI.db.profile.chat.style == "mUI" or Chat.Module:IsEnabled()) then
                        return
                    end

                    -- Apply new timer to all chat frames if fading is enabled
                    if mUI.db.profile.chat.settings.fade.enabled then
                        for i = 1, 10 do
                            Chat.Module.Style:ForChatFrame(i, "SetTimeVisible", val)
                        end
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.settings.fade.out_delay
                end,
                order = 22
            },
            header5 = {
                name = "Options",
                type = "header",
                order = 23
            },
            link = {
                name = "Link Copy",
                desc = "Make Links clickable to copy them",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.link = val

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

                    if val then
                        Chat.Module.Link:Enable()
                    else
                        Chat.Module.Link:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.link
                end,
                order = 24
            },
            copy = {
                name = "Copy Chat",
                desc = "Enable / Disable Copying of Chat History",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.copy = val

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

                    if val then
                        Chat.Module.Copy:Enable()
                    else
                        Chat.Module.Copy:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.copy
                end,
                order = 25
            },
            whisperalert = {
                name = "Whisper Alert",
                desc = "Enable / Disable Whisper Alerts",
                type = "toggle",
                set = function(_, val)
                    mUI.db.profile.chat.whisperalert = val

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

                    if val then
                        Chat.Module.WhisperAlert:Enable()
                    else
                        Chat.Module.WhisperAlert:Disable()
                    end
                end,
                get = function()
                    return mUI.db.profile.chat.whisperalert
                end,
                order = 26
            }
        }
    }

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