local this = {}

function this:req_CSProtoProductStart(param, callback)
  local data = {
    build_guid = param.guid,
    product_id = param.productId,
    count = param.count
  }
  L_Net:sendMessage(MsgGenCode.CSProtoProductStart, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoNormalProductStart(guid, productId, count, selectMaterial, callback)
  local data = {
    build_guid = guid,
    product_id = productId,
    count = count,
    select_material = selectMaterial or {}
  }
  L_Net:sendMessage(MsgGenCode.CSProtoProductStart, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoAccessoryProductStart(buildGuid, productId, accGuid, callback)
  local data = {
    build_guid = buildGuid,
    product_id = productId,
    ornament_guid = accGuid
  }
  L_Net:sendMessage(MsgGenCode.CSProtoProductOrnamentStart, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoProductStartImmediately(param, callback)
  local data = {
    guid = param.guid,
    product_id = param.productId,
    count = param.count
  }
  L_Net:sendMessage(MsgGenCode.CSProtoProductStart, data, function(rspData, errorCode)
    if callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_CSProtoProductFinish(buildingGuid, callback)
  local data = {guid = buildingGuid, product_guid = 0}
  L_Net:sendMessage(MsgGenCode.CSProtoProductFinish, data, function(rspData, errorCode)
    if callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_CSProtoProductFinishOne(buildingGuid, productGuid, callback)
  local data = {guid = buildingGuid, product_guid = productGuid}
  L_Net:sendMessage(MsgGenCode.CSProtoProductFinish, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_CSProtoProductCancel(buildingGuid, productGuid, callback)
  local data = {guid = buildingGuid, product_guid = productGuid}
  L_Net:sendMessage(MsgGenCode.CSProtoProductCancel, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_CSProtoSimpleProductStart(productId, count, selectMaterial, callback)
  local data = {
    build_guid = 0,
    product_id = productId,
    count = count,
    select_material = selectMaterial
  }
  L_Net:sendMessage(MsgGenCode.CSProtoSimpleProductStart, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:req_CSProtoSimpleProductCancel(index, callback)
  local data = {u32 = index}
  L_Net:sendMessage(MsgGenCode.CSProtoSimpleProductCancel, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:req_CSProtoSimpleProductOrnamentStart(cookId, cookMaterial, cookCount, callback)
  local data = {
    build_guid = buildingGuid,
    cook_id = cookId,
    cook_material = cookMaterial,
    cook_count = cookCount
  }
  L_Net:sendMessage(MsgGenCode.CSProtoSimpleProductOrnamentStart, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:req_CSProtoSimpleCookRequest(productId, ornamentGuid, callback)
  local data = {
    build_guid = buildingGuid,
    product_id = productId,
    ornament_guid = ornamentGuid
  }
  L_Net:sendMessage(MsgGenCode.CSProtoSimpleCookRequest, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:rsp_syncHomeProduct(data)
  local mapInfo = data.map_info
  if table.isEmpty(mapInfo) or table.isEmpty(mapInfo.home) then
    return
  end
  local homeData = mapInfo.home
  for i, v in ipairs(homeData.builds or {}) do
    local homeItem = v
    self.data.homeProductMap[homeItem.guid] = homeItem.product
  end
end

function this:rsp_onPetFinishProduct(data)
  local guid = data.pet_guid
  local reward = data.reward
  if reward then
    L_ProductManager:showPetFinishMsg(guid, reward.rewards or {})
  end
end

function this:rsp_syncHomeProduct(data)
  local mapInfo = data.map_info
  if table.isEmpty(mapInfo) or table.isEmpty(mapInfo.home) then
    return
  end
  local homeData = mapInfo.home
  for i, v in ipairs(homeData.builds or {}) do
    local homeItem = v
    self.data.homeProductMap[homeItem.guid] = homeItem.product
  end
end

function this:rsp_onSyncFastProductData(data, errorCode)
  if errorCode ~= L_Const.errorCode.ErrCodeSucc then
    return
  end
  local productList = data.products
  if not table.isEmpty(productList) then
    for _, value in ipairs(productList) do
      local guid = value.product_guid
      self.data.fastProductList[guid] = value
    end
  end
  if not table.isEmpty(data.dels) then
    for _, value in ipairs(data.dels) do
      self.data.fastProductList[value] = nil
    end
  end
  if not table.isEmpty(data.reward) then
    self:addFastProductReward(data.reward)
  end
  if not table.isEmpty(data.special_reward) then
    self:addFastProductReward(data.special_reward)
  end
  L_ProductStore:call(L_ProductStore.event.refreshFastProduct)
  L_ProductManager:onShowAllFastProductRewards()
end

return this
