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, tooltip)
    local register = RegisterAddOnSettings(name, title)
    register:SetValueChangedCallback(function(_, value) setter(name, value) end)
    Settings.CreateCheckbox(Options.category, register, tooltip)
end

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

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

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

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

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

    CreateDropdown("QUEST_TEXT_CZECH_ORIGINAL_FORMAT", "Format doslovneho prekladu", addon.API.GetCzechOriginalOptions, Update)
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", "Aktivni", 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("Rozhovory"))

    CreateCheckbox("SPEECH_ENABLED", "Aktivni", Update)

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

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

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

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

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

    CreateCheckbox("SPEECH_ORIGINAL_WHEN_MISSING", "Chybejici preklad", Update)

    CreateSlider("SPEECH_MESSAGE_TEXTURE_ALPHA", "Pruhlednost 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:Show()
        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", "Aktivni", Update, 'Zmema vyzaduje reload hry.')

    CreateDropdown("ENCOUNTER_TEXT_FONT_NAME", "Pismo", addon.API.GetFontContainer, Update, 'Zmema vyzaduje reload hry.')

    CreateSlider("ENCOUNTER_TEXT_FONT_SIZE", "Velikost pisma", 10, 30, 1, Update, 'Zmema vyzaduje reload hry.')
end

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

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

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

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

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