local module = class("cellTalentRuneItem", G_UIModuleBase)
local talentRuneTpl = L_GameTpl:getTalentRuneTpl()

function module.bind()
  return {
    itemIcon = "",
    addMarkActive = true,
    runeName = "",
    itemColor = C_Color.white,
    contentAnchorPos = C_Vector2(0, 0),
    targetRingActive = false,
    redStatusActive = false,
    statusText = ""
  }
end

function module.methods()
  return {
    onClick_addMark = function(self)
      L_UI:open("pageHeroTalentInfo", self.runeData)
    end,
    onClick_ItemIcon = function(self)
      L_UI:open("pageHeroTalentInfo", self.runeData)
    end
  }
end

function module:open()
  L_BagStore:listenCallFunc(L_BagStore.event.refreshBag, self.onEvent_composeSuccess, self)
end

function module:close()
  self:removeTargetTimer()
  L_BagStore:unListenCallFunc(L_BagStore.event.refreshBag, self.onEvent_composeSuccess, self)
end

function module:onEvent_composeSuccess()
  self:refreshRedInfo()
end

function module:refresh()
  if not self.isBind or not self.bind.runeId then
    return
  end
  self.bind.contentAnchorPos = self.bind.runePosition
  self.runeData = {
    isEquiped = self.bind.isEquiped,
    runeId = self.bind.runeId,
    heroGuid = self.bind.heroGuid,
    slotId = self.bind.slotId,
    canEquip = not self.bind.isEquiped and self.bind.isPreview ~= true,
    currentRank = self.bind.curRank
  }
  self.bind.addMarkActive = not self.bind.isEquiped
  if self.bind.isEquiped then
    self.bind.itemColor = C_Color.white
  else
    self.bind.itemColor = C_Color(1, 1, 1, 0.3)
  end
  local runeTpl = talentRuneTpl:getTplById(self.bind.runeId)
  local itemId = talentRuneTpl:getRuneItem(runeTpl)
  local item = L_ItemTplManager:getCommonItem(itemId)
  self.itemId = itemId
  self.bind.runeName = item.name
  self.bind.itemIcon = item.icon
  self:setTargetRing()
  self:refreshRedInfo()
  if self.bind.isInit then
    self:cellPlayFillAnim("Filled", self.bind.isEquiped, 0)
    self.bindComponents.cellTalentAnimator:Play("Init", 1)
    self.bind.isInit = false
    return
  end
  if not self.bind.isEquiped and not self.bind.isPreview then
    self:cellPlayFillAnim("Filled", false, 0)
  else
    self:cellPlayFillAnim("Filled", true, (self.bind.slotId + 1) * 0.1)
    self.bind.firstFilled = true
  end
end

function module:refreshRedInfo()
  if not self.bind.isEquiped and not self.bind.isPreview then
    self.bind.redStatusActive = true
    self.bind.statusText = L_WordsTpl:getValue("ui_talent_detail_rune_state2")
    if L_BagStore:getItemNum(self.itemId) > 0 then
      self.bind.statusText = L_WordsTpl:getValue("ui_talent_rune_compose_06")
    elseif 0 < L_RuneStore:getMaxCanCompose(self.itemId) then
      self.bind.statusText = L_WordsTpl:getValue("ui_talent_rune_compose_05")
    end
  else
    self.bind.redStatusActive = false
  end
end

function module:setTargetRing()
  if not self.isBind then
    return
  end
end

function module:removeTargetTimer()
  if self.targetTimer ~= nil then
    Timer.remove(self.targetTimer)
    self.targetTimer = nil
  end
end

function module:cellPlayAnim(needInit, animName, layer, delayTime)
  if not self.isBind or not self.bindComponents.cellTalentAnimator then
    return
  end
  if needInit then
    self.bindComponents.cellTalentAnimator:Play("Init", 0)
  end
  Timer.once(delayTime, function()
    if not self.isBind or not self.bindComponents.cellTalentAnimator then
      return
    end
    self.bindComponents.cellTalentAnimator:Play(animName, layer)
  end, self)
end

function module:cellPlayFillAnim(animName, bool, delayTime)
  Timer.once(delayTime, function()
    if not self.isBind or not self.bindComponents.cellTalentAnimator then
      return
    end
    self.bindComponents.cellTalentAnimator:SetBool(animName, bool)
  end, self)
end

return module
