local WorldBossBloodTip = BaseClass("WorldBossBloodTip")
local soldier_num_text_path = "PosGo/Bg/Bg1/soldierNum"
local soldier_slider_A_path = "PosGo/Bg/Bg1/solidierSliderA"
local soldier_slider_B_path = "PosGo/Bg/Bg1/solidierSliderB"
local soldier_slider_Mid_path = "PosGo/Bg/Bg1/solidierSliderMid"
local soldier_icon_path = "PosGo/Bg/Bg1/headIcon"
local ResourceManager = CS.GameEntry.Resource
local Localization = CS.GameEntry.Localization
local SliderSoliderLength = Vector2.New(2.81, 0.4)
local BloodColorList = {
  "uibuild_ruins_boss_pro_red",
  "uibuild_ruins_boss_pro_orange",
  "uibuild_ruins_boss_pro_yellow",
  "uibuild_ruins_boss_pro_green",
  "uibuild_ruins_boss_pro_blue",
  "uibuild_ruins_boss_pro_purple"
}
local OnCreate = function(self, go)
  if go ~= nil then
    self.request = go
    self.gameObject = go.gameObject
    self.transform = go.gameObject.transform
  end
  self:ComponentDefine()
end
local OnDestroy = function(self)
  self:ComponentDestroy()
end
local ComponentDefine = function(self)
  self.soldier_num_text = self.transform:Find(soldier_num_text_path):GetComponent(typeof(CS.SuperTextMesh))
  self.soldier_slider_A = self.transform:Find(soldier_slider_A_path):GetComponent(typeof(CS.UnityEngine.SpriteRenderer))
  self.soldier_slider_B = self.transform:Find(soldier_slider_B_path):GetComponent(typeof(CS.UnityEngine.SpriteRenderer))
  self.soldier_slider_Mid = self.transform:Find(soldier_slider_Mid_path):GetComponent(typeof(CS.UnityEngine.SpriteRenderer))
  self.curSoldierSize = Vector2.New(SliderSoliderLength.x, SliderSoliderLength.y)
  self.cacheHpAImg = ""
  self.cacheHpBImg = ""
  self.isDoSoldierAnim = false
  self.lastPercent = 0
  self.targetPercent = 0
  self.deltaPercent = 0
  self.curSoldierTime = 0
  
  function self.__update_handle()
    self:Update()
  end
  
  UpdateManager:GetInstance():AddUpdate(self.__update_handle)
end
local ComponentDestroy = function(self)
  self:RemoveTimer()
end
local RemoveTimer = function(self)
  UpdateManager:GetInstance():RemoveUpdate(self.__update_handle)
  self.__update_handle = nil
end
local ShowMarchInfo = function(self, marchInfo)
  self.data = marchInfo
  self.oneBloodSize = DataCenter.ActBossDataManager.oneBloodSize
  if marchInfo ~= nil and marchInfo:GetMarchType() == NewMarchType.PUZZLE_BOSS then
    self.oneBloodSize = LuaEntry.DataConfig:TryGetNum("activity_puzzle", "k5")
  end
  self.soliderCurValue = self.data:GetHP()
  self.soldierValue = self.data:GetMaxHP()
  self.totalCount = math.floor(self.soldierValue / math.max(self.oneBloodSize, 100))
  self:UpdatePosition()
  self:RefreshSoldierSlider()
end
local UpdatePosition = function(self)
  local worldPos = self.data:GetMarchCurPos()
  self.transform.position = worldPos
