local this = class("cellComposeMaterial", G_UIModuleBase)
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local colorMap = {
  [-1] = "#df9b0a",
  [0] = "#a0aeb7",
  [1] = "#27ae89",
  [2] = "#3ab3ff",
  [3] = "#a264ff",
  [4] = "#ffbf35"
}

function this.bind()
  return {
    txt_num = "",
    img_icon = nil,
    img_iconFilter = nil,
    materialActive = false,
    emptyActive = true,
    bgColor = C_Color.white
  }
end

function this.methods()
  return {
    onClick_ItemIcon = function(self)
      local targetNum = self.bind.needNum
      if targetNum < 1 then
        targetNum = 1
      end
      self:emit("onClick_item", {
        itemId = self.itemId,
        targetNum = targetNum
      })
    end
  }
end

function this:refresh()
  self.bind.materialActive = self.bind.itemId ~= nil
  self.bind.emptyActive = self.bind.itemId == nil
  if self.bind.emptyActive then
    return
  end
  local item_tmp = L_ItemTplManager:getItemConfig(self.bind.itemType, self.bind.itemId)
  local commonItemTpl = _commonItemTpl:getTplById(self.bind.itemId)
  self.bind.img_iconFilter = commonItemTpl and _commonItemTpl:getFilterIcon(commonItemTpl) or ""
  self.bind.img_icon = item_tmp.icon
  local hadNum = self.bind.hadNum
  local needNum = self.bind.needNum
  if hadNum < needNum then
    hadNum = L_GameUtil.fillColor(L_GameUtil.itemShowNum(hadNum), L_Const.colorHtml.red005)
  end
  self.bind.txt_num = string.format("%s/%s", hadNum, needNum)
  local rarity = _commonItemTpl:getRarity(commonItemTpl)
  local _, color = C_ColorUtility.TryParseHtmlString(colorMap[rarity or -1])
  self.bind.bgColor = color
  self.targetNum = self.bind.needNum
  self.itemId = self.bind.itemId
end

function this:isSatisfy()
  if self.bind.hadNum and self.bind.needNum then
    return self.bind.hadNum >= self.bind.needNum
  end
  return true
end

return this
