local homeBlockRanchData = class("homeBlockRanchData")

function homeBlockRanchData:ctor(blockId)
  self.block_id = blockId
end

function homeBlockRanchData:populate(data)
  self.product_pets = {}
  for _, slotData in ipairs(data.product_pets or {}) do
    self.product_pets[slotData.slot] = slotData
  end
  self.harvest_pet = data.harvest_pet
  self.drops = {}
  for _, dropData in ipairs(data.drops or {}) do
    self.drops[dropData.guid] = dropData
  end
  self.eggs = {}
  for _, eggData in ipairs(data.eggs or {}) do
    self.eggs[eggData.guid] = require(L_R.store .. "pet.data.petEggItem").new()
    self.eggs[eggData.guid]:populate(eggData)
  end
  self.stores = data.stores or {}
  self.lv = data.lv
end

function homeBlockRanchData:getGuidBySlot(slot)
  return self.product_pets[slot] and self.product_pets[slot].pet_guid or 0
end

function homeBlockRanchData:getProductPetNum()
  local res = 0
  for _, slot in pairs(self.product_pets) do
    if not math.isEmpty(slot.pet_guid) then
      res = res + 1
    end
  end
  return res
end

function homeBlockRanchData:getDropItemTotalNum()
  local totalItemNum = 0
  for _, dropData in pairs(self.drops) do
    local item = dropData.item
    totalItemNum = totalItemNum + item.itemnum
  end
  return totalItemNum
end

local this = class("homeRanchStore", G_BaseStore)
this.event = {
  refreshRanch = "refreshRanch"
}
this:importPartialClass(require(L_R.store .. "homeRanch.homeRanchState"))
this:importPartialClass(require(L_R.store .. "homeRanch.homeRanchAction"))

function this:ctor()
  this.super.ctor(self)
end

function this:syncDatas(protoDatas)
  printf("\231\137\167\229\156\186\230\149\176\230\141\174", protoDatas)
  for _, data in ipairs(protoDatas or {}) do
    local block_id = data.block_id
    self.data.ranchBlockData[block_id] = self.data.ranchBlockData[block_id] or homeBlockRanchData.new(block_id)
    self.data.ranchBlockData[block_id]:populate(data)
  end
  self:call(this.event.refreshRanch)
end

return this
