local _, addon = ...

-- Prepare API object
addon.API = {}

-- Prepare Data objects
addon.data = {};
addon.data.quest = {};
addon.data.book = {};
addon.data.speech = {}
addon.data.encounter = {}

-- Expose object as global for other addon integration
CzechQuestsAddon = {}
CzechQuestsAddon.data = addon.data

local function SetDefault(key, default)
    if CzechQuestsAddon_Store.config[key] == nil then
        CzechQuestsAddon_Store.config[key] = default
    end
    return CzechQuestsAddon_Store.config[key]
end

local function InitStore()
    -- Init Store
    CzechQuestsAddon_Store = CzechQuestsAddon_Store or {}
    CzechQuestsAddon_Store.config = CzechQuestsAddon_Store.config or {}

    -- General Settings
    CzechQuestsAddon_Store.config.DEBUG_MODE = SetDefault("DEBUG_MODE", false)

    -- Quest Settings
    CzechQuestsAddon_Store.config.QUEST_ENABLED = SetDefault("QUEST_ENABLED", true)
    CzechQuestsAddon_Store.config.QUEST_DARK_MODE = SetDefault("QUEST_DARK_MODE", false)
    CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_SIZE = SetDefault("QUEST_HEADER_FONT_SIZE", 18)
    CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_SIZE = SetDefault("QUEST_TEXT_FONT_SIZE", 14)
    CzechQuestsAddon_Store.config.QUEST_HEADER_FONT_NAME = SetDefault("QUEST_HEADER_FONT_NAME", 'morpheus_cz.ttf')
    CzechQuestsAddon_Store.config.QUEST_TEXT_FONT_NAME = SetDefault("QUEST_TEXT_FONT_NAME", 'frizquadratatt_cz.ttf')
    CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA = SetDefault("QUEST_TEXTURE_ALPHA", 80)
    CzechQuestsAddon_Store.config.QUEST_TEXTURE_ALPHA_ONLY_MOVING = SetDefault("QUEST_TEXTURE_ALPHA_ONLY_MOVING", true)

    -- Speech Settings
    CzechQuestsAddon_Store.config.SPEECH_ENABLED = SetDefault("SPEECH_ENABLED", true)
    CzechQuestsAddon_Store.config.SPEECH_TEXT_FONT_SIZE = SetDefault("SPEECH_TEXT_FONT_SIZE", 13)
    CzechQuestsAddon_Store.config.SPEECH_TEXT_FONT_NAME = SetDefault("SPEECH_TEXT_FONT_NAME", 'frizquadratatt_cz.ttf')
    CzechQuestsAddon_Store.config.SPEECH_FRAME_WIDTH = SetDefault("SPEECH_FRAME_WIDTH", 320)
    CzechQuestsAddon_Store.config.SPEECH_ORIGINAL_WHEN_MISSING = SetDefault("SPEECH_ORIGINAL_WHEN_MISSING", false)
    CzechQuestsAddon_Store.config.SPEECH_MESSAGE_TEXTURE_ALPHA = SetDefault("SPEECH_MESSAGE_TEXTURE_ALPHA", 40)
    CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION_X = SetDefault("SPEECH_FRAME_POSITION_X", 0)
    CzechQuestsAddon_Store.config.SPEECH_FRAME_POSITION_Y = SetDefault("SPEECH_FRAME_POSITION_Y", 0)

    -- Encounter Settings
    CzechQuestsAddon_Store.config.ENCOUNTER_ENABLED = SetDefault("ENCOUNTER_ENABLED", true)
    CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_SIZE = SetDefault("ENCOUNTER_TEXT_FONT_SIZE", 12)
    CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_NAME = SetDefault("ENCOUNTER_TEXT_FONT_NAME", 'frizquadratatt_cz.ttf')
end

-- Event handler frame
local frame = CreateFrame("Frame")
local function OnEvent(self, event, addonName, ...)
    if event == "ADDON_LOADED" and addonName == 'CzechQuests' then
        InitStore()
        addon.API.InitOptions()
        addon.API.InitQuests()
        addon.API.InitSpeeches()
        if (WOW_PROJECT_ID ~= WOW_PROJECT_CLASSIC) then
            addon.API.InitEncounters()
        end
        self:UnregisterEvent("ADDON_LOADED")
    end
end

-- Register the event
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", OnEvent)