local this = class("cellIconHomeBag", G_UIModuleBase)
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this.bind()
  return {
    active_select = false,
    toggle_isEmpty = false,
    active_showQualityLine = true,
    img_iconFilter = "",
    img_icon = "",
    img_colorBg = "",
    img_color = "",
    txt_name = "",
    ani_cell = "",
    list_star = {}
  }
end

function this.methods()
  return {
    onClick = function(self)
      if self._callback then
        self._callback()
      end
    end
  }
end

function this:open()
  self:refreshByItem(self._item, self._callback)
end

local itemData = {
  item_id = 0,
  build_id = 0,
  num = 0
}

function this:refreshByItem(itemData, cb, hideQualityLine)
  self._callback = cb
  self._item = itemData
  if not self.isBind then
    return
  end
  self.bind.active_showQualityLine = not hideQualityLine
  if itemData then
    self.bind.toggle_isEmpty = false
    local conf
    if itemData.item_id then
      conf = L_ItemTplManager:getCommonItem(itemData.item_id)
      local commonItemTpl = _commonItemTpl:getTplById(itemData.item_id)
      self.bind.img_iconFilter = _commonItemTpl:getFilterIcon(commonItemTpl)
      self.bind.list_star:clear()
      self.bind.list_star:insert_array(L_DataUtil.getEmptyModuleDatas(conf.quality + 1))
    elseif itemData.build_id then
      conf = L_ItemTplManager:getBuildingItem(itemData.build_id)
    end
    if conf then
      self.bind.img_icon = conf.icon
      self.bind.img_colorBg = string.format("UI/Atlas/CellIcon/tex_icon_raritybsqrtd_%s.png", conf.quality or 0)
      self.bind.img_color = string.format("UI/Atlas/CellIcon/tex_icon_raritybsqrt_%s.png", conf.quality or 0)
      self.bind.txt_name = tostring(itemData.num)
      return
    end
  end
  self.bind.toggle_isEmpty = true
end

function this:getRaycastGo()
  if self.isBind then
    return self.bindComponents.button.gameObject
  end
end

function this:setSelect(isSelect)
  self.bind.active_select = isSelect
end

function this:invokeDragAni(isRecover)
  if isRecover then
    self.bind.ani_cell = "anim_homebag_icon_unclick"
  else
    self.bind.ani_cell = "anim_homebag_icon_click"
  end
  self.bind.ani_cell = ""
end

return this
