local this = class("petStore", G_BaseStore)
this.event = {
  sortPetList = "sortPetList",
  refreshPet = "refreshPet",
  setCurPetId = "setCurPetId",
  setSelectReleasePet = "setSelectReleasePet",
  refreshIncubationList = "refreshIncubationList",
  refreshFeedBack = "refreshFeedBack",
  refreshNewPet = "refreshNewPet",
  refreshPetGeneMutation = "refreshPetGeneMutation",
  removePet = "removePet",
  removePetEgg = "removePetEgg",
  closeGetPet = "closeGetPet",
  closeGetPetEgg = "closeGetPetEgg",
  closePetBox = "closePetBox",
  updatePetSkill = "updatePetSkill",
  getNewPetSkill = "getNewPetSkill",
  convertPetSkill = "convertPetSkill",
  refreshBuildingPet = "refreshBuildingPet",
  refreshPetBox = "refreshPetBox",
  refreshSinglePetBox = "refreshSinglePetBox",
  refreshPetCombindHero = "refreshPetCombindHero",
  petLevelUp = "petLevelUp",
  petComprehensionLvUp = "petComprehensionLvUp",
  setPetReleaseMode = "setPetReleaseMode",
  specialPetInfoRefresh = "specialPetInfoRefresh",
  refreshPetFavorLvUp = "refreshPetFavorLvUp"
}
this.saveKey = {
  save_curBoxIndex = "saveBoxIndex_%d"
}
this:importPartialClass(require(L_R.store .. "pet.petState"))
this:importPartialClass(require(L_R.store .. "pet.petAction"))

function this:setCurSelectedEggGuid(guid)
  self.data.curSelectedEggGuid = guid
end

function this:removePet(guid)
  local refreshBox = {}
  for i, v in ipairs(guid) do
    for n, k in pairs(self.data.petList) do
      if k.guid == v then
        if self.data.curSelectedPetGuid == v then
          self.data.curSelectedPetGuid = nil
        end
        self.data.petList[n] = nil
      end
    end
    local index = self.data.petBoxIndex[v].index
    if index and self.data.petBoxList[index][self.data.petBoxIndex[v].slot] then
      self.data.petBoxList[index][self.data.petBoxIndex[v].slot] = 0
      refreshBox[index] = index
    end
  end
  if not table.isEmpty(refreshBox) then
    self:call(self.event.refreshPetBox, refreshBox)
  end
  self:call(self.event.removePet, guid)
end

function this:removePetEgg(guid)
  for i, v in ipairs(guid) do
    if self.data.petEggList[v] ~= nil then
      self.data.petEggList[v] = nil
    end
  end
  self:call(self.event.removePetEgg)
end

function this:setHasInitSync()
  self.data.setHasInitSync = true
end

function this:setInsertBox(boxId, guid, needRefreshBox)
  if math.isEmpty(boxId) then
    return
  end
  needRefreshBox = needRefreshBox or {}
  local index = boxId // 100
  local slot = boxId % 100
  local oldBox = 0
  local oldSlot = 0
  if self.data.petBoxIndex[guid] then
    oldBox = self.data.petBoxIndex[guid].index
    oldSlot = self.data.petBoxIndex[guid].slot
    if (oldBox ~= index or oldSlot ~= slot) and self.data.petBoxList[oldBox][oldSlot] == guid then
      self.data.petBoxList[oldBox][oldSlot] = 0
    end
  end
  if self.data.petBoxList[index] == nil then
    self.data.petBoxList[index] = {}
  end
  self.data.petBoxList[index][slot] = guid
  self.data.petBoxIndex[guid] = {}
  self.data.petBoxIndex[guid].index = index
  self.data.petBoxIndex[guid].slot = slot
  needRefreshBox[oldBox] = oldBox
  needRefreshBox[index] = index
  return needRefreshBox
end

function this:setSaveBoxIndexKey(boxIndex, playerId)
  local pId = playerId or L_PlayerStore:getPlayerId()
  local key = string.format(self.saveKey.save_curBoxIndex, pId)
  C_PlayerPrefsUtility.SetInt(key, boxIndex)
end

function this:setPetBoxName(index, name)
  self.data.petBoxNameList[index] = name
end

return this
