local this = class("interactive", G_UIPageBase)
local _buildingTpl = L_GameTpl:getHomeBuildingTpl()
local _buildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()

function this.bind()
  return {
    list_button = {
      moduleName = "pages/interactive/cellInteractive"
    },
    active_buttonBg = false,
    active_mouse = false,
    size_buttonBg = C_Vector2(608, 320),
    size_line1 = C_Vector2(100, 53),
    size_line2 = C_Vector2(100, 174)
  }
end

function this.methods()
  return {}
end

function this:open(options)
  this.super.open(self, options)
  L_InteractiveManager:addListener(L_InteractiveManager.event.onButtonDataChange, self.refreshButtons, self)
  L_ShortCutManager:registerShortCut(self.pageName, C_InputManager_KeyType.EMainInteraction, handler(self, self.invokeSelectButton), true)
  if not L_DeviceTpl:getIsMobile() then
    self._mouseScrollWheelHandle = handler(self, self.onListen_mouseScrollWheel)
    C_InputManager.AddSingleAxisListener(C_InputManager.EInputSingleAxisType.EMainMouseScroll, self._mouseScrollWheelHandle)
    self.selectedIndex = 1
    self.selectedOption = 1
  end
  self._entity2ButtonDatas = {}
  self._currCropState = L_Const.homeCropHudType.None
  self:refreshButtons()
end

function this:hide()
  if not L_DeviceTpl:getIsMobile() then
    L_CameraManager:setVirCameraScaleView(true)
  end
end

function this:refreshButtons()
  local isShow = L_InteractiveManager:getButtonIsShow()
  if not isShow then
    self.bind.list_button:clear()
    self.bind.active_buttonBg = false
    if not L_DeviceTpl:getIsMobile() then
      L_CameraManager:setVirCameraScaleView(true)
    end
    return
  end
  local tmp = {}
  for i, v in pairs(L_InteractiveManager:getButtonDatas()) do
    for i2, v2 in ipairs(v) do
      table.insert(tmp, {
        index = #tmp + 1,
        interData = v2
      })
    end
  end
  for entity, interactList in pairs(L_InteractiveManager:getNewInteractDatas()) do
    local sorted = {}
    for sId, data in pairs(interactList) do
      if data.hide == nil or not data.hide then
        table.insert(sorted, {sId = sId, data = data})
      end
    end
    table.sort(sorted, function(a, b)
      return a.data.priority > b.data.priority
    end)
    for i = 1, #sorted do
      local sortData = sorted[i]
      if interactList[sortData.sId] then
        table.insert(tmp, {
          index = #tmp + 1,
          interData = interactList[sortData.sId]
        })
      end
    end
  end
  if #self.modules.list_button == #tmp then
    local hasChange = false
    for i, module in ipairs(self.modules.list_button) do
      local interData = module.bind.interData
      local interDataN = tmp[i].interData
      if interData.entity ~= interDataN.entity or interData.name ~= interDataN.name or interData.sId ~= interDataN.sId then
        hasChange = true
        break
      end
    end
    if not hasChange then
      return
    end
  end
  self:endPress()
  self.bind.list_button:clear()
  if not L_DeviceTpl:getIsMobile() then
    L_CameraManager:setVirCameraScaleView(#tmp <= 1)
  end
  self.bind.active_buttonBg = 1 < #tmp and L_DeviceTpl:getIsPc()
  self.bind.size_buttonBg = C_Vector2(660, 320 + #tmp * 63)
  self.bind.list_button:insert_array(tmp)
  self.selectedIndex = 1
  self:_refreshButtons()
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.generalNode)
end

function this:_refreshButtons()
  if not L_DeviceTpl:getIsPc() then
    return
  end
  local selectIndex = self.selectedIndex or 0
  self.bind.size_line1 = C_Vector2(100, -10 + selectIndex * 63)
  self.bind.size_line2 = C_Vector2(100, 53 + (#self.modules.list_button - selectIndex) * 63)
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.bgLineNode)
  for _, button in ipairs(self.modules.list_button) do
    button:refreshState()
  end
end

function this:onListen_mouseScrollWheel(dv)
  if self._pressing then
    return
  end
  self.selectedIndex = self.selectedIndex + (0 < dv and -1 or dv < 0 and 1 or 0)
  self.selectedIndex = math.clamp(self.selectedIndex, 1, #self.modules.list_button)
  self:_refreshButtons()
end

function this:invokeSelectButton(isPress)
  local isShow = L_InteractiveManager:getButtonIsShow()
  if isShow and self.modules.list_button[self.selectedIndex] then
    if isPress then
      if not self.modules.list_button[self.selectedIndex]:invokeClick() then
        self.modules.list_button[self.selectedIndex]:startPress()
      end
    else
      self:endPress()
    end
  end
end

function this:setPressState(s)
  self._pressing = s
end

function this:endPress()
  for _, module in ipairs(self.modules.list_button) do
    module:doEndPress()
  end
  self._pressing = false
end

return this
