LeoDolmenRunner = {
    name = "LeoDolmenRunner",
    displayName = "Leo's Dolmen Runner",
    version = "1.1.0",
    chatPrefix = "|c39B027LeoDolmenRunner|r: ",
    debug = false,
    defaults = {
        hidden = true,
        runner = {
            autoTravel = true,
            autoDismiss = true,
            reapplyBuff = true
        },
        inviter = {
            maxSize = 24,
            autoKick = false,
            kickDelay = 10
        }
    }
}

local LDR = LeoDolmenRunner

function LDR.log(message)
    d(LDR.chatPrefix .. message)
end

function LDR.debug(message)
    if not LDR.debug then return end
    LDR.log("[D] " .. message)
end

local LastUpdateTimestamp = GetTimeStamp()

function LDR:Update()
    local currentTimestamp = GetTimeStamp()
    local tick = 1
    if GetDiffBetweenTimeStamps(currentTimestamp, LastUpdateTimestamp) >= 5 then
        tick = 5
        LastUpdateTimestamp = currentTimestamp
    end

    self.runner:Update(tick)
    self.inviter:Update(tick)
    self.ui:Update(tick)
end

function LDR:Initialize()
    local showButton, feedbackWindow = LibFeedback:initializeFeedbackWindow(LeoDolmenRunner,
    LDR.name,LeoDolmenRunnerWindow, "@LeandroSilva",
            {TOPRIGHT, LeoDolmenRunnerWindow, TOPRIGHT,-35,-2},
            {0,1000,10000,"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=Y9KM4PZU2UZ6A"},
            "If you find a bug, have a request, or simply wish to donate, send me a mail.")
    LDR.feedback = feedbackWindow
    LDR.feedback:SetDrawTier(DT_HIGH)

    LeoDolmenRunnerWindowTitle:SetText(LeoDolmenRunner.displayName .. " v" .. LeoDolmenRunner.version)

    SLASH_COMMANDS["/ldr"] = function(cmd)
        local options = {}
        local searchResult = { string.match(cmd,"^(%S*)%s*(.-)$") }
        for i,v in pairs(searchResult) do
            if (v ~= nil and v ~= "") then
                options[i] = string.lower(v)
            end
        end

        if #options == 0 or options[1] == "help" then
            LDR.log("Available options: ")
            d("    /ldr toggle: Toggles the main window")
            d("    /ldr show: Shows the main window")
            d("    /ldr hide: Hides the main window")
            d("    /ldr start: Starts the runner")
            d("    /ldr stop: Stops the runner")
            d("    /ldr cw: Changes direction to clockwise")
            d("    /ldr ccw: Changes direction to counter clockwise")
            d("    /ldr <letter>: Starts auto invite listening to the letter <letter>")
            d("    /ldr ai stop: Stops auto invite")
            return
        end

        if options[1] == "toggle" then LeoDolmenRunnerWindow:ToggleHidden()
        elseif options[1] == "show" then LeoDolmenRunnerWindow:SetHidden(false)
        elseif options[1] == "hide" then LeoDolmenRunnerWindow:SetHidden(true)
        elseif options[1] == "start" then LDR.runner:Start()
        elseif options[1] == "stop" then LDR.runner:Stop()
        elseif options[1] == "cw" then LDR.runner:CW()
        elseif options[1] == "ccw" then LDR.runner:CCW()
        elseif options[1] == "ai" and #options == 2 and options[2] == "stop" then LDR.inviter:Stop()
        elseif string.len(options[1]) == 1 then LDR.inviter:Start(options[1])
        else LDR.log("Listening message must be only 1 letter (eg: x).")
        end
    end

    if GetDisplayName() == "@LeandroSilva" then
        SLASH_COMMANDS["/rr"] = function(cmd)
            ReloadUI()
        end
    end

    self.runner:Initialize()
    self.inviter:Initialize()
    self.ui:Initialize()

    LDR.settingsPanel = LeoDolmenRunner_Settings:New()
    LDR.settingsPanel:CreatePanel()

    EVENT_MANAGER:RegisterForUpdate(LeoDolmenRunner.name, 1000, function() LDR:Update() end)
end

local function OnAddOnLoaded(event, addonName)
    if addonName ~= LDR.name then return end

    EVENT_MANAGER:UnregisterForEvent(LDR.name, EVENT_ADD_ON_LOADED)
    LDR.settings = LibSavedVars:NewAccountWide( LDR.name .. "_Data", "Account", LDR.defaults )

    LDR:Initialize()

    CALLBACK_MANAGER:RegisterCallback("LAM-PanelControlsCreated", OnSettingsControlsCreated)
    EVENT_MANAGER:RegisterForEvent(LDR.name, EVENT_START_FAST_TRAVEL_INTERACTION, function(eventId, ...) LDR.runner:OnFastTravelInteraction(...) end)
    EVENT_MANAGER:RegisterForEvent(LDR.name, EVENT_EXPERIENCE_GAIN, function(eventId, ...) LDR.runner:OnExperienceGain(...) end)
    EVENT_MANAGER:RegisterForEvent(LDR.name, EVENT_PLAYER_COMBAT_STATE, function(eventId, ...) LDR.runner:OnCombatState(...) end)

    LDR.log("started.")
end

EVENT_MANAGER:RegisterForEvent(LDR.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded)