local base = UIAsyncNode
local MummyEffect = BaseClass("MummyEffect", base)
local SpriteRenderer = CS.UnityEngine.SpriteRenderer
local SuperTextMesh = CS.SuperTextMesh
local TouchObjectEventTrigger = CS.TouchObjectEventTrigger
local icon_army_path = "num/iconArmy"
local txt_army_count_path = "num/txtArmyCount"
local collider1_path = "num/Collider1"
local image_path = "num/add/Image"
local add_count_path = "num/add/Image/addCount"
local item_icon_path = "num/add/Image/ItemIconBg/ItemIcon"

function MummyEffect:OnCreate()
  base.OnCreate(self)
  local transform = self.transform
  if IsNull(transform) then
    return
  end
  transform:Set_localScale(1, 1, 1)
  transform:Set_localPosition(0, 3, 0)
  self.anim = transform:Find("num"):GetComponent(typeof(CS.SimpleAnimation))
  self.icon_army = transform:Find(icon_army_path):GetComponent(typeof(SpriteRenderer))
  self.txt_army_count = transform:Find(txt_army_count_path):GetComponent(typeof(SuperTextMesh))
  self.animRoot = transform:Find(image_path):GetComponent(typeof(CS.UnityEngine.CanvasGroup))
  self.animAddCount = transform:Find(add_count_path):GetComponent(typeof(CS.TextMeshProUGUIEx))
  self.animAddIcon = transform:Find(item_icon_path):GetComponent(typeof(CS.UnityEngine.UI.Image))
  self.animRoot.alpha = 0
end

function MummyEffect:OnDestroy()
  base.OnDestroy(self)
end

function MummyEffect:ReInit(mummyConvertId, mummyConvertCount)
  local oldSoldierId = self.mummyConvertId
  local oldSoldierCount = self.mummyConvertCount
  self.mummyConvertId = mummyConvertId
  self.mummyConvertCount = mummyConvertCount
  if self:AsyncLoadDone() then
    local anim_name = "Default"
    if oldSoldierId and mummyConvertCount < oldSoldierCount then
      self.animAddCount:Native_SetText("+" .. oldSoldierCount - mummyConvertCount)
      local _, _, showAddCount = WorldSimpleModeUtils.ShowMummyTranslate()
      if showAddCount then
        anim_name = "anim"
      else
        anim_name = "Default"
      end
    else
      self.animRoot.alpha = 0
      anim_name = "Default"
    end
    if self.anim:IsPlaying(anim_name) then
      self.anim:Rewind(anim_name)
    else
      self.anim:Play(anim_name)
    end
    self:UpdateData()
  end
end

function MummyEffect:UpdateData()
  if IsNotNull(self.txt_army_count) and self.mummyConvertCount then
    self.txt_army_count.text = self.mummyConvertCount
    local soldierMeta = DataCenter.SoldierDataManager:GetTemplate(self.mummyConvertId)
    if soldierMeta then
      local meta = DataCenter.ResourceItemDataManager:GetResourceItemTemplate(soldierMeta.resourceItemId)
      if meta then
        local iconPath = string.format(LoadPath.UIMainBubble, meta.bubble_icon)
        self.icon_army:LoadSprite(iconPath)
        self.animAddIcon:LoadSprite(iconPath)
      end
      soldierMeta = DataCenter.SoldierDataManager:GetTemplate(DataCenter.SoldierDataManager:GetSoldierIdByLevel(soldierMeta.lv, SoldierType.Mummy))
      if soldierMeta then
        meta = DataCenter.ResourceItemDataManager:GetResourceItemTemplate(soldierMeta.resourceItemId)
        if meta then
          local iconPath = string.format(LoadPath.UIMainBubble, meta.bubble_icon)
          self.animAddIcon:LoadSprite(iconPath)
        end
      end
    end
  end
end

return MummyEffect
