local _, GW = ...
local AddToAnimation = GW.AddToAnimation
local lerpEaseOut = GW.lerpEaseOut
local lerp = GW.lerp
local numSpritesInAnimation = 15
local uniqueID  = 0

--[[
  TODO
    Implement Vertical bars (for healglobe etc)



  
  Bar:SetFillAmount(amount)
    Sets the fill amount of the bar to bar, Animates over time if smooth was set when bar was created

  Bar:GetFillAmount()
    returns the bars current fill amount. if animating returns the current animating value

  bar.barOnUpdate
      used for hooking custom onupdate function to the bars animation. Does nothing if the bar is not set to animate

  bar.speed
    used to set custom speed, default is 100 (pixels / second)


]]

local function getAnimationDuration(self,val1,val2,width)
  if width ==nil then width = 0 end
  local speed = self.speed or 100
  local t = (width * math.abs(val1 - val2)) / speed
  return t
end

local function SetFillAmount(self,value)

  local totalWidth = self:GetWidth()
  local barWidth = totalWidth * value



  self.fillAmount = value
  local bit = totalWidth / numSpritesInAnimation
  local spark = bit * math.floor(numSpritesInAnimation * value)
  local spark_current = (bit * (numSpritesInAnimation * (value)) - spark) / bit
  local bI = math.min(numSpritesInAnimation, math.max(1, math.floor((numSpritesInAnimation ) - (numSpritesInAnimation * spark_current))))
  local fill_threshold = (1/numSpritesInAnimation) * ( math.floor(numSpritesInAnimation * value) + 1)
  local maskTest = (totalWidth * fill_threshold) - bit

  if not self.bI or bI~=self.bI then
    local newMask = self.maskContainer["mask"..bI]
    self.bar:AddMaskTexture(newMask)
    if self.bI~=nil then
      local oldMask = self.maskContainer["mask"..self.bI]
      self.bar:RemoveMaskTexture(oldMask)
    end
    self.bI = bI
  end
  if self.fill_threshold~=fill_threshold then
      self.maskContainer:SetPoint("LEFT",self.bar,"LEFT",spark,0)
      self:SetValue(fill_threshold)
      self.fill_threshold = fill_threshold
  end
end

local function  barUpdate(self,delta)

  self.animatedTime = self.animatedTime + delta
  local newValue = lerpEaseOut(self.animatedStartValue,self.animatedValue,self.animatedTime/self.animatedDuration)
  SetFillAmount(self,newValue)
  if self.animatedTime>=self.animatedDuration then
  --  SetFillAmount(self,self.animatedValue)
    self:SetScript("OnUpdate",nil)
  end
  if self.barOnUpdate then
    self.barOnUpdate(self)
  end

end

local function onupdate_AnimateBar(self,value)
    self.animatedValue = value;
    self.animatedStartValue = GetFillAmount(self)
    self.animatedTime =0
    self.animatedDuration = getAnimationDuration(self,self.animatedStartValue , self.animatedValue,self:GetWidth())

    self:SetScript("OnUpdate",barUpdate)
end

local function hookStatusbarBehaviour(statusBar,smooth)

  if not AddToAnimation then
    AddToAnimation = GW.AddToAnimation
     round = GW.RoundInt
  end

  uniqueID = uniqueID + 1
  statusBar.maskContainer:SetSize(statusBar.bar:GetWidth() / numSpritesInAnimation,statusBar.bar:GetHeight())
  statusBar.fill_threshold = 0
  statusBar.GetFillAmount = GetFillAmount
  statusBar.SetFillAmount = smooth and onupdate_AnimateBar or SetFillAmount
  statusBar.uniqueID = uniqueID


end
GW.hookStatusbarBehaviour = hookStatusbarBehaviour
local function createNewStatusBar(name,parent,template,smooth)
  template = template or "GwStatusBarTemplate"
  local statusBar = CreateFrame("StatusBar",name, parent, template)
  hookStatusbarBehaviour(statusBar,smooth)
  return statusBar
end

GW.createNewStatusbar = createNewStatusBar


local function LoadStatusbarTest()
  local test1  = createNewStatusBar(UIParent,false)
  local test2  = createNewStatusBar(UIParent,true)
  test2:SetPoint("CENTER",0,-90)


    local delay = 0
    local delay2 = 0
    test1:SetScript("OnUpdate",function(self,delta)
      if delay2>GetTime() then
        return
      end
        delay2 = GetTime() + 3

        test1:SetFillAmount(math.abs(math.sin(GetTime())))
        test2:SetFillAmount(math.abs(math.sin(GetTime()*2)))
    end)


end

GW.LoadStatusbarTest = LoadStatusbarTest
