local this = class("moduleHomeCenterPetManage", require("ui.pages.home.homeCenter.moduleHomeCenterToggleModule"))
local _homeFreeWorkPositionTpl = L_GameTpl:getHomeFreeWorkPositionTpl()
local _homeBlockTpl = L_GameTpl:getHomeBlockTpl()
local _homeRanchTpl = L_GameTpl:getHomeRanchTpl()

function this.bind()
  return {
    text_petStationedNumInfo = "",
    text_ranchPetNumInfo = "",
    active_petStationUnlock = true,
    active_petStationLock = false,
    text_petStationLockTip = "",
    text_petStationDesc = "",
    btn_petStationInteractable = true,
    active_ranchUnlock = true,
    active_ranchLock = false,
    text_ranchLockTip = "",
    text_ranchDesc = "",
    btn_ranchInteractable = true
  }
end

function this.methods()
  return {
    onClick_btn_petStation = function(self)
      self:onClickBtnPetStation()
    end,
    onClick_btn_ranch = function(self)
      self:onClickBtnRanch()
    end
  }
end

function this:open()
  self.onHomeHubSyncHandler = handler(self, self.onHomeHubSync)
  L_HomeStore:getCsHomeStore():RegisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
  L_HomeRanchStore:listenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshUI, self)
  self.bind.text_petStationDesc = L_WordsTpl:getValue("ui_homemanage_petmanage_desc")
  self.bind.text_ranchDesc = L_WordsTpl:getValue("ui_homemanage_ranch_desc")
  self.petStationedConditionNodeList = {}
  for _, petStationedUnlockedConditionList in pairs(_homeFreeWorkPositionTpl:getAllUnlockConditionList()) do
    local conditionNode = L_ConditionManager:registGroup("moduleHomeCenterPetManagePetStationedUnlock", petStationedUnlockedConditionList, self.onPetStationedUnlockedConditionStateChanged, self)
    if conditionNode ~= nil then
      table.insert(self.petStationedConditionNodeList, conditionNode)
    end
  end
  self:initSystemUnlock()
end

function this:close()
  L_HomeStore:getCsHomeStore():UnregisterEvent(C_EHomeStoreEvent.HomeHubSync, self.onHomeHubSyncHandler)
  L_HomeRanchStore:unListenCallFunc(L_HomeRanchStore.event.refreshRanch, self.refreshUI, self)
  for _, conditionNode in pairs(self.petStationedConditionNodeList) do
    L_ConditionManager:unRegist(conditionNode, "moduleHomeCenterPetManagePetStationedUnlock")
  end
  self:disposeSystemUnlock()
end

function this:show()
  self.bindComponents.ranchAnim.transform.parent:SetAsLastSibling()
  self.bindComponents.petAnim:SetBool("Enter", false)
  self.bindComponents.ranchAnim:SetBool("Enter", false)
  if self._isEnter then
    self:showAnim("anim_homecenter_pet_show")
    self._isEnter = nil
  end
end

function this:hide()
  if self._coAnim then
    co.stop(self._coAnim)
    self._coAnim = nil
  end
end

function this:refreshData()
  self.UIData = {}
  self.UIData.curPetStationedNum = L_HomeStore:getCurPetStationedNum()
  self.UIData.unlockedPetStationedNum = self:getUnlockedPetStationedNum()
  local defaultBlockId = _homeBlockTpl:getId(_homeBlockTpl:getBlockTypeList(L_HomeConst.blockType.RANCH)[1])
  local ranchData = L_HomeRanchStore:getBlockRanchData(defaultBlockId)
  self.UIData.curRanchPetNum = ranchData and ranchData:getProductPetNum() or 0
  local lv = ranchData and ranchData.lv or 1
  local ranchTpl = _homeRanchTpl:getTplById(lv)
  self.UIData.maxCanRanchPetNum = _homeRanchTpl:getPetLimit(ranchTpl)
end

