local this = {}

function this:req_CSProtoOperateCanteenItem(param, callback)
  local data = {
    counts = param.count,
    guid = param.guid
  }
  L_Net:sendMessage(MsgGenCode.CSProtoOperateCanteenItem, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoStopCanteen(buildingGuid, callback)
  local data = {u32 = buildingGuid}
  L_Net:sendMessage(MsgGenCode.CSProtoStopCanteen, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoStartCanteen(param, callback)
  local data = {
    product_id = param.productId,
    leader_id = param.masterId,
    guid = param.guid,
    item_counts = param.count
  }
  L_Net:sendMessage(MsgGenCode.CSProtoStartCanteen, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoRefreshCanteen(callback)
  L_Net:sendMessage(MsgGenCode.CSProtoRefreshCanteen, nil, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:rsp_syncCanteen(data)
  local mapInfo = data.map_info
  if table.isEmpty(mapInfo) or table.isEmpty(mapInfo.home) then
    return
  end
  local homeData = mapInfo.home
  local needCallCanteen = false
  for i, v in ipairs(homeData.builds or {}) do
    local homeItem = v
    if not table.isEmpty(homeItem.canteen) then
      needCallCanteen = true
      self.data.canteenMap[homeItem.guid] = homeItem.canteen
    end
  end
  if needCallCanteen then
    self:call(self.event.refreshCanteen)
  end
  if homeData.food_refresh_time then
    self.data.refreshTime = homeData.food_refresh_time
  end
  if homeData.food_num then
    self.data.foodStorage = homeData.food_num
    self:initServer()
  end
end

return this
