-------------------------------------
-- Sphere Resource Monitor Creator --
-------------------------------------
local AddOnName, nsVars = ...
local ss = nsVars.SS

local setmetatable = setmetatable
local UnitGetTotalAbsorbs, UnitInVehicle = UnitGetTotalAbsorbs, UnitInVehicle
local UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax = UnitHealth, UnitHealthMax, UnitPower, UnitPowerMax

local mtResourceMonitor = {
  monitorResource = function(display)
    local unit = display.config.unit

    if (display.config.unit == "vehicle") and (not UnitInVehicle("player")) then
      ss.Layout:OnPlayerExitedVehicle()
    end

    local maxResourceVal = 0
    local currentVal     = 0
    local newPercent     = nil

    -- Update the micro spheres.
    display:UpdateMicroSpheres()

    -- Get first value.
    if (display.configName == "health") or (display.configName == "pet") or (display.configName == "vehicle") then
      maxResourceVal = UnitHealthMax(unit)
      currentVal     = UnitHealth   (unit)
    else
      maxResourceVal = UnitPowerMax(display.config.unit, display.config.unitId)
      currentVal     = UnitPower   (display.config.unit, display.config.unitId)
    end

    if display.config.secondaryUnitId then
      -- Update the first value.
      display.config.previousValue, newPercent = display.config.onMonitorUpdate(
        currentVal,
        maxResourceVal,
        display.config.realHeight,
        display.font1,
        display.fillingLeft,
        display.config.previousValue,
        nil,
        nil,
        display.config.passed)

      -- Get second value.
      if (display.configName == "pet") or (display.configName == "vehicle") then
        maxResourceVal = UnitPowerMax(display.config.unit)
        currentVal     = UnitPower   (display.config.unit)
      else
        maxResourceVal = UnitPowerMax(display.config.unit, display.config.secondaryUnitId)
        currentVal     = UnitPower   (display.config.unit, display.config.secondaryUnitId)
      end

      -- Update the second value.
      display.config.previousSecondaryValue = display.config.onMonitorUpdateSecondary(
        currentVal,
        maxResourceVal,
        display.config.realHeight,
        display.font2,
        display.fillingRight,
        display.config.previousSecondaryValue,
        nil,
        nil,
        display.config.passed)
    else
      -- Update the first value.
      display.config.previousValue, newPercent = display.config.onMonitorUpdate(
        currentVal,
        maxResourceVal,
        display.config.realHeight,
        display.font1,
        display.filling,
        display.config.previousValue,
        display.font2,
        display.config.textFormat,
        display.config.passed)

      if display.config.absorbUnitId then
        -- Update the absorb value.
        currentVal = UnitGetTotalAbsorbs(unit)
        display.config.previousAbsorbValue = display.config.onMonitorUpdate(
          currentVal,
          maxResourceVal,
          display.config.realHeight,
          nil,
          display.fillingAbsorb,
          display.config.previousAbsorbValue,
          nil,
          display.config.textFormat,
          display.config.passed)
      end
    end

  if newPercent then
    display.swirl1:SetAlpha(newPercent)
    display.swirl2:SetAlpha(newPercent)
    display.swirl3:SetAlpha(newPercent)

    -- TODO: Prevent animation from restarting when updating duration
    --    display.swirl1:SetDurationPercent(newPercent)
    --    display.swirl2:SetDurationPercent(newPercent)
    --    display.swirl3:SetDurationPercent(newPercent)
  end

    -- Set the flag to start skipping redundant processing.
    display.config.passed = true
  end,
};

function mtResourceMonitor:Watch()
  return self.monitorResource
end

ss["ResourceMonitor"] = {}

function ss.ResourceMonitor:New()
  local instance = {}
  setmetatable(instance, mtResourceMonitor)
  mtResourceMonitor.__index = mtResourceMonitor

  return instance
end
