local this = class("pageProduct_old", G_UIPageBase)
local PageMode = {
  OLD = 1,
  ACCESSORY = 2,
  NORMAL = 3,
  HATCH = 4
}
local HideNodeList_PageProduct = {
  L_Const.WorldNode.PLAYER,
  L_Const.WorldNode.PLAYERPETNODE
}

function this.bind()
  return {
    int_selectTab = -1,
    module_accessoryTabBtnList = {
      moduleName = "pages/product/moduleAccessoryTabBtnList"
    },
    module_productNormalNew = {
      moduleName = "pages/product/moduleProductNormal_old"
    },
    module_productHatchNew = {
      moduleName = "pages/product/moduleProductHatchNew"
    },
    module_productInfo = {
      moduleName = "pages/product/moduleProductInfo_old"
    },
    module_productQueueNew = {
      moduleName = "pages/product/moduleProductQueue_old"
    },
    module_homeProperty = {
      moduleName = "pages/home/homeProperty/moduleHomeProperty"
    }
  }
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)
  local buildType = L_HomeUtil.getBuildingType(self._buildingGuid)
  self.modules.module_productInfo:initModule(self._buildingGuid, buildType)
  self.modules.module_productQueueNew:initModule(self._buildingGuid)
  if L_Const.WorldMapBuildType.WMBT_PRO_ORNAMENT == buildType then
    self.bind.int_selectTab = PageMode.ACCESSORY
    self.modules.module_accessoryTabBtnList:initModule(self._buildingGuid)
  elseif L_Const.WorldMapBuildType.WMBT_HATCH == buildType then
    self.bind.int_selectTab = PageMode.HATCH
    self.modules.module_productHatchNew:initModule(self._buildingGuid)
  else
    self.bind.int_selectTab = PageMode.NORMAL
    self.modules.module_productNormalNew:initModule(self._buildingGuid)
  end
  self:refreshProperty()
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)
  self.modules.module_productInfo:refreshId(id)
end

function this:open(options)
  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)
  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_productInfo:initModule(self._buildingGuid, buildType)
  self.modules.module_productQueueNew:initModule(self._buildingGuid)
  if L_Const.WorldMapBuildType.WMBT_PRO_ORNAMENT == buildType then
    self.bind.int_selectTab = PageMode.ACCESSORY
  elseif L_Const.WorldMapBuildType.WMBT_HATCH == buildType then
    self.bind.int_selectTab = PageMode.HATCH
    self.modules.module_productHatchNew:initModule(self._buildingGuid)
  else
    self.bind.int_selectTab = PageMode.NORMAL
    self.modules.module_productNormalNew:initModule(self._buildingGuid)
  end
end

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

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

return this
