local this = class("interactiveManager", G_EventManagerBase)
this:importPartialClass(require("ui.manager.interactive.interactiveManagerHandles"))
this.event = {
  onButtonDataChange = "onButtonDataChange",
  onBuildButtonChange = "onBuildButtonChange"
}
local CONFIG_PAGE = require("ui.config").pages
local _wroldCityTpl = L_GameTpl:getWorldCityTpl()
local _interactiveTpl = L_GameTpl:getWorldInteractivetypeTpl()
local _battleTpl = L_GameTpl:getBattleTpl()
local _transferTpl = L_GameTpl:getWorldBorthposTpl()
local pageNoButton = {
  pageFaceIn = true,
  pageHomePlayerEdit = true,
  pageHomePlayerEditCS = true,
  pageWorldBattle = true,
  dialogWindow = true,
  pageShop = true,
  pageBattlePetCatch = true
}

function this:ctor()
  self.super.ctor(self)
  self._entity2ButtonDatas = {}
  self._triggerBuildGuids = {}
  self._newEntity2BtnDatas = {}
  self._pageLock = {}
  self._moduleLock = {}
  self._entityInteractOptions = {}
end

function this:setInteract(L_interData)
  if L_interData.entity == nil then
    return
  end
  self._newEntity2BtnDatas[L_interData.entity] = self._newEntity2BtnDatas[L_interData.entity] or {}
  if L_interData.isIn then
    self._newEntity2BtnDatas[L_interData.entity][L_interData.sId] = L_interData
  else
    self._newEntity2BtnDatas[L_interData.entity][L_interData.sId] = nil
  end
  self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
end

function this:findInteractData(cData)
  if cData.entity == nil then
    return
  end
  if self._newEntity2BtnDatas[cData.entity] == nil then
    return
  end
  return self._newEntity2BtnDatas[cData.entity][cData.sId]
end

function this:changeInteractState(cData)
  local interData = self:findInteractData(cData)
  if interData == nil then
    return
  end
  interData.hide = cData.hide
  self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
end

function this:getIsLock()
  local result = next(self._pageLock) or next(self._moduleLock)
  if result then
  end
  return result
end

function this:getButtonIsShow()
  return not self:getIsLock()
end

function this:getNewInteractDatas()
  return self._newEntity2BtnDatas
end

function this:getButtonDatas()
  return self._entity2ButtonDatas
end

function this:getInteractOptions()
  return self._entityInteractOptions
end

function this:initialize()
  L_UI:open("interactive")
  L_UI:addListener(L_UI.pageEvent.preOpen, self._onShowPage, self)
  L_UI:addListener(L_UI.pageEvent.closed, self._onHidePage, self)
  L_SceneManager:addListener(L_SceneManager.event.suspendCurScene, function()
    self:_setModuleLock(L_Const.moduleControl.world, true)
  end)
  L_SceneManager:addListener(L_SceneManager.event.resumeCurSceneEnd, function()
    self:_setModuleLock(L_Const.moduleControl.world, false)
  end)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshMountStatus, function()
    self:_setModuleLock(L_Const.moduleControl.fly, L_PlayerManager:inMountFly())
  end)
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshPlayerStatus, function()
    self:_setModuleLock(L_Const.moduleControl.fly, L_PlayerManager:inMountFly())
  end)
  L_SceneManager:addListener(L_SceneManager.event.switchSceneEnd, function()
    self:_setModuleLock(L_Const.moduleControl.fly, L_PlayerManager:inMountFly())
  end)
  L_BattleManager:addListener(L_BattleManager.event.tmpPauseInput, function(pause)
    self:_setModuleLock(L_Const.moduleControl.heroUltimateSkill, pause)
  end)
  self:addModulesListener()
end

function this:resetLock()
  self._pageLock = {}
  self._moduleLock = {}
  self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
end

function this:_onShowPage(pageName)
  local cfg = CONFIG_PAGE[pageName]
  if not (cfg.uiType == "freedom" or cfg.controlMove) or pageNoButton[pageName] then
    self._pageLock[pageName] = true
    self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
  end
end

function this:_onHidePage(pageName)
  self._pageLock[pageName] = nil
  self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
end

function this:_setModuleLock(key, state)
  self._moduleLock[key] = state == true or nil
  self:sendEvent(L_InteractiveManager.event.onButtonDataChange)
end

function this:getInterNameByBevNode(interData, bevNode)
  local entity = interData.entity
  local interType = interData.interType
  local tpl = _interactiveTpl:getTplById(interType)
  local type = _interactiveTpl:getType(tpl)
  if type == L_Const.interactiveType.battle then
    local tpl = _battleTpl:getTplById(bevNode.battleId)
    return _battleTpl:getName(tpl)
  elseif type == L_Const.interactiveType.tranferPoint then
    local tpl = _transferTpl:getTplById(bevNode.pointId)
    return L_WordsTpl:getValue("ui_interactiveManager_07", {
      [0] = _transferTpl:getName(tpl)
    })
  elseif type == L_Const.interactiveType.transferScene then
    local tpl = _transferTpl:getTplById(bevNode.pointId)
    local sceneId = _transferTpl:getCityId(tpl)
    tpl = _wroldCityTpl:getTplById(sceneId)
    return L_WordsTpl:getValue("ui_interactiveManager_07", {
      [0] = _wroldCityTpl:getCity(tpl)
    })
  else
    return entity:getEntityName()
  end
end

return this
