local this = class("cellRanchRaisePet", G_UIModuleBase)
local _HomeRanchTpl = L_GameTpl:getHomeRanchTpl()
local _petHomeTalentTpl = L_GameTpl:getPetHomeTalentTpl()
local _homeDropTpl = L_GameTpl:getHomeDropTpl()
local CellMode = {
  LOCK = 1,
  EMPTY = 2,
  PET = 3
}

function this.bind()
  return {
    tab_mode = -1,
    module_pixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    },
    txt_condition = "",
    img_iconDrop = ""
  }
end

function this.methods()
  return {
    onClick_pet = function(self)
      if self.bind.tab_mode == CellMode.EMPTY or self.bind.tab_mode == CellMode.PET then
        local func = function(petGuid)
          L_HomeRanchStore:req_setProductPet(self.bind.blockId, self.bind.index, petGuid, handler(self, self.refreshView))
        end
        L_UI:open("pageStation", {
          capacityId = L_HomeConst.homeLaborType.RANCH_PRODUCT,
          selectGuid = self._guid,
          petSize = {
            1,
            2,
            3,
            4
          },
          blockId = self.bind.blockId,
          callback = func
        })
      end
    end
  }
end

function this:open()
  self:refreshView()
  L_HomeRanchStore:listenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshView, self)
end

function this:refreshView()
  local blockId = self.bind.blockId
  local index = self.bind.index
  local ranchData = L_HomeRanchStore:getBlockRanchData(blockId)
  local currLv = ranchData and ranchData.lv or 1
  local tpl = _HomeRanchTpl:getTplById(currLv)
  if index > _HomeRanchTpl:getPetLimit(tpl) then
    self.bind.tab_mode = CellMode.LOCK
    for i = 1, _HomeRanchTpl:getMaxLv() do
      local tempTpl = _HomeRanchTpl:getTplById(i)
      if index <= _HomeRanchTpl:getPetLimit(tempTpl) then
        self.bind.txt_condition = L_WordsTpl:getValue("ui_moduleCanteenFree_03", {
          [0] = i
        })
        break
      end
    end
  elseif not ranchData.product_pets[index] then
    self.bind.tab_mode = CellMode.EMPTY
    self._guid = 0
  else
    self.bind.tab_mode = CellMode.PET
    self._guid = ranchData.product_pets[index].pet_guid
    self.modules.module_pixelIcon:setGuid(self._guid, true)
    local confId = L_PetStore:getPetItem(self._guid).id
    local petTalentTpl = _petHomeTalentTpl:getTplById(confId)
    local dropIds = _petHomeTalentTpl:getFarmProducts(petTalentTpl)
    if #dropIds <= 0 then
      errorf("\231\173\150\229\136\146\233\133\141\233\148\153\228\186\134 ,\229\174\160\231\137\169" .. confId .. "\230\178\161\230\156\137\231\137\167\229\156\186\228\186\167\229\135\186\239\188\140\232\166\129\230\177\130\230\137\128\230\156\137\229\174\160\231\137\169\233\131\189\230\156\137")
    else
      local tpl_drop = _homeDropTpl:getTplById(dropIds[1])
      local itemType, itemId = _homeDropTpl:getItemType(tpl_drop), _homeDropTpl:getItemId(tpl_drop)
      self.bind.img_iconDrop = L_ItemTplManager:getItemConfig(itemType, itemId[1]).icon
    end
  end
end

function this:close()
  L_HomeRanchStore:unListenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshView, self)
end

return this
