local this = {}

function this:req_setPetDuelFormation(slotId, petGuids, heroGuid, heroSkills, levelId, index, callback)
  local curSaveFormationUids = self:getPetDuelFormationPetGuids(slotId)
  local isSame = true
  for i = 1, #petGuids do
    if curSaveFormationUids[i].is_trial ~= petGuids[i].is_trial or curSaveFormationUids[i].id ~= petGuids[i].id then
      isSame = false
      break
    end
  end
  if isSame and heroGuid ~= self:getPetDuelFormationHeroGuid(slotId) then
    isSame = false
  end
  if isSame then
    local curSaveFormationSkills = self:getPetDuelFormationHeroSkills(slotId)
    if #curSaveFormationSkills <= 0 then
      isSame = false
    else
      for i = 1, #curSaveFormationSkills do
        if curSaveFormationSkills[i].skill_id ~= heroSkills[i].skill_id then
          isSame = false
          break
        end
      end
    end
  end
  if isSame then
    return
  end
  local data = {
    slot = slotId,
    pet_guids = petGuids,
    hero = heroGuid,
    hero_skills = heroSkills,
    duel_id = levelId,
    index = index
  }
  L_Net:sendMessage(MsgGenCode.CSProtoKiboDuelGroupInfoChange, data, function(rspData, errorCode)
    if 0 < errorCode then
      return
    end
    if callback then
      callback()
    end
  end)
end

function this:req_setPetCurDuelFormation(slotId, callback)
  if slotId == L_Const.kiBoDuelBlockSlot then
    if callback then
      callback()
    end
    return
  end
  local data = {u32 = slotId}
  L_Net:sendMessage(MsgGenCode.CSProtoKiboDuelSetCurGroup, data, function(rspData, errorCode)
    if 0 < errorCode then
      return
    end
    if callback then
      callback()
    end
  end)
end

function this:rsp_petDuelFormationData(data, errorCode)
  if not data then
    return
  end
  L_PetDuelStore:setCurRealPetDuelFormationSlot(data.use_slot or 0)
  L_PetDuelStore:setCurPetDuelFormationSlot(data.use_slot or 1)
  if data.groups then
    for _, v in ipairs(data.groups) do
      local petUids = {}
      if v.pet_guids then
        for _, v1 in ipairs(v.pet_guids) do
          table.insert(petUids, {
            is_trial = v1.is_trial,
            id = v1.id or 0
          })
        end
      end
      self.data.petDuelFormationGroupInfo[v.slot] = {
        slot = v.slot,
        pet_guids = petUids,
        hero = v.hero,
        hero_skills = v.hero_skills,
        levelId = v.duel_id,
        lockSlot = v.index
      }
    end
  end
  if data.records then
    for _, v in ipairs(data.records) do
      self.data.petDuelRecordInfo[v.id] = {
        id = v.id,
        star = v.star,
        success_time = v.success_time
      }
    end
    L_PetDuelStore:call(L_PetDuelStore.event.refresh_recordInfo)
  end
  L_PetDuelStore:call(L_PetDuelStore.event.refresh_formationInfo)
end

function this:req_CSProtoKiboDuelBattleFinish(status, callback)
  local data = {status = status}
  L_Net:sendMessage(MsgGenCode.CSProtoKiboDuelBattleFinish, data, function(rspData, errorCode)
    if callback then
      callback()
    end
  end)
end

function this:req_CSProtoKiboDuelStart(duelId, callback)
  local data = {u32 = duelId}
  printLog("KIBODUEL_DEBUG:req_CSProtoKiboDuelStart", duelId)
  L_Net:sendMessage(MsgGenCode.CSProtoKiboDuelStart, data, function(rspData, errorCode)
    if 0 < errorCode then
      return
    end
    if callback then
      callback()
    end
  end)
end

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

function this:rsp_CSProtoKiboDuelAttrInfoSync(data)
  for _, info in ipairs(data.infos) do
    self.data.trialPetGuids[info.pet_guid.id] = info.pet_guid.guid
  end
  if data ~= nil and data.hero_info ~= nil then
    self.data.curHeroGuid = data.hero_info.hero_guid
  end
end

function this:rsp_CSProtoKiboDuelBattleFinish(data)
  if not data or not data.infos then
    return
  end
  self.data.petExpInfo = {}
  for _, info in ipairs(data.infos) do
    table.insert(self.data.petExpInfo, {
      guid = info.pet_guid,
      oldLevel = info.old_lv,
      nowLevel = info.now_lv,
      oldExp = info.old_exp,
      nowExp = info.now_exp
    })
  end
  printf("self.data.petExpInfo", self.data.petExpInfo)
end

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

function this:req_CSProtoKiboDuelGetGroup(levelId, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoKiboDuelGetGroup, {u32 = levelId}, function(rspData, errorCode)
    if callback then
      callback()
    end
  end)
end

return this
