---------------------------------------------------------------------------------------------------
-- Element: Target Marker (Raid Target Marker)
---------------------------------------------------------------------------------------------------
local ADDON_NAME, Addon = ...

---------------------------------------------------------------------------------------------------
-- Imported functions and constants
---------------------------------------------------------------------------------------------------

-- Lua APIs

-- WoW APIs
local SetRaidTargetIconTexture = SetRaidTargetIconTexture

-- ThreatPlates APIs
local SubscribeEvent = Addon.EventService.Subscribe

---------------------------------------------------------------------------------------------------
-- Local variables
---------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------
-- Element code
---------------------------------------------------------------------------------------------------

local Element = Addon.Elements.NewElement("TargetMarker")

function Element.TargetMarkerUpdate(tp_frame)
  local unit, style = tp_frame.unit, tp_frame.style

  -- Bug https://wow.curseforge.com/projects/tidy-plates-threat-plates/issues/304 should be fixed with this
  -- as unit.TargetMarker should only have valid values (1-8)
  local target_marker = tp_frame.visual.TargetMarker
  local icon = (style.raidicon.show and unit.TargetMarkerIcon) or unit.MentorIcon
  if icon then
    if Addon.ExpansionIsAtLeastMidnight then
      SetRaidTargetIconTexture(target_marker, icon)
    else
      Addon:SetIconTexture(target_marker, "TargetMarker." .. icon)
    end
    target_marker:Show()
  else
    target_marker:Hide()
  end
end

-- Called in processing event: NAME_PLATE_CREATED
function Element.PlateCreated(tp_frame)
  -- Keep marker above the healthbar: healthbar uses a higher frame level than tp_frame.
  -- Using textframe as parent preserves overlay ordering across parent-frame changes.
  local target_marker = tp_frame.visual.textframe:CreateTexture(nil, "OVERLAY", nil, 7)
  target_marker:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
  tp_frame.visual.TargetMarker = target_marker
end

-- Called in processing event: NAME_PLATE_UNIT_ADDED
Element.PlateUnitAdded = Element.TargetMarkerUpdate

-- Called in processing event: NAME_PLATE_UNIT_REMOVED
--function Element.PlateUnitRemoved(tp_frame)
--  tp_frame.visual.ThreatGlow:Hide() -- done in UpdateStyle
--end

function Element.UpdateStyle(tp_frame, style, plate_style)
  local target_marker = tp_frame.visual.TargetMarker
  local target_marker_style = style.raidicon

  if plate_style == "None" or not target_marker_style.show then
    target_marker:Hide()
    return
  end

  target_marker:SetSize(target_marker_style.width, target_marker_style.height)
  target_marker:ClearAllPoints()
  target_marker:SetPoint(target_marker_style.anchor, tp_frame, target_marker_style.anchor, target_marker_style.x, target_marker_style.y)
  target_marker:SetShown(tp_frame.unit.TargetMarkerIcon ~= nil or tp_frame.unit.MentorIcon ~= nil)
end

SubscribeEvent(Element, "TargetMarkerUpdate", Element.TargetMarkerUpdate)
