-- This file is part of XPCount
--
-- (C) 2015 Scott Yeskie (Sasky)
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

XPCount = {}

XPCount.init = function(_, _)
    EVENT_MANAGER:UnregisterForEvent("XPCount", EVENT_PLAYER_ACTIVATED)

    if XPCount.curZone ~= nil then
        d("Already init XPCount")
    end

    XPCount.resetCounter("zone")
    XPCount.zoneSet("zone")
    EVENT_MANAGER:RegisterForEvent("XPCountPlayerActivate", EVENT_PLAYER_ACTIVATED, XPCount.zoneChange)
    EVENT_MANAGER:RegisterForEvent("XPCountZoneChange", EVENT_ZONE_CHANGED, XPCount.zoneChange)
    EVENT_MANAGER:RegisterForUpdate("XPCountUIUpdate", 15000, XPCount.updateUI)
end

XPCount.zoneSet = function()
    XPCount.curZone = GetUnitZone("player")
    XPCount.zoneStart = GetTimeStamp()
    XPCount.zoneXpStart = GetUnitXP("player")
end

XPCount.resetCounter = function()
    XPCount.curZone = GetUnitZone("player")
    XPCount.clockStart = GetTimeStamp()
    XPCount.clockXpStart = GetUnitXP("player")
end



XPCount.zoneChange = function()
    local playerZone = GetUnitZone("player")
    if playerZone ~= XPCount.curZone then
        d("Enlightenment Pool: " .. (GetEnlightenedPool() or ""))
        if XPCount.curZone ~= "" then
            d("Exiting " .. XPCount.curZone .. " into " .. playerZone .. ":")
            XPCount.showStats()
        end

        XPCount.zoneSet()
    end
end

local function nn(val) if val == nil then return 0 end return val end

XPCount.updateUI = function()
    local time = GetTimeStamp()
    local elapsedZone = GetDiffBetweenTimeStamps(time, XPCount.zoneStart)
    local curXP = GetUnitXP("player")
    local xp = curXP - nn(XPCount.zoneXpStart)
    XPCountUIZoneTime:SetText(XPCount.formatTime(elapsedZone))
    XPCountUIZoneXP:SetText(xp .. "xp")

    local rate = 0
    if elapsedZone > 0 then
        rate = math.floor((xp/elapsedZone)*3600)
    end
    XPCountUIZoneRate:SetText(rate .. "/h")
end

SLASH_COMMANDS["/zzz"] = XPCount.updateUI

XPCount.formatTime = function(delta)
    local sec = delta % 60
    delta = (delta - sec) / 60
    local min = delta % 60
    local hrs = (delta - min) / 60

    local out = ""
    if hrs > 0 then
        out = out .. hrs .. "h "
    end
    out = out .. min .. "m "
    return out
end

XPCount.showStats = function()
    local time = GetTimeStamp()
    local curXP = GetUnitXP("player")
    local xp = curXP - XPCount.zoneXpStart
    local elapsed = GetDiffBetweenTimeStamps(time, XPCount.zoneStart)
    d("Time in zone: " .. XPCount.formatTime(elapsed))
    d("Experience earned: " .. xp .. "xp")
    d("Rate: " .. math.floor((xp/elapsed)*3600) .. "xp/h")
end

EVENT_MANAGER:RegisterForEvent("XPCount", EVENT_PLAYER_ACTIVATED, XPCount.init)