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,
            setter
    )
end

local function CreateCheckbox(name, title, setter)
    local register = RegisterAddOnSettings(name, title)
    register:SetValueChangedCallback(setter)
    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, setter, nil, title)
    )
end

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

    local function CreateQuestCheckbox(name, title)
        CreateCheckbox(name, title, function (_, value) Update(name, value) end)
    end

    local function CreateQuestDropdown(name, title, items)
        CreateDropdown(name, title, items, function (value) Update(name, value) end)
    end

    local function CreateQuestSlider(name, title, min, max, step)
        CreateSlider(name, title, min, max, step, function (value) Update(name, value) end)
    end

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

    CreateQuestCheckbox("QUEST_ENABLED", "Zapnout")

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

    CreateQuestDropdown("QUEST_HEADER_FONT_NAME", "Pismo nadpisu", addon.API.GetFontContainer)
    CreateQuestDropdown("QUEST_TEXT_FONT_NAME", "Pismo textu", addon.API.GetFontContainer)

    CreateQuestSlider("QUEST_HEADER_FONT_SIZE", "Velikost nadpisu", 10, 30, 1)
    CreateQuestSlider("QUEST_TEXT_FONT_SIZE", "Velikost textu", 10, 30, 1)

    CreateQuestSlider("QUEST_TEXTURE_ALPHA", "Pruhlednost pozadi", 10, 100, 10)
    CreateQuestCheckbox("QUEST_TEXTURE_ALPHA_ONLY_MOVING", "Pruhlednost pouze pri chuzi")
end

local function InitSpeeches()
    local function CreateSpeechCheckbox(name, title)
        CreateCheckbox(name, title, function (_, value)
            CzechQuestsAddon_Store.config[name] = value
            addon.SpeechFrame:UpdateSettings()
        end)
    end

    local function CreateSpeechDropdown(name, title, items)
        CreateDropdown(name, title, items, function (value)
            CzechQuestsAddon_Store.config[name] = value
            addon.SpeechFrame:UpdateSettings()
        end)
    end

    local function CreateSpeechSlider(name, title, min, max, step)
        CreateSlider(name, title, min, max, step, function (value)
            CzechQuestsAddon_Store.config[name] = value
            addon.SpeechFrame:UpdateSettings()
        end)
    end

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

    CreateSpeechCheckbox("SPEECH_ENABLED", "Zapnout")

    CreateSpeechDropdown("SPEECH_TEXT_FONT_NAME", "Pismo *", addon.API.GetFontContainer)
    CreateSpeechSlider("SPEECH_TEXT_FONT_SIZE", "Velikost pisma *", 10, 30, 1)

    CreateSpeechSlider("SPEECH_FRAME_WIDTH", "Sirka okna", 200, 1000, 10)

    CreateSpeechCheckbox("SPEECH_ORIGINAL_WHEN_MISSING", "Original pokud není preklad ")

    CreateButton('Resetovat pozici okna', "RESET", function()
        CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION = { x = 0, y = 0}
        addon.SpeechFrame:SetPoint(
                "TOPLEFT", UIParent, "TOPLEFT",
                CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION.x,
                CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION.y
        )
    end)
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("CzechQuests")
    Options.category = category
    Options.layout = layout

    InitQuests()
    InitSpeeches()
    InitOthers()

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