local _, addon = ...

local Options = {}
addon.Options = Options

local function RegisterAddOnSettings(name, title)
    return Settings.RegisterAddOnSetting(
            Options.category,
            "CzechQuestsAddon__" .. name,
            name,
            CzechQuestsAddon_Store.config,
            type(CzechQuestsAddon_Store.config[name]),
            title,
            CzechQuestsAddon_Store.config[name]
    )
end

local function RegisterProxySettings(name, title, setter)
    return Settings.RegisterProxySetting(
            Options.category,
            "CzechQuestsAddon__" .. name,
            type(CzechQuestsAddon_Store.config[name]),
            title,
            CzechQuestsAddon_Store.config[name],
            function()
                return CzechQuestsAddon_Store.config[name]
            end,
            function(value) setter(name, value) end
    )
end

local function CreateCheckbox(name, title, setter)
    local register = RegisterAddOnSettings(name, title)
    register:SetValueChangedCallback(function(_, value) setter(name, value) end)
    Settings.CreateCheckbox(Options.category, register)
end

local function CreateDropdown(name, title, items, setter)
    Settings.CreateDropdown(Options.category, RegisterProxySettings(name, title, setter), items)
end

local function CreateSlider(name, title, min, max, step, setter)
    local options = Settings.CreateSliderOptions(min, max, step)
    options:SetLabelFormatter(MinimalSliderWithSteppersMixin.Label.Right);
    Settings.CreateSlider(Options.category, RegisterProxySettings(name, title, setter), options)
end

local function CreateButton(title, label, setter)
    Options.layout:AddInitializer(
            CreateSettingsButtonInitializer(title, label, function(value) setter(name, value) end, nil, title)
    )
end

local function InitQuests()
    local function Update(name, value)
        CzechQuestsAddon_Store.config[name] = value
        addon.QuestFrame:UpdateSettings()
    end

    local layout = Options.layout
    layout:AddInitializer(CreateSettingsListSectionHeaderInitializer("Questy"))

    CreateCheckbox("QUEST_ENABLED", "Zapnout", Update)

    if (WOW_PROJECT_ID == WOW_PROJECT_CLASSIC) then
        CreateCheckbox("QUEST_DARK_MODE", "Pouzit tmavy rezim", Update)
    end

    CreateDropdown("QUEST_HEADER_FONT_NAME", "Pismo nadpisu", addon.API.GetFontContainer, Update)

    CreateDropdown("QUEST_TEXT_FONT_NAME", "Pismo textu", addon.API.GetFontContainer, Update)

    CreateSlider("QUEST_HEADER_FONT_SIZE", "Velikost nadpisu", 10, 30, 1, Update)

    CreateSlider("QUEST_TEXT_FONT_SIZE", "Velikost textu", 10, 30, 1, Update)

    CreateSlider("QUEST_TEXTURE_ALPHA", "Pruhlednost pozadi", 10, 100, 10, Update)

    CreateCheckbox("QUEST_TEXTURE_ALPHA_ONLY_MOVING", "Pruhlednost pouze pri chuzi", Update)
end

local function InitSpeeches()
    local function Update(name, value)
        CzechQuestsAddon_Store.config[name] = value
        addon.SpeechFrame:UpdateSettings()
    end

    local layout = Options.layout
    layout:AddInitializer(CreateSettingsListSectionHeaderInitializer("Bubliny"))

    CreateCheckbox("SPEECH_ENABLED", "Zapnout", Update)

    CreateDropdown("SPEECH_TEXT_FONT_NAME", "Pismo *", addon.API.GetFontContainer, Update)

    CreateSlider("SPEECH_TEXT_FONT_SIZE", "Velikost pisma *", 10, 30, 1, Update)

    CreateSlider("SPEECH_FRAME_WIDTH", "Sirka okna", 200, 1000, 10, Update)

    CreateCheckbox("SPEECH_ORIGINAL_WHEN_MISSING", "Original pokud není preklad", Update)

    CreateSlider("SPEECH_MESSAGE_TEXTURE_ALPHA", "Pruhlednost pozadi zpravy", 0, 100, 10, Update)

    CreateSlider("SPEECH_FRAME_POSITION_X", "Pozice okna X", 0, math.floor(GetScreenWidth()), 1, Update)

    CreateSlider("SPEECH_FRAME_POSITION_Y", "Pozice okna Y", 0, math.floor(GetScreenHeight()), 1, Update)

    CreateButton('Resetovat pozici okna', "RESET", function()
        CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION_X = 0
        CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION_Y = 0
        addon.SpeechFrame:UpdateSettings()
    end)

    CreateButton('Zobrazit testovaci zpravu', "Zobrazit", function()
        addon.API.AddSpeechMessage("Test", "Text message")
    end)

end

local function InitEncounters()
    local function Update(name, value)
        CzechQuestsAddon_Store.config[name] = value
        addon.EncounterFrame:UpdateSettings()
    end

    local layout = Options.layout
    layout:AddInitializer(CreateSettingsListSectionHeaderInitializer("Taktiky"))

    CreateCheckbox("ENCOUNTER_ENABLED", "Zapnout  *", Update)

    CreateDropdown("ENCOUNTER_TEXT_FONT_NAME", "Pismo *", addon.API.GetFontContainer, Update)

    CreateSlider("ENCOUNTER_TEXT_FONT_SIZE", "Velikost pisma *", 10, 30, 1, Update)
end

local function InitOthers()
    local layout = Options.layout
    layout:AddInitializer(CreateSettingsListSectionHeaderInitializer("Ostatni"))

    CreateCheckbox("DEBUG_MODE", "Zapnout DEBUG", function()
    end)
end

local function InitOptions()
    local category, layout = Settings.RegisterVerticalLayoutCategory("Czech Quests")
    Options.category = category
    Options.layout = layout

    InitQuests()
    InitSpeeches()
    if (WOW_PROJECT_ID ~= WOW_PROJECT_CLASSIC) then
        InitEncounters()
    end
    InitOthers()

    Settings.RegisterAddOnCategory(category)
end
addon.API.InitOptions = InitOptions
