local this = class("pageProductAccessory", G_UIPageBase)
local _HomeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _HomeBuildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()

function this.bind()
  return {
    module_productAccessory = {
      moduleName = "pages/product/moduleProductAccessory"
    },
    module_productQueue = {
      moduleName = "pages/product/moduleProductQueueNew"
    },
    module_productInfo = {
      moduleName = "pages/home/homeStation/moduleStationInfo"
    },
    module_Product = {
      moduleName = "pages/product/moduleProductInfoNormal"
    },
    active_mask = false,
    active_nodeProductInfo = false
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_mask = function(self)
      self:refreshInfo(false)
    end
  }
end

function this:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.product)
  callback(result)
end

function this:preOpen(options)
  self._buildingGuid = options.buildingGuid
  self._buildingType = options.buildType
  this.super.preOpen(self, options)
  self.modules.module_productQueue:initModule(self._buildingGuid)
  self.modules.module_productAccessory:initModule(self._buildingGuid, options.selectTabId)
  local serverData = L_HomeStore:getHomeBuildingByGuid(self._buildingGuid)
  local buildTpl = _HomeBuildingTpl:getTplById(serverData.build_id)
  local groupId = _HomeBuildingTpl:getGroupId(buildTpl)
  local groupTpl = _HomeBuildingGroupTpl:getTplById(groupId)
  self:refreshInfo(false)
end

function this:onSelectId(id, pos)
  self:setDynaNodePos(pos)
  L_TimerManager:newOrResetTimer(self, "animation_onClickItem", function()
    self.modules.module_Product:setSingleQualityProduct(self._buildingGuid, id, self._buildingType)
  end, 0.066, 1)
end

function this:setDynaNodePos(pos)
  if pos == nil then
    return
  end
  local rRects = self.bindComponents.rNodeTrans
  local viewWidth = 656
  local screenPos = C_CameraManager.uiCamera:WorldToScreenPoint(pos)
  local targetPos = rRects.position
  targetPos.x = pos.x
  targetPos.z = pos.z
  rRects.position = targetPos
  local anchoredPos = rRects.anchoredPosition
  if screenPos.x < C_Screen.width / 2 then
    rRects.anchoredPosition = C_Vector2(anchoredPos.x + viewWidth / 2 + 121, anchoredPos.y)
  else
    rRects.anchoredPosition = C_Vector2(anchoredPos.x - viewWidth / 2 - 182, anchoredPos.y)
  end
end

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

function this:refreshdata()
  self.modules.module_productQueue:initModule(self._buildingGuid)
  self.modules.module_productInfo:initBuilding(self._buildingGuid)
end

function this:refreshInfo(isActive)
  self.bind.active_nodeProductInfo = isActive
  self.bind.active_mask = isActive
  if not isActive then
    self.modules.module_Product:hide()
  end
end

return this
