local module = class("modulePetAttrFruitSelectNum", G_UIModuleBase)

function module.bind()
  return {text_num = ""}
end

function module.methods()
  return {
    onClickClear = function(self)
      if self.controlItem == nil then
        return
      end
      self:setSelectNumAndDoSelectNumChangeCallback(0)
    end,
    onClickDecrease = function(self)
      if self.controlItem == nil then
        return
      end
      self:setSelectNumAndDoSelectNumChangeCallback(self.selectNum - 1)
    end,
    onClickAdd = function(self)
      if self.controlItem == nil then
        return
      end
      if self:checkIfCanAddSelectNum() then
        self:setSelectNumAndDoSelectNumChangeCallback(self.selectNum + 1)
      end
    end,
    onClickMax = function(self)
      if self.controlItem == nil then
        return
      end
      if self:checkIfCanAddSelectNum() then
        local maxNumCurCanSelect = self:getMaxNumCurCanSelect()
        self:setSelectNumAndDoSelectNumChangeCallback(maxNumCurCanSelect)
      end
    end
  }
end

function module:initModule(params)
  self.controlItem = params.controlItem
  self.onSelectChangeNumChangeCbk = params.onSelectChangeNumChangeCbk
  self.selectNum = 0
  self.maxNum = self.controlItem:getItemNum()
  self.bind.text_num = tostring(0)
end

function module:setSelectNumAndDoSelectNumChangeCallback(num)
  self:setSelectNum(num)
  self:onSelectChangeNumChange()
end

function module:setSelectNum(num)
  num = self:clampSelectNum(num)
  self.selectNum = num
  self.bind.text_num = tostring(num)
end

function module:clampSelectNum(inputNum)
  if inputNum > self.maxNum then
    return self.maxNum
  elseif inputNum < 0 then
    return 0
  else
    return inputNum
  end
end

function module:onSelectChangeNumChange()
  self.onSelectChangeNumChangeCbk()
end

function module:checkIfCanAddSelectNum()
  return self.controlItem:checkIfCanAddSelectNum()
end

function module:getMaxNumCurCanSelect()
  return self.controlItem:getMaxNumCurCanSelect()
end

function module:resetSelectNum()
  self:setSelectNum(0)
end

function module:getSelectNum()
  return self.selectNum
end

return module
