local colorMax = {173, 255, 95}
local colorMin = {246, 60, 60}

HealthBar = class(Object, function(o, x, y, w, h)
  Object.init(o, x, y, w, h)
  o.sprite = sprites['tiles']['white.png']
  o.drawType = 'stretched'

  o.padding = 4
  o.padding = o.padding* r_scale/2

  o.background = Object(x, y, w, h)
  o.background.sprite = o.sprite
  o.background.drawType = o.drawType
  o.background:setColor(0,0,0,.25*255)

  o.centerbg = Object(x+o.padding, y+o.padding, w-o.padding*2, h-o.padding*2)
  o.centerbg.sprite = o.sprite
  o.centerbg.drawType = o.drawType
  o.centerbg:setColor(0,0,0,.49*255)

  o.valueobj = Object(x+o.padding, y+o.padding, w-o.padding*2, h-o.padding*2)
  o.valueobj.sprite = o.sprite
  o.valueobj.drawType = o.drawType
  o.valueobj:setColor(173, 255, 95)


  o.min = 0
  o.max = 1
  o.value = 0.5
end)

function HealthBar:draw()
  local valPercent = math.max(0, (self.value-self.min)/self.max)
  self.background:draw()
  self.centerbg:draw()
  self.valueobj.w = (self.w-self.padding*2)*valPercent
  self.valueobj:draw()
end
function HealthBar:setValue(value)
  self.value = value
  local valPercent = math.max(0, (self.value-self.min)/self.max)
  -- set a lerped value for the color
  print("valpercent", valPercent)
  local lcolor = {}
  for i,v in ipairs(colorMax) do
    lcolor[i] = valPercent*v + (1-valPercent)*colorMin[i]
  end
  self.valueobj:setColor(lcolor[1], lcolor[2], lcolor[3])
end
function HealthBar:setPosition(x, y)
  Object.setPosition(self, x, y)
  if self.background then
    self.background:setPosition(x, y)
  end
  if self.centerbg then
    self.centerbg:setPosition(x+self.padding, y+self.padding)
  end
  if self.valueobj then
    self.valueobj:setPosition(x+self.padding, y+self.padding)
  end
end
function HealthBar:setScrollFactor(x,y)
  local sf = Vec2(x,y)
  if self.background then
    self.background:setScrollFactor(sf)
  end
  if self.centerbg then
    self.centerbg:setScrollFactor(sf)
    
  end
  if self.valueobj then
    self.valueobj:setScrollFactor(sf)
  end
end
-- mainState = WrapperState(Group());
-- local lss = LevelSelectState();
-- mainState:changeState(lss)
-- lss.levelButtons.members[3].onPress()
-- lss.playButton.onPress()