local AllianceBuildBloodTip = BaseClass("AllianceBuildBloodTip")
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 blood_num_text_path = "PosGo/Bg/Bg2/buildBloodNum"
local blood_slider_path = "PosGo/Bg/Bg2/buildSlider"
local PositionDelta7 = Vector3.New(0, 0, 0)
local SliderSoliderLength = Vector2.New(2.81, 0.4)
local SliderBloodLength = Vector2.New(2.9, 0.17)
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)
  DataCenter.AllianceCityTipManager:SetCanShowFlag(self.data.cityId, true)
  self:RemoveTimer()
  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.soldier_icon = self.transform:Find(soldier_icon_path):GetComponent(typeof(CS.UnityEngine.SpriteRenderer))
  self.blood_num_text = self.transform:Find(blood_num_text_path):GetComponent(typeof(CS.SuperTextMesh))
  self.blood_slider = self.transform:Find(blood_slider_path):GetComponent(typeof(CS.UnityEngine.SpriteRenderer))
  self.curSoldierSize = Vector2.New(SliderSoliderLength.x, SliderSoliderLength.y)
  self.curBloodSize = Vector2.New(SliderBloodLength.x, SliderBloodLength.y)
  self.isDoAnim = false
  self.inOnClick = false
  self.curBloodPro = -1
  self.isClearSlider = false
  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.soldier_num_text = nil
  self.soldier_slider = nil
  self.soldier_icon = nil
  self.blood_num_text = nil
  self.blood_slider = nil
  self.curBloodPro = nil
end
local RemoveTimer = function(self)
  self.isDoAnim = false
  self.isClearSlider = false
  if self.__update_handle ~= nil then
    UpdateManager:GetInstance():RemoveUpdate(self.__update_handle)
  end
  self.__update_handle = nil
end
local StartShow = function(self, param)
  self.data = param
  self:UpdatePosition(self.data.serverId, self.data.pointId)
  DataCenter.AllianceCityTipManager:SetCanShowFlag(self.data.cityId, false)
  self.soldier_icon:LoadSprite("Assets/Main/Sprites/HeroIconsSmall/hero_icon_21001.png")
  self:RefreshSoldierSlider()
end
local SetIsOnClick = function(self)
  self.isOnClick = true
end
local SetAllianceBlood = function(self, pro)
  local value = math.min(pro, 1)
  if self.curBloodPro ~= value then
    self.curBloodPro = value
    self.curBloodSize.x = SliderBloodLength.x * value
    self.blood_slider.size = self.curBloodSize
    local temp = math.floor(value * self.data.maxBlood)
    self.blood_num_text.text = string.GetFormattedSeperatorNum(temp) .. "/" .. string.GetFormattedSeperatorNum(self.data.maxBlood)
  end
end
local StartShowBlood = function(self, startBlood, targetBlood, maxBlood, isBroken)
  self.curTime = 0
  self.startPro = startBlood / maxBlood
  self.targetPro = targetBlood / maxBlood
  self.data.maxBlood = maxBlood
  self.deltaPro = self.targetPro - self.startPro
  self.isBroken = isBroken
  self.isDoAnim = true
  self:Update()
end
local RefreshSoldierSlider = function(self)
  local value = math.min(self.data.soliderCurValue / self.data.soldierValue, 1)
  local realNum = value * self.data.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.data.soliderCurValue = curBlood
  self.data.soldierValue = maxBlood
  self:RefreshSoldierSlider()
end
local UpdatePosition = function(self, serverId, index)
  local worldPos = SceneUtils.TileIndexToWorld(index, ForceChangeScene.World, serverId) + PositionDelta7
  self.transform.position = worldPos
end
local Update = function(self)
  if self.isDoSoldierAnim ~= nil and self.isDoSoldierAnim == true then
    if IsNull(self.gameObject) then
      return
    end
    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
    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
  if self.isDoAnim ~= nil and self.isDoAnim == true then
    if IsNull(self.gameObject) then
      return
    end
    self.curTime = self.curTime + Time.deltaTime
    if self.curTime > 60 then
      self:RemoveTimer()
      self:CheckRemove()
    elseif self.curTime <= 2 then
      local changePro = self.curTime / 2
      local curPro = self.startPro + changePro * self.deltaPro
      self:SetAllianceBlood(curPro)
    elseif self.isBroken ~= nil and self.isBroken == true and self.isClearSlider == false then
      self.isClearSlider = true
      self:SetAllianceBlood(0)
      self:RemoveTimer()
      self:CheckRemove()
    end
  end
end
local TryRemove = function(self)
  self.isOnClick = false
  if self.isDoAnim == nil or self.isDoAnim == false then
    AllianceBuildBloodManager:GetInstance():RemoveOneEffect(self.data.bUuid)
  end
end
local CheckRemove = function(self)
  if (self.isOnClick == nil or self.isOnClick == false) and (self.isDoAnim == nil or self.isDoAnim == false) then
    AllianceBuildBloodManager:GetInstance():RemoveOneEffect(self.data.bUuid)
  end
end
AllianceBuildBloodTip.OnCreate = OnCreate
AllianceBuildBloodTip.OnDestroy = OnDestroy
AllianceBuildBloodTip.ComponentDefine = ComponentDefine
AllianceBuildBloodTip.ComponentDestroy = ComponentDestroy
AllianceBuildBloodTip.RemoveTimer = RemoveTimer
AllianceBuildBloodTip.UpdatePosition = UpdatePosition
AllianceBuildBloodTip.StartShowBlood = StartShowBlood
AllianceBuildBloodTip.Update = Update
AllianceBuildBloodTip.RefreshSoldierData = RefreshSoldierData
AllianceBuildBloodTip.TryRemove = TryRemove
AllianceBuildBloodTip.RefreshSoldierSlider = RefreshSoldierSlider
AllianceBuildBloodTip.SetAllianceBlood = SetAllianceBlood
AllianceBuildBloodTip.SetIsOnClick = SetIsOnClick
AllianceBuildBloodTip.StartShow = StartShow
AllianceBuildBloodTip.CheckRemove = CheckRemove
return AllianceBuildBloodTip