end
local RefreshSoldierSlider = function(self)
  local value = math.min(self.soliderCurValue / self.soldierValue, 1)
  local realNum = value * self.totalCount
  local ANum = math.floor(realNum)
  local BNum = math.ceil(realNum)
  local percent = math.min(realNum - ANum, 1)
  if ANum == BNum then
    percent = 1
  end
  self.lastPercent = self.targetPercent
  self.targetPercent = percent
  if percent <= 0 then
    self.targetPercent = 0
  end
  if 0 < ANum then
    local count = #BloodColorList
    local posA = ANum % count
    local posB = BNum % count
    if posA <= 0 then
      posA = count
    end
    if posB <= 0 then
      posB = count
    end
    local sprA = BloodColorList[posA]
    if self.cacheHpAImg ~= sprA then
      self.cacheHpAImg = sprA
      self.soldier_slider_A:LoadSprite("Assets/Main/Sprites/UI/UIBuildBtns/" .. sprA)
    end
    local sprB = BloodColorList[posB]
    if self.cacheHpBImg ~= sprB then
      self.cacheHpBImg = sprB
      self.soldier_slider_B:LoadSprite("Assets/Main/Sprites/UI/UIBuildBtns/" .. sprB)
    end
    self.curSoldierSize.x = SliderSoliderLength.x * 1
    self.soldier_slider_A.size = self.curSoldierSize
    self.curSoldierSize.x = SliderSoliderLength.x * percent
    self.soldier_slider_B.size = self.curSoldierSize
    self.targetPercent = percent
  elseif 0 < BNum then
    local count = #BloodColorList
    local posB = BNum % count
    if posB <= 0 then
      posB = count
    end
    self.cacheHpAImg = ""
    local sprB = BloodColorList[posB]
    if self.cacheHpBImg ~= sprB then
      self.cacheHpBImg = sprB
      self.soldier_slider_B:LoadSprite("Assets/Main/Sprites/UI/UIBuildBtns/" .. sprB)
    end
    self.curSoldierSize.x = 0
    self.soldier_slider_A.size = self.curSoldierSize
    self.curSoldierSize.x = SliderSoliderLength.x * percent
    self.soldier_slider_B.size = self.curSoldierSize
    self.targetPercent = percent
  else
    self.cacheHpAImg = ""
    self.cacheHpBImg = ""
    self.curSoldierSize.x = 0
    self.soldier_slider_A.size = self.curSoldierSize
    self.soldier_slider_B.size = self.curSoldierSize
    self.targetPercent = 0
  end
  if BNum <= 0 then
    self.soldier_num_text.text = ""
  else
    self.soldier_num_text.text = "x" .. BNum
  end
  if self.targetPercent > self.lastPercent then
    self.lastPercent = self.targetPercent
  end
  self.curSoldierTime = 0
  self.deltaPercent = self.targetPercent - self.lastPercent
  self.curSoldierSize.x = SliderSoliderLength.x * self.lastPercent
  self.soldier_slider_Mid.size = self.curSoldierSize
  if self.deltaPercent ~= 0 then
    self.isDoSoldierAnim = true
    self:Update()
  end
end
local RefreshSoldierData = function(self, curBlood, maxBlood)
  self.soliderCurValue = curBlood
  self.soldierValue = maxBlood
  self.totalCount = math.floor(self.soldierValue / math.max(self.oneBloodSize, 100))
  self:RefreshSoldierSlider()
end
local Update = function(self)
  if self.isDoSoldierAnim ~= nil and self.isDoSoldierAnim == true then
    self.curSoldierTime = self.curSoldierTime + Time.deltaTime
    if self.curSoldierTime > 0.2 then
      self.curSoldierSize.x = SliderSoliderLength.x * self.targetPercent
      self.soldier_slider_Mid.size = self.curSoldierSize
      self.isDoSoldierAnim = false
    else
      local changePro = self.curSoldierTime / 0.2
      local curPro = self.lastPercent + changePro * self.deltaPercent
      self.curSoldierSize.x = SliderSoliderLength.x * curPro
      self.soldier_slider_Mid.size = self.curSoldierSize
    end
  end
end
WorldBossBloodTip.OnCreate = OnCreate
WorldBossBloodTip.OnDestroy = OnDestroy
WorldBossBloodTip.ComponentDefine = ComponentDefine
WorldBossBloodTip.ComponentDestroy = ComponentDestroy
WorldBossBloodTip.ShowMarchInfo = ShowMarchInfo
WorldBossBloodTip.UpdatePosition = UpdatePosition
WorldBossBloodTip.RefreshSoldierSlider = RefreshSoldierSlider
WorldBossBloodTip.RefreshSoldierData = RefreshSoldierData
WorldBossBloodTip.Update = Update
WorldBossBloodTip.RemoveTimer = RemoveTimer
return WorldBossBloodTip
