local base = require("DataCenter.ZombieBattle.HpBar.HpBarCell")
local MultHpBarCell = BaseClass("MultHpBarCell", base)
local Const = require("Scene.LWBattle.Const")
local asset = "Assets/Main/Prefabs/LWBattle/NewMultHpBar.prefab"
local hp_fg1 = "Content/HpBar/fg1"
local hp_fg2 = "Content/HpBar/fg2"
local hp_text = "Content/txtNum/text"
local num_text = "Content/txtNum"
local Resource = CS.GameEntry.Resource
local UnitySlider = typeof(CS.UnityEngine.UI.Slider)
local TMPText = typeof(CS.TextMeshProUGUIEx)

function MultHpBarCell:__init(style, transform, height, barNum)
  self.barNum = barNum
end

function MultHpBarCell:LoadAndSetHp(curHp, maxHp)
  self.req = Resource:InstantiateAsync(asset)
  self.req:completed("+", function(req)
    local go = req.gameObject
    go.transform:SetParent(base.GetRootContainer())
    self.gameObject = go
    self.transform = go.transform
    self:InitComponent()
    self:SetHp(curHp, maxHp)
  end)
end

function MultHpBarCell:InitComponent()
  self.fg1 = self.transform:Find(hp_fg1):GetComponent(UnitySlider)
  self.fg2 = self.transform:Find(hp_fg2):GetComponent(UnitySlider)
  self.txtNum = self.transform:Find(num_text):GetComponent(TMPText)
  self.text = self.transform:Find(hp_text):GetComponent(TMPText)
  self:SetHp(1, 1)
end

function MultHpBarCell:SetHp(curHp, maxHp, curShieldValue)
  if not self.gameObject then
    return
  end
  if curShieldValue and 0 < curShieldValue then
    curHp = curHp + curShieldValue
  end
  local percent = curHp / maxHp
  if percent == 0 then
    self.fg1.value = 0
    self.fg2.value = 0
    self.text.text = "x0"
    return
  end
  local step = 1 / self.barNum
  local result = {}
  for i = 1, self.barNum do
    local value = math.min(step, percent)
    percent = percent - value
    if 0 < value then
      table.insert(result, value)
    end
  end
  local v1 = result[#result]
  local v2 = result[#result - 1]
  if v1 then
    local bar1, bar2
    if #result % 2 == 0 then
      bar1 = self.fg1
      bar2 = self.fg2
    else
      bar1 = self.fg2
      bar2 = self.fg1
    end
    bar1.transform:SetAsLastSibling()
    bar1.value = v1 / step
    if v2 then
      bar2.value = 1
    else
      bar2.value = 0
    end
  end
  self.text.text = "x" .. #result
  if curHp then
    self.txtNum.text = string.numbericFormationWithPrefix(curHp, 5, ",")
  else
    self.txtNum.text = ""
  end
end

function MultHpBarCell:SetHpText(text)
end

return MultHpBarCell
