local this = class("pageProduct", G_UIPageBase)
local HideNodeList_PageProduct = {
  L_Const.WorldNode.PLAYER,
  L_Const.WorldNode.PLAYERPETNODE
}

function this.bind()
  return {
    module_productNormalNew = {
      moduleName = "pages/product/moduleProductNormalNew"
    },
    module_productInfo = {
      moduleName = "pages/product/moduleProductInfoNormal"
    },
    module_productQueueNew = {
      moduleName = "pages/product/moduleProductQueueNew"
    },
    module_homeProperty = {
      moduleName = "pages/home/homeProperty/moduleHomeProperty"
    },
    moduleStationInfo = {
      moduleName = "pages/home/homeStation/moduleStationInfo"
    }
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_bg = function(self)
      self.modules.module_productNormalNew:onSelectTarget(0)
    end
  }
end

function this:check(options, callback)
  local manager = AzurWorld.HomeMgr
  local build = manager:GetHomeBuild(options.buildingGuid)
  local isFromHomeCenter = options.isFromHomeCenter
  if (not build or not build.buildEntity) and isFromHomeCenter ~= true then
    callback(false)
    return
  end
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.product)
  callback(result)
end

function this:preOpen(options)
  self._buildingGuid = options.buildingGuid
  self.isFromHomeCenter = options.isFromHomeCenter
  self:cameraFocusOnMainUITrans()
  this.super.preOpen(self, options)
  self:refreshProperty()
  self.isFirstInAndSelectId = true
end

function this:cameraFocusOnMainUITrans()
  if self.isFromHomeCenter == true then
    return
  end
  L_HomeUtil.focusBuildWithCustomHideList(self._buildingGuid, L_HomeConst.FocusType.MAINUI, HideNodeList_PageProduct, "pageProduct")
end

function this:cameraExitFocusOnMainUITrans()
  if self.isFromHomeCenter == true then
    return
  end
  L_HomeUtil.exitFocusBuildAndRevertCustomHideNodeList(HideNodeList_PageProduct, "pageProduct")
end

function this:onSelectId(id, isMultiQuality, sourceOfPlayModuleProductInfoAnimation)
  local moduleProductInfo = self.modules.module_productInfo
  local moduleProductInfoAnimation = moduleProductInfo.bindComponents.animation
  if self.isFirstInAndSelectId == true or sourceOfPlayModuleProductInfoAnimation == nil then
    self:setModuleProductInfo(id, isMultiQuality)
  elseif sourceOfPlayModuleProductInfoAnimation == L_HomeConst.SourceOfPlayModuleProductInfoAnimation.OnClickTab then
    if moduleProductInfoAnimation.isPlaying then
      moduleProductInfoAnimation:Stop()
    end
    moduleProductInfoAnimation:Play("anim_product_commoninfo_change")
    L_TimerManager:newOrResetTimer(self, "animation_onClickTab", function()
      self:setModuleProductInfo(id, isMultiQuality)
    end, 0.13, 1)
  elseif sourceOfPlayModuleProductInfoAnimation == L_HomeConst.SourceOfPlayModuleProductInfoAnimation.OnClickItem then
    if moduleProductInfoAnimation.isPlaying then
      moduleProductInfoAnimation:Stop()
    end
    moduleProductInfoAnimation:Play("anim_product_commoninfo_change1")
    L_TimerManager:newOrResetTimer(self, "animation_onClickItem", function()
      self:setModuleProductInfo(id, isMultiQuality)
    end, 0.066, 1)
  end
  self.isFirstInAndSelectId = false
end

function this:setModuleProductInfo(id, isMultiQuality)
  local moduleProductInfo = self.modules.module_productInfo
  if id == 0 then
    moduleProductInfo:setIsEmpty(true)
    return
  end
  self._buildingType = self._buildingType or L_HomeUtil.getBuildingType(self._buildingGuid)
  if self._buildingType == L_Const.WorldMapBuildType.WMBT_HATCH then
    local eggGuid = id
    moduleProductInfo:setEggInfo(eggGuid, self._buildingGuid)
  elseif isMultiQuality then
    local foodGroupId = id
    moduleProductInfo:setMultiQualityProduct(self._buildingGuid, foodGroupId)
  else
    local productId = id
    moduleProductInfo:setSingleQualityProduct(self._buildingGuid, productId, self._buildingType)
  end
end

function this:open(options)
  self.bindComponents.anim_moduleProduct_BgTemplate:Play("anim_product_common_bg")
  L_ProductStore:listenCallFunc(L_ProductStore.event.closeTargetInfo, self.closeTargetInfo, self)
end

function this:close(options)
  local entity = L_HomeManager:getBuildingEntityByGuid(self._buildingGuid)
  if entity then
    L_HomeManager:sendEvent(L_HomeManager.event.triggerBuilding, {
      isIn = entity.data.isIn,
      entity = entity
    })
  end
  self:cameraExitFocusOnMainUITrans()
  L_ProductStore:unListenCallFunc(L_ProductStore.event.closeTargetInfo, self.closeTargetInfo, self)
  L_TimerManager:clearTimer(self)
  this.super.close(self, options)
end

function this:show()
  self:cameraFocusOnMainUITrans()
  self:refreshdata()
end

function this:refreshdata()
  local buildType = L_HomeUtil.getBuildingType(self._buildingGuid)
  self.modules.module_productQueueNew:initModule(self._buildingGuid)
  self.modules.module_productNormalNew:initModule(self._buildingGuid)
  self.modules.moduleStationInfo:initBuilding(self._buildingGuid)
end

function this:refreshProperty()
  self.modules.module_homeProperty:setProduct(self._buildingGuid)
end

function this:closeTargetInfo()
  self.modules.module_productNormalNew:onSelectTarget(0)
end

return this
