local this = class("pageHomeRanch", G_UIPageBase)
local _HomeRanchTpl = L_GameTpl:getHomeRanchTpl()
local _homeBlockTpl = L_GameTpl:getHomeBlockTpl()
local PageMode = {REWARD = false, PET = true}

function this.bind()
  return {
    txt_petCurNum = "",
    txt_petMaxNum = "",
    list_showPet = {
      moduleName = "pages/pet/modulePetPixelIcon"
    },
    toggle_mode = PageMode.REWARD,
    list_raisePet = {
      moduleName = "pages/home/homeRanch/cellRanchRaisePet"
    },
    module_ranchPet = {
      moduleName = "pages/home/homeStation/moduleStationInfo"
    },
    module_homeProperty = {
      moduleName = "pages/home/homeProperty/moduleHomeProperty"
    },
    module_reward = {
      moduleName = "pages/home/homeCrop/moduleHomeRewardPanel"
    }
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      self:escHandle()
    end,
    module_reward = {
      onClick_addSeed = function(self)
        self.bind.toggle_mode = PageMode.PET
        self:refreshAll()
      end
    }
  }
end

function this:preOpen(params)
  params = params or {}
  local defaultBlockId = _homeBlockTpl:getId(_homeBlockTpl:getBlockTypeList(L_HomeConst.blockType.RANCH)[1])
  self._blockId = params.blockId or defaultBlockId
  local maxLv = _HomeRanchTpl:getMaxLv()
  local maxTpl = _HomeRanchTpl:getTplById(maxLv)
  local datalist = {}
  for i = 1, _HomeRanchTpl:getPetLimit(maxTpl) do
    table.insert(datalist, {
      blockId = self._blockId,
      index = i
    })
  end
  self.bind.list_raisePet:clear()
  self.bind.list_raisePet:insert_array(datalist)
  self.bind.list_showPet:clear()
  self.bind.list_showPet:insert_array(L_DataUtil.getEmptyModuleDatas(_HomeRanchTpl:getPetLimit(maxTpl)))
  L_HomeRanchStore:listenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshAll, self)
  self.bind.toggle_mode = PageMode.REWARD
  local moduleHomeRewardPanel = self.modules.module_reward
  moduleHomeRewardPanel:initRanchData(self._blockId)
  self:refreshAll()
end

function this:close()
  L_HomeRanchStore:unListenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshAll, self)
  L_TimerManager:stopTimer(self, "closePetStationed")
end

function this:refreshAll()
  local ranchData = L_HomeRanchStore:getBlockRanchData(self._blockId)
  local lv = ranchData and ranchData.lv or 1
  local ranchTpl = _HomeRanchTpl:getTplById(lv)
  self.bind.txt_petCurNum = string.format("%s", ranchData and ranchData:getProductPetNum() or 0)
  self.bind.txt_petMaxNum = string.format("/%s", _HomeRanchTpl:getPetLimit(ranchTpl))
  for i, module in ipairs(self.modules.list_showPet) do
    module:setGuid(ranchData.product_pets[i] and ranchData.product_pets[i].pet_guid or 0, true)
  end
  local moduleStationInfo = self.modules.module_ranchPet
  moduleStationInfo:initRanch(self._blockId)
  self.modules.module_homeProperty:setRanchBlock(self._blockId)
end

function this:onClickUpgrade()
  local ranchData = L_HomeRanchStore:getBlockRanchData(self._blockId)
  local currLv = ranchData and ranchData.lv or 1
  if currLv >= _HomeRanchTpl:getMaxLv() then
    L_FlyMsgManager:showNormalMsgByKey("ui_pageHomeRanch_02")
    return
  end
  L_UI:open("pageBuildingUpgrade", {
    ranchBlockId = self._blockId
  })
end

function this:escHandle()
  if self.bind.toggle_mode == PageMode.PET then
    self:refreshAll()
    self.bindComponents.petStationedAnim:Play("anim_product_stationedManage_cellout")
    L_TimerManager:newOrResetTimer(self, "closePetStationed", function()
      self.bind.toggle_mode = PageMode.REWARD
    end, 0.1)
  else
    L_UI:close(self.pageName)
  end
end

return this
