--## Author: Yuyuli ## Version: 1.0.53
local NPCTime = CreateFrame('frame')
--local Settings = {}
local timeFormat = "%H:%M, %d.%m"
local band = bit.band
local dontShowPhaseOnInit = true
local isClassic = (WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE)

local timeFormatter = CreateFromMixins(SecondsFormatterMixin);
timeFormatter:Init(1, SecondsFormatter.Abbreviation.Truncate);
local function AddMessage(...) _G.DEFAULT_CHAT_FRAME:AddMessage(strjoin(" ", tostringall(...))) end

local function AddColoredDoubleLine(tooltip, leftT, rightT, leftC, rightC, wrap)
  leftC = leftC or NORMAL_FONT_COLOR
  rightC = rightC or HIGHLIGHT_FONT_COLOR
  wrap = wrap or true
  tooltip:AddDoubleLine(leftT, rightT, leftC.r, leftC.g, leftC.b, rightC.r, rightC.g, rightC.b, wrap);
end

function NPCTime:PhaseAlert(e)
  local text = string.format("|cff9BFFA8 # %s New Connection|r", date("%H:%M"))
  AddMessage(text)
end

function NPCTime:OnEvent(e,...)
  if e == "ADDON_LOADED" and ... == addonName then
    NPCTimeDB = NPCTimeDB or {}
    Settings = NPCTimeDB
  elseif e == "CONSOLE_MESSAGE" then
    if not Settings.ShowPhasing then return end
    local message = ...
    if string.find(message, "new connection") then
      if not dontShowPhaseOnInit then
        self:PhaseAlert(e)
      end
    end
  end
end

function NPCTime:ProcessGUID(tooltip, guid)
  if not IsModifierKeyDown() then return end
  if not guid then return end

  local unitType, _, serverID, _, layerUID, unitID = strsplit("-", guid)
  local timeRaw = tonumber(strsub(guid, -6), 16)
  if timeRaw and (unitType == "Creature" or unitType == "Vehicle" or unitType == "GameObject") then
    local serverTime = GetServerTime()
    local spawnTime = ( serverTime - (serverTime % 2^23) ) + bit.band(timeRaw, 0x7fffff)

    local spawnIndex = bit.rshift(band(tonumber(strsub(guid, -10, -6), 16), 0xffff8), 3)

    --if Settings.ShowCurrentTime then
      --AddColoredDoubleLine(tooltip, TIMEMANAGER_TOOLTIP_REALMTIME , date(timeFormat, serverTime))
    --end

    if spawnTime > serverTime then
      spawnTime = spawnTime - ((2^23) - 1)
    end
    AddColoredDoubleLine(tooltip, serverID.."-"..layerUID, timeFormatter:Format((serverTime-spawnTime), false).." ("..date(timeFormat, spawnTime)..")")
    --if Settings.ShowLayer then
      --AddColoredDoubleLine(tooltip, "Layer", serverID.."-"..layerUID)
    --end

    --if Settings.ShowNPCID then
      --AddColoredDoubleLine(tooltip, "ID", unitID)
      --if spawnIndex > 0 then
        --AddColoredDoubleLine(tooltip, "Index", spawnIndex)
      --end
    --end

    tooltip:Show()
  end
end

function NPCTime:ShowTime(tooltip)
  --if Settings.UsingMod and not IsModifierKeyDown() then return end

  if not isClassic then
    local tooltipData = tooltip:GetPrimaryTooltipData();
    if tooltipData and issecretvalue and issecretvalue(tooltipData.guid) then
      return
    end
  end

  local _, unit = tooltip:GetUnit()
  if not unit then return end

  local guid = UnitGUID(unit)
  self:ProcessGUID(tooltip, guid)
end

function NPCTime:ShowObjectTime(tooltip)
  if isClassic then return end
  --if not Settings.ShowObjectTooltips then return end
  --if Settings.UsingMod and not IsModifierKeyDown() then return end

  local tokens = {"npc", "softinteract", "softtarget"}
  local guid
  for _, token in ipairs(tokens) do
    local _guid = UnitGUID(token)
    if _guid and not issecretvalue(_guid) and strsplit("-", _guid) == "GameObject" then
      guid = _guid
      break
    end
  end

  if guid then
    self:ProcessGUID(tooltip, guid)
  end
end

function NPCTime:OnLoad()
  --self:RegisterEvent("ADDON_LOADED")
  --self:RegisterEvent("CONSOLE_MESSAGE")
  C_Timer.After(1, function() dontShowPhaseOnInit = false end)

  if C_TooltipInfo and TooltipDataProcessor then
    TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, function(tooltip, tooltipData)
      if tooltip ~= GameTooltip then return end
      self:ShowTime(tooltip)
    end)
    TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Object, function(tooltip, tooltipData)
      if tooltip ~= GameTooltip then return end
      self:ShowObjectTime(tooltip)
    end)
  else
    GameTooltip:HookScript("OnTooltipSetUnit", function(...) self:ShowTime(...) end)
  end
  --self:SetScript("OnEvent", self.OnEvent)
end

NPCTime:OnLoad()