local this = class("cellSubmitItem", G_UIModuleBase)

function this.bind()
  return {
    activeEmpty = false,
    activeNumBg = false,
    txtNum = "",
    activeIcon = false,
    cellIcon = L_Const.ModuleInfo.CellIconBag,
    activeSelectNum = false,
    activeSelectNumBg = false,
    txtSelectNum = "",
    activeMask = false,
    activeNeedIcon = false,
    sprNeedIcon = "",
    activeTypeIcon = false,
    sprTypeIcon = "",
    activeAdd = false,
    btn = true,
    content = true
  }
end

function this.methods()
  return {
    onClick = function(self)
      if self.bind.clickCallback then
        self.bind.clickCallback(self)
      end
    end,
    onLongClick = function(self)
      if self.bind.itemType and self.bind.itemId then
        L_ItemTplManager:showInfoTip(self.bind.itemType, self.bind.itemId)
      end
    end,
    onClickRemove = function(self)
      if self.bind.clickRemoveCallback then
        self.bind.clickRemoveCallback(self)
      end
    end
  }
end

function this:resetData(option)
  self.bind.itemType = option.itemType
  self.bind.itemId = option.itemId
  self.bind.itemNum = option.itemNum
  self.bind.activeMask = option.activeMask
  self.bind.quality = option.quality
  self.bind.curCount = option.curCount
  self.bind.clickCallback = option.clickCallback
  self.bind.clickRemoveCallback = option.clickRemoveHandle
  self.bind.itemShowType = option.itemShowType
  self.bind.itemSubType = option.itemSubType
end

function this:refresh()
  self.bind.content = true
  if self.bind.itemShowType == L_Const.submitItemShowType.empty then
    self.modules.cellIcon:setContentActive(false)
    self.bind.activeIcon = true
    self.bind.btn = true
  elseif self.bind.itemShowType == L_Const.submitItemShowType.select then
    self.bind.activeNumBg = true
    self.bind.activeNeedIcon = true
    self.modules.cellIcon:setGeneralContent(self.bind.itemType, self.bind.itemId)
    self.bind.activeIcon = true
    if self.bind.itemNum then
      self.bind.txtNum = tostring(self.bind.itemNum)
    end
    self.bind.btn = true
  elseif self.bind.itemShowType == L_Const.submitItemShowType.submitTaget then
    self.bind.activeNumBg = true
    self.modules.cellIcon:setGeneralContent(self.bind.itemType, self.bind.itemId)
    self.bind.activeIcon = false
    local conf = L_ItemTplManager:getItemConfig(self.bind.itemType, self.bind.itemId)
    self.bind.sprNeedIcon = conf.icon
    self.bind.activeNeedIcon = true
    self.bind.activeEmpty = true
    self.bind.btn = true
    self:setCurCount(self.bind.curCount)
  elseif self.bind.itemShowType == L_Const.submitItemShowType.submitSubType then
    self.bind.activeNumBg = true
    local commonItemTypeTpl = L_GameTpl:getCommonItemTypeTpl()
    local tpl = commonItemTypeTpl:getTplById(self.bind.itemSubType)
    local typeIcon = tpl and commonItemTypeTpl:getIcon(tpl) or L_GameConstTpl:getValue("TASK_SUBMIT_ICON_DEFAULT")
    self.bind.sprTypeIcon = typeIcon
    self.bind.activeTypeIcon = true
    self.bind.activeEmpty = true
    self.bind.btn = true
    self:setCurCount(self.bind.curCount)
  elseif self.bind.itemShowType == L_Const.submitItemShowType.waitAdd then
    self.modules.cellIcon:setContentActive(false)
    self.bind.activeNumBg = false
    self.bind.activeSelectNum = false
    self.bind.activeTypeIcon = false
    self.bind.activeIcon = true
    self.bind.btn = false
    self.bind.itemId = nil
    self.bind.content = false
    self.select = false
  end
end

function this:setCurCount(curCount)
  if self.bind.itemShowType == L_Const.submitItemShowType.select then
    return
  end
  self.bind.curCount = curCount
  if self.bind.needCount then
    if self.bind.curCount < self.bind.needCount then
      self.bind.txtNum = string.format("<color=#ff878f>%s</color>/%s", self.bind.curCount, self.bind.needCount)
    else
      self.bind.txtNum = string.format("%s/%s", self.bind.curCount, self.bind.needCount)
    end
    if self.bind.itemShowType == L_Const.submitItemShowType.submitTaget then
      self.bind.activeEmpty = self.bind.curCount < 1
      self.bind.activeIcon = not self.bind.activeEmpty
    end
  end
end

function this:setSelect(select, num)
  self.select = select
  if select then
    if num then
      if self.bind.itemNum then
        num = math.min(self.bind.itemNum, num)
      end
      self.selectNum = num
      self.bind.txtSelectNum = tostring(num)
      self.bind.activeSelectNumBg = true
    else
      self.bind.activeSelectNumBg = false
    end
    self.bind.activeSelectNum = true
    return num
  else
    self.selectNum = nil
    self.bind.activeSelectNum = false
    return 0
  end
end

function this:getSelectNum()
  if not self.select or not self.selectNum then
    return 0
  end
  return self.selectNum
end

return this
