local this = {}

function this:req_setProductPet(blockId, slot, guid, cb)
  local data = {
    block_id = blockId,
    slot = slot,
    pet_guid = guid,
    work_type = 1
  }
  L_Net:sendMessage(MsgGenCode.CSProtoRanchSetPetOption, data, cb)
end

function this:req_setGatherPet(blockId, guid, cb, isOutHomeHub)
  local data = {
    block_id = blockId,
    slot = 0,
    pet_guid = guid,
    work_type = 2,
    out_hub = isOutHomeHub
  }
  L_Net:sendMessage(MsgGenCode.CSProtoRanchSetPetOption, data, cb)
end

function this:req_pickDropItem(blockId, guid, isEgg)
  if self.data.cacheBlockId ~= blockId then
    if #self.data.cacheDropIds > 0 or 0 < #self.data.cacheEggIds then
      local data = {
        block_id = self.data.cacheBlockId,
        reward_guids = self.data.cacheDropIds,
        egg_guids = self.data.cacheEggIds
      }
      L_Net:sendMessage(MsgGenCode.CSProtoRanchPlayerGetReward, data)
    end
    self.data.cacheDropIds = {}
    self.data.cacheEggIds = {}
    self.data.cacheBlockId = 0
    L_TimerManager:stopTimer(self, "picking")
  end
  if self.data.cacheBlockId == 0 then
    self.data.cacheBlockId = blockId
    L_TimerManager:newOrResetTimer(self, "picking", function()
      if self.data.cacheBlockId == 0 then
        return
      end
      local data = {
        block_id = self.data.cacheBlockId,
        reward_guids = self.data.cacheDropIds,
        egg_guids = self.data.cacheEggIds
      }
      L_Net:sendMessage(MsgGenCode.CSProtoRanchPlayerGetReward, data)
      self.data.cacheDropIds = {}
      self.data.cacheEggIds = {}
      self.data.cacheBlockId = 0
    end, 2)
  end
  if isEgg then
    table.insert(self.data.cacheEggIds, guid)
  else
    table.insert(self.data.cacheDropIds, guid)
  end
  printf("\232\175\183\230\177\130\229\138\160\229\133\165", blockId, guid, self.data.cacheDropIds, self.data.cacheEggIds)
end

function this:req_pickDropItemByPet(blockId, guid)
  local data = {block_id = blockId, reward_guid = guid}
  L_Net:sendMessage(MsgGenCode.CSProtoRanchPetAutoGetReward, data)
end

function this:req_getStoredRewards(blockId, callBack)
  local data = {u32 = blockId}
  L_Net:sendMessage(MsgGenCode.CSProtoRanchGetStoreReward, data, function(rspData, errorcode)
    if errorcode == L_Const.errorCode.ErrCodeSucc then
      local reward = rspData.rewards
      if not table.isEmpty(reward) then
        L_RewardManager:ShowReward(rspData)
      end
      if callBack then
        callBack()
      end
    end
  end)
end

function this:req_refreshRanch()
  if self._requestingRefresh then
    return
  end
  self._requestingRefresh = true
  L_Net:sendMessage(MsgGenCode.CSProtoRanchRefreshProduct, nil, function()
    self._requestingRefresh = false
  end)
end

function this:req_ranchLvUp(blockId, callback)
  local data = {u32 = blockId}
  L_Net:sendMessage(MsgGenCode.CSProtoRanchLvUp, data, callback)
end

return this
