local this = {}

function this:req_CSProtoFieldOpen(blockId, ids)
  local datas = {}
  for _, id in ipairs(ids) do
    if not self:getFieldData(blockId, id) then
      table.insert(datas, {block_id = blockId, field_id = id})
    end
  end
  if 0 < #datas then
    L_Net:sendMessage(MsgGenCode.CSProtoFieldOpen, {field_keys = datas})
  end
end

function this:req_CSProtoFieldClose(blockId, id)
  L_Net:sendMessage(MsgGenCode.CSProtoFieldClose, {field_id = id, block_id = blockId})
end

function this:req_CSProtoFieldPlant(block_id, idDatas, seed_id)
  local itemNum = L_BagStore:getItemNum(seed_id)
  local datas = {}
  for _, ids in ipairs(idDatas) do
    if itemNum <= 0 then
      break
    end
    if L_HomeUtil.getIdsCanPlant(block_id, ids) then
      itemNum = itemNum - 1
      table.insert(datas, {seed_id = seed_id, field_ids = ids})
    end
  end
  if 0 < #datas then
    if self._requesting then
      return
    end
    self._requesting = true
    L_Net:sendMessage(MsgGenCode.CSProtoFieldPlant, {
      plant_info = datas,
      block_id = block_id,
      is_pet = false
    }, function(data)
      self._requesting = nil
    end)
  end
end

function this:req_CSProtoFieldPlantByPet(block_id, idDatas)
  local datas = {}
  for _, ids in ipairs(idDatas) do
    if L_HomeUtil.getIdsCanPlant(block_id, ids) then
      table.insert(datas, {seed_id = 0, field_ids = ids})
    end
  end
  if 0 < #datas then
    if self._requesting then
      return
    end
    self._requesting = true
    L_Net:sendMessage(MsgGenCode.CSProtoFieldPlant, {
      plant_info = datas,
      block_id = block_id,
      is_pet = true
    }, function(data)
      self._requesting = nil
    end, nil, {isMask = false})
  end
end

function this:req_CSProtoFieldWater(block_id, fieldIds, is_pet)
  is_pet = is_pet or false
  local ids = {}
  local fieldIdMap = {}
  for _, id in ipairs(fieldIds) do
    local fieldData = self:getFieldData(block_id, id)
    if fieldData then
      if fieldData.status == L_Const.WorldMapFieldStatusType.WMFST_OPEN then
        fieldIdMap[id] = true
      elseif fieldData.status == L_Const.WorldMapFieldStatusType.WMFST_CROP then
        fieldIdMap[fieldData:getCropData().field_min] = true
      end
    end
  end
  for id, _ in pairs(fieldIdMap) do
    table.insert(ids, id)
  end
  if 0 < #ids then
    if self._requesting then
      return
    end
    self._requesting = true
    L_Net:sendMessage(MsgGenCode.CSProtoFieldWater, {
      block_id = block_id,
      field_ids = ids,
      is_pet = is_pet
    }, function(data)
      self._requesting = nil
    end, nil, {
      isMask = not is_pet
    })
  end
end

function this:req_CSProtoFieldHarvest(guids, is_pet, callback)
  if self._requesting or #guids <= 0 then
    return
  end
  self._requesting = true
  local crop = L_HomeCropStore:getCropData(guids[1])
  L_Net:sendMessage(MsgGenCode.CSProtoFieldHarvest, {
    block_id = crop:getFieldData().block_id,
    crop_ids = guids,
    is_pet = is_pet
  }, function(data)
    if callback then
      callback(data)
    end
    self._requesting = nil
  end, nil, {
    isMask = not is_pet
  })
end

function this:req_CSProtoFieldUnplant(guid, callback)
  if self._requesting then
    return
  end
  self._requesting = true
  L_Net:sendMessage(MsgGenCode.CSProtoFieldUnplant, {u32 = guid}, function(data)
    if callback then
      callback()
    end
    self._requesting = nil
  end)
end

function this:req_CSProtoAutoSelectSeed(id, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoQuickOperateSeedStore, {u32 = id}, function()
    if callback then
      callback()
    end
  end)
end

function this:req_CSProtoOperateSeed(op, id, num, block_id, guid, cb)
  L_Net:sendMessage(MsgGenCode.CSProtoOperateSeedStorehouse, {
    operate_type = op,
    block_id = block_id,
    seed = {
      itemid = id,
      itemnum = num,
      guid = guid or 0,
      itemtype = L_Const.resType.commonItem
    }
  }, cb)
end

function this:req_CSProtoExchangeSeedLoc(block_id, guid1, guid2)
  L_Net:sendMessage(MsgGenCode.CSProtoExchangeSeedLoc, {
    block_id = block_id,
    guid = guid1,
    target_guid = guid2
  })
end

function this:req_CSProtoGetAutoWorkCropReward(block_id, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoGetAutoWorkCropReward, {u32 = block_id}, function(rspData, errCode)
    if errCode == L_Const.errorCode.ErrCodeSucc then
      local reward = rspData.rewards
      if not table.isEmpty(reward) then
        L_RewardManager:ShowReward(reward)
      end
      if callback then
        callback()
      end
    end
  end)
end

return this
