--------------------------
-- 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,-1)

  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.anim1:SetScript("OnFinished", function(self)
  --    self.durationRemaining = self.maxDuration / self.durationPercent
  --    print(self.durationRemaining)
  ----    ss.Debug.describe(self)
  --  end)

  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].modes[ss.profile.modeInfo[config].modeSpec][ss.profile.modeInfo[config].mode].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

function mtSwirl:UpdateGroup(parent, swirlConfig, swirlColor, size)
  local swirlTextures = ss.FillTextures.get(swirlConfig.groupId, swirlConfig.id)
  parent.swirlId      = swirlConfig.id
  local size          = ss.FillTextures.getSwirlSize(swirlConfig.id, size)

  parent.swirl1.texture:SetTexture(swirlTextures[1])
  parent.swirl1:SetHeight(size * 0.9)
  parent.swirl1:SetWidth (size * 0.9)
  parent.swirl1:SetSpeed(swirlConfig.speed, 2, 4)
  parent.swirl1.texture:SetVertexColor(ss:GetRGBAValues(swirlColor[1]))

  parent.swirl2.texture:SetTexture(swirlTextures[2])
  parent.swirl2:SetHeight(size * 0.9)
  parent.swirl2:SetWidth (size * 0.9)
  parent.swirl2:SetSpeed(swirlConfig.speed, 3, 5)
  parent.swirl2.texture:SetVertexColor(ss:GetRGBAValues(swirlColor[2] or swirlColor[1]))

  parent.swirl3.texture:SetTexture(swirlTextures[3])
  parent.swirl3:SetHeight(size * 0.9)
  parent.swirl3:SetWidth (size * 0.9)
  parent.swirl3:SetSpeed(swirlConfig.speed, 2, 3)
  parent.swirl3.texture:SetVertexColor(ss:GetRGBAValues(swirlColor[3] or swirlColor[1]))
end

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