local this = class("cellCurrency", G_UIModuleBase)
local _numType = {simple = 1, mul = 2}
local _resourceBarItemTpl = L_GameTpl:getResourceBarItemTpl()

function this.bind()
  return {
    imgIcon = nil,
    txtNum = "0",
    goBtnAdd = true
  }
end

function this.methods()
  return {
    onClick_add = function(self)
      local tpl = _resourceBarItemTpl:getTplById(self.bind.resId)
      local systemId = _resourceBarItemTpl:getSystem(tpl)
      L_SystemBreakManager:jumpTo(systemId)
    end,
    onClick_icon = function(self)
      local tpl = _resourceBarItemTpl:getTplById(self.bind.resId)
      local type = _resourceBarItemTpl:getResourceType(tpl)
      local id = _resourceBarItemTpl:getResourceID(tpl)
      L_ItemTplManager:showInfoTip(type, id)
    end
  }
end

function this:refresh()
  local tpl = _resourceBarItemTpl:getTplById(self.bind.resId)
  local type = _resourceBarItemTpl:getResourceType(tpl)
  local id = _resourceBarItemTpl:getResourceID(tpl)
  self:refreshNum()
  local item = L_ItemManager:parseItem(type, id)
  self.bind.imgIcon = item.icon
  self.bind.goBtnAdd = _resourceBarItemTpl:getIsAddSign(tpl)
end

function this:refreshNum()
  local tpl = _resourceBarItemTpl:getTplById(self.bind.resId)
  local type = _resourceBarItemTpl:getResourceType(tpl)
  local id = _resourceBarItemTpl:getResourceID(tpl)
  local numType = _resourceBarItemTpl:getNumType(tpl)
  local num = self:getNum(type, id)
  local showTxt = ""
  if numType == _numType.simple then
    showTxt = L_GameUtil.itemShowNum(num)
  elseif numType == _numType.mul then
    local limit = self:getLimitNum(type, id)
    showTxt = string.format("%s/%s", num, limit)
  end
  self.bind.txtNum = showTxt
end

function this:getNum(type, id)
  if type == L_Const.resType.currency then
    return L_PlayerStore:getCurrencyNum(id)
  elseif type == L_Const.resType.commonItem then
    return L_BagStore:getItemNum(id)
  end
end

function this:getLimitNum(type, id)
  if id == L_Const.currencyType.stamina and type == L_Const.resType.currency then
    local playerLevelTpl = L_GameTpl:getPlayerLevelTpl()
    return playerLevelTpl:getStamina(playerLevelTpl:getTplById(L_PlayerStore:getLv()))
  end
  return 0
end

return this
