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

local setmetatable = setmetatable
local CreateFrame, math = CreateFrame, math

local mtSwirl = {};

function mtSwirl:New(parent, texture, duration, size, color, index)
  local swirl = CreateFrame("Frame", ss._f..parent.configName.."_swirl"..index,parent)
  swirl:SetHeight(size)
  swirl:SetWidth (size)
  swirl:SetPoint ("Center",0,0)

  swirl.texture = swirl:CreateTexture()
  swirl.texture:SetAllPoints(swirl)
  swirl.texture:SetTexture  (texture)
  swirl.texture:SetBlendMode("ADD")
  swirl.texture:SetAlpha    (1)

  swirl.animGroup       = swirl:CreateAnimationGroup()
  swirl.animGroup.anim1 = swirl.animGroup:CreateAnimation("Rotation")
  swirl.animGroup.anim1:SetDegrees (360)
  swirl.animGroup.anim1:SetDuration(duration)
  swirl.animGroup.anim1.maxDuration       = duration
  swirl.animGroup.anim1.durationPercent   = 1
  swirl.animGroup.anim1.durationRemaining = 1

  swirl.animGroup:Play()
  swirl.animGroup:SetLooping    ("REPEAT")
  swirl.texture  :SetVertexColor(ss:GetRGBAValues(color))

  function swirl:SetSpeed(speed, low, high)
    local val = 11 - (speed or 5)
    local duration = math.random(val*low, val*high)

    self.animGroup.anim1:SetDuration(duration / self.animGroup.anim1.durationPercent)
    self.animGroup.anim1.maxDuration = duration
    self.speed = speed
  end

  function swirl:SetDurationPercent(percent)
    percent = (percent or 1) + 0.01

    if percent ~= self.animGroup.anim1.durationPercent then
--      print(self.maxDuration / percent)
--    print(self.animGroup.anim1:GetElapsed())
--      local progress = self.animGroup.anim1:GetProgress()

      self.animGroup.anim1:SetDuration(self.animGroup.anim1.maxDuration / percent)
      self.animGroup.anim1.durationPercent = percent
    end
  end

  function swirl:GetSpeed()
    return self.speed
  end

  return swirl
end

function mtSwirl:NewGroup(parent, config, color)
  local swirlConfig = ss.db[config].swirl
  parent.swirlId    = swirlConfig.id

  -- Get saved variables.
  local val           = 11 - (swirlConfig.speed or 5)
  local swirlTextures = ss.FillTextures.get         (swirlConfig.groupId, swirlConfig.id)
  local size          = ss.FillTextures.getSwirlSize(swirlConfig.id, ss.db[config].general.size)

  -- Determine texture rotation speeds for each new swril.
  local duration = math.random(val*2,val*4)
  local swirl1   = self:New(parent, swirlTextures[1], duration,size*0.9, color[1], 1)
  swirl1.speed   = swirlConfig.speed

  duration     = math.random(val*3,val*5)
  local swirl2 = self:New(parent, swirlTextures[2], duration,size*0.9, color[2] or color[1], 2)
  swirl2.speed = swirlConfig.speed

  duration     = math.random(val*2,val*3)
  local swirl3 = self:New(parent, swirlTextures[3], duration,size*0.9, color[3] or color[1], 3)
  swirl3.speed = swirlConfig.speed

  return swirl1, swirl2, swirl3
end

ss["Swirl"] = setmetatable({}, { __index = mtSwirl });
