local this = class("shortCutManager", G_EventManagerBase)
local PATH_SHORTKEY = "UI/Pages/ShortCut/partShortKey.prefab"

function this:ctor()
  this.super.ctor(self)
  self.pageShortCutDic = {}
  self.cutTime = 0
  self._shortCutFlagGos = {}
end

function this:initialize()
  C_UIMgr.RegisterEscShortCut()
end

local persistentShortCut = {interactive = true}

function this:dispose()
  for i, v in pairs(self.pageShortCutDic) do
    if not persistentShortCut[i] then
      self:removeShortCut(i)
    end
  end
end

function this:registerPageMainShortCut(curModule)
  if curModule == nil then
    curModule = L_WorldManager:getCurModule()
  end
  if string.isEmpty(curModule) then
    return
  end
  local mainCityPage = L_Const.worldData[curModule].ui
  local shortCutData = L_ShortCutConst.mainCityKeyMap[curModule]
  self:registerShortCutMapList(mainCityPage, shortCutData.shortCutList)
  self:registerShortCut(mainCityPage, C_InputManager_KeyType.EMainBack, function()
    if not string.isEmpty(shortCutData.menuPage) then
      L_UI:open(shortCutData.menuPage)
    end
  end)
end

function this:registerShortCutMapList(pageName, shortCutList)
  for i, v in pairs(shortCutList) do
    self:registerShortCut(pageName, v, L_ShortCutConst.shortCutMap[v], L_ShortCutConst.shortCutMapNeedFlag[v])
  end
end

function this:registerShortCut(pageName, type, func, needFlag)
  if not self.pageShortCutDic[pageName] then
    self.pageShortCutDic[pageName] = {}
  end
  if not self.pageShortCutDic[pageName].shortCutHandle then
    self.pageShortCutDic[pageName].shortCutHandle = {}
  end
  local callback = function(self, isPress)
    printf("ShortCut", type, isPress)
    if not self:getIsShortCutAllow() then
      return
    end
    if needFlag then
      func(isPress)
    elseif isPress then
      func()
    end
  end
  local handle = self.pageShortCutDic[pageName].shortCutHandle[type] or handler(self, callback)
  C_InputManager.AddKeyListener(type, handle)
  self.pageShortCutDic[pageName].shortCutHandle[type] = handle
end

function this:removeShortCut(pageName)
  if self.pageShortCutDic[pageName] then
    local handleList = self.pageShortCutDic[pageName].shortCutHandle
    if not table.isEmpty(handleList) then
      for i, v in pairs(handleList) do
        C_InputManager.RemoveKeyListener(i, v)
      end
    end
    self.pageShortCutDic[pageName] = nil
  end
end

function this:generateShortKey(parent)
  local go = L_ResPool:syncGameObject(PATH_SHORTKEY, parent)
  local part = require("ui.pages.shortCut.partShortKey").new()
  part:created(go)
  return part
end

function this:generateAndCheckShortKey(parent, offset, scale)
  if not L_DeviceTpl:getIsPc() then
    return
  end
  local part = self:generateShortKey(parent)
  part:refreshTransform(parent, offset, scale)
  return part
end

function this:addShortKeyFlag(obj, key, anchorType, parent, offset, scale)
  if not L_DeviceTpl:getIsPc() then
    return
  end
  local part = self._shortCutFlagGos[obj] and self._shortCutFlagGos[obj][key] or self:generateShortKey(parent)
  part:refreshTransform(parent, anchorType, offset, scale)
  part:refreshKey(key)
  self._shortCutFlagGos[obj] = self._shortCutFlagGos[obj] or {}
  self._shortCutFlagGos[obj][key] = part
  return part
end

function this:destroyShortKey(partShortKey)
  local go = partShortKey.gameObject
  partShortKey:destroy()
  C_GameObject.Destroy(go)
end

function this:clearShortKeyFlag(obj)
  for _, part in pairs(self._shortCutFlagGos[obj] or {}) do
    self:destroyShortKey(part)
  end
  self._shortCutFlagGos[obj] = nil
end

function this:getIsShortCutEscAllow()
  if L_LoadingManager:isLoading() or L_Net:isWaitingNetOption() then
    return false
  end
  return true
end

function this:getIsShortCutAllow()
  if L_LoadingManager:isLoading() or L_Net:isWaitingNetOption() then
    return false
  end
  return true
end

return this