function this:refreshUI()
  self.bind.text_petStationedNumInfo = string.format("%d/%d", self.UIData.curPetStationedNum, self.UIData.unlockedPetStationedNum)
  self.bind.text_ranchPetNumInfo = string.format("%d/%d", self.UIData.curRanchPetNum, self.UIData.maxCanRanchPetNum)
end

function this:showAnim(animName)
  self.bindComponents.anim:Play(animName)
end

function this:getUnlockedPetStationedNum()
  local unlockedPetStationedNum = 0
  local freeWorkPosId2UnlockConditionList = _homeFreeWorkPositionTpl:getFreeWorkPosId2UnlockConditionList()
  for freeWorkPositionId, unlockConditionList in pairs(freeWorkPosId2UnlockConditionList) do
    if L_ConditionManager:isComplete(unlockConditionList) and freeWorkPositionId > unlockedPetStationedNum then
      unlockedPetStationedNum = freeWorkPositionId
    end
  end
  return unlockedPetStationedNum
end

function this:initSystemUnlock()
  L_SystemUnlockStore:listenCallFunc(L_SystemUnlockStore.event.updateSystem, self.refreshSystemUnlock, self)
  self:refreshSystemUnlock()
end

function this:refreshSystemUnlock(id)
  if id ~= nil and id ~= L_SystemConst.enum.homeCenterPet and id ~= L_SystemConst.enum.homeCenterRanch then
    return
  end
  if L_SystemUnlockManager:check(L_SystemConst.enum.homeCenterPet) then
    self.bind.active_petStationUnlock = true
    self.bind.active_petStationLock = false
    self.bind.btn_petStationInteractable = true
  else
    self.bind.active_petStationUnlock = false
    self.bind.active_petStationLock = true
    self.bind.btn_petStationInteractable = false
    self.bind.text_petStationLockTip = L_SystemUnlockManager:getSystemUnlockConditionDesc(L_SystemConst.enum.homeCenterPet)
  end
  if L_SystemUnlockManager:check(L_SystemConst.enum.homeCenterRanch) then
    self.bind.active_ranchUnlock = true
    self.bind.active_ranchLock = false
    self.bind.btn_ranchInteractable = true
  else
    self.bind.active_ranchUnlock = false
    self.bind.active_ranchLock = true
    self.bind.btn_ranchInteractable = false
    self.bind.text_ranchLockTip = L_SystemUnlockManager:getSystemUnlockConditionDesc(L_SystemConst.enum.homeCenterRanch)
  end
end

function this:disposeSystemUnlock()
  L_SystemUnlockStore:unListenCallFunc(L_SystemUnlockStore.event.updateSystem, self.refreshSystemUnlock, self)
end

function this:onHomeHubSync()
  self:refreshData()
  self:refreshUI()
end

function this:onPetStationedUnlockedConditionStateChanged()
  self:refreshData()
  self:refreshUI()
end

function this:onClickBtnPetStation()
  if self._coAnim then
    return
  end
  self._coAnim = co.start(function()
    self.bindComponents.petAnim.transform.parent:SetAsLastSibling()
    self.bindComponents.petAnim:SetBool("Enter", true)
    self:showAnim("anim_homecenter_pet_enter")
    self._isEnter = true
    self.parent:hideChangeButton("changeToTagAnim")
    co.wait(0.2333, true)
    L_UI:open("pageHomeCenterPet")
    self._coAnim = nil
  end)
end

function this:onClickBtnRanch()
  self._coAnim = co.start(function()
    self.bindComponents.ranchAnim.transform.parent:SetAsLastSibling()
    self.bindComponents.ranchAnim:SetBool("Enter", true)
    self:showAnim("anim_homecenter_pet_enter")
    self._isEnter = true
    self.parent:hideChangeButton("changeToTagAnim")
    co.wait(0.2333, true)
    L_UI:open("pageHomeRanch")
    self._coAnim = nil
  end)
end

return this
