-------------------------
-- Sphere Base Creator --
-------------------------
local addon, ns = ...
local ss = ns.SS

local setmetatable = setmetatable
local CreateFrame, GameTooltip_SetDefaultAnchor = CreateFrame, GameTooltip_SetDefaultAnchor
local GameTooltip, UIParent = GameTooltip, UIParent
local math, cos, sin = math, cos, sin

local mtBase = {
};

function mtBase:New(parent, config, displayMode)
  local template    = (displayMode == "preview") and nil or "SecureActionButtonTemplate"
  local generalInfo = ss.db[config].general

  local base = CreateFrame("Button", ss._f..config.."base", parent,template)
  base:SetHeight(ss.global.general.SphereSize / generalInfo.scale)
  base:SetWidth (ss.global.general.SphereSize / generalInfo.scale)

  if displayMode == "active" then
    base:SetPoint         (generalInfo.anchor, generalInfo.posX, generalInfo.posY)
    base:RegisterForClicks("AnyUp")
    base:SetAttribute     ("type1","target")
    base:SetAttribute     ("target","unit")
    base:SetAttribute     ("type2","togglemenu")
    base:SetAttribute     ("togglemenu", generalInfo.unit)
    base:SetAttribute     ("unit",generalInfo.unit)
  
    base.tooltip = ss.TooltipInfo:New(generalInfo.unit)

    base:SetScript("OnEnter", function(self)
      if self:GetAlpha() > 0 then
        GameTooltip_SetDefaultAnchor(GameTooltip,UIParent)
        GameTooltip:SetUnit(generalInfo.unit)
        GameTooltip:Show()
      end
    end)
    base:SetScript("OnLeave",function()
      if GameTooltip:IsVisible() then
        GameTooltip:Hide()
      end
    end)
  else
    base:SetFrameStrata("HIGH")
    base:SetAllPoints(parent)
  end

  return base
end

local adjustToBoundary

adjustToBoundary = function(val, min, max)
  local newval = val
  if newval < min then
    newval = adjustToBoundary(min + (min - newval), min, max)
  elseif newval > max then
    newval = adjustToBoundary(max - (newval - max), min, max)
  end
  return newval
end

-- Health sphere micro sphere location
local resourceGroupPoints = {
  [1] = {
    [1] = -471.22714233398,
    [2] = 133.99948120117,
  },
  [2] = {
    [1] = -447.04940795898,
    [2] = 159.79916381836,
  },
  [3] = {
    [1] = -417.14456176758,
    [2] = 174.73603820801,
  },
  [4] = {
    [1] = -382.66906738281,
    [2] = 178.61676025391,
  },
  [5] = {
    [1] = -348.99588012695,
    [2] = 170.17042541504,
  },
  [6] = {
    [1] = -319.02352905273,
    [2] = 149.04713439941,
  },
  [7] = {
    [1] = -299.69085693359,
    [2] = 120.04951477051,
  },
  [8] = {
    [1] = -291.75930786133,
    [2] = 85.393577575684,
  },
  [9] = {
    [1] = -296.63027954102,
    [2] = 50.645896911621,
  },
  [10] = {
    [1] = -312.75939941406,
    [2] = 20.778709411621,
  },
};

function mtBase:NewGroupItem(parent, config, index, region)
  local generalInfo = ss.db[config].general
  local base        = CreateFrame("Button", ss._f..config.."_microsphere"..index.."_base", parent,nil)

  base:SetHeight(30 / generalInfo.scale)
  base:SetWidth (30 / generalInfo.scale)

  if resourceGroupPoints[index] then
    if region and region == "secondary" then
      base:SetPoint("BOTTOM", -(resourceGroupPoints[index][1] + 389), resourceGroupPoints[index][2] - 22)
    else
      base:SetPoint("BOTTOM", resourceGroupPoints[index][1] + 389,    resourceGroupPoints[index][2] - 22)
    end
  else
    local adjust = index - 1

    if adjust < 0 then
      adjust = -adjust
    end

    local yAdjustment = ((adjust*15))
    local xAdjustment = index - 1
    local ypos        = 285 + yAdjustment
    local xpos        = 360 + (xAdjustment * 20)
    xpos              = adjustToBoundary(xpos, 310, 480)
    ypos              = adjustToBoundary(ypos, 110, 290)

    local xmin, ymin = parent:GetLeft()-45, parent:GetBottom()+45
    xpos             = xmin-xpos/UIParent:GetScale()+(parent:GetWidth()/2)
    ypos             = ypos/UIParent:GetScale()-ymin-(parent:GetWidth()/2)

    local locationAngle = math.deg(math.atan2(ypos,xpos))
    local x             = 60-(((parent:GetWidth()+45)/2)*cos(locationAngle))
    local y             = ((((parent:GetWidth()+45)/2)*sin(locationAngle))-60)

    base:SetPoint("TOPLEFT",parent,"TOPLEFT", x, y)
  end

  return base
end

ss["BaseCreator"] = setmetatable({}, { __index = mtBase });
