local this = class("systemUnlockManager")
local _systemTpl = L_GameTpl:getSystemUnlockTpl()

function this:initialize()
  self.queue = L_Struct.getQueue()
  self.lockerDic = {}
end

function this:addSystem(systemId)
  local tpl = _systemTpl:getTplById(systemId)
  local tip = _systemTpl:getUnlockTips(tpl)
  if not string.isEmpty(tip) then
    if not self.queue then
      self.queue = L_Struct.getQueue()
    end
    self.queue:enQueue(systemId)
    if not L_UI:checkPageOpen("pageSystemUnlock") then
      L_UI:open("pageSystemUnlock")
    end
  else
    L_SystemUnlockStore:dispathchGuideTriggerEvent(systemId)
  end
end

function this:deQueueSystemUnlockId()
  return self.queue:deQueue()
end

function this:tryGenerateLocker(systemId)
  local isOpen = L_SystemUnlockStore:getSystemIsOpen(systemId)
  if isOpen then
    return
  end
  local locker = self:generateLocker(systemId)
  return locker
end

function this:generateLocker(systemId)
  local locker = require("ui.manager.system.systemLocker").new(systemId)
  self.lockerDic[locker] = locker
  return locker
end

function this:removeLocker(locker)
  locker:onDestroy()
  self.lockerDic[locker] = nil
end

function this:refreshLockers(systemId)
  if not self.lockerDic then
    return
  end
  for key, locker in pairs(self.lockerDic) do
    if locker.systemId == systemId then
      locker:unlock()
      self:removeLocker(locker)
    end
  end
end

function this:canButtonShow(systemId)
  local tpl = _systemTpl:getTplById(systemId)
  if tpl == nil then
    return true
  end
  return _systemTpl:getButtonShow(tpl)
end

function this:check(systemId)
  return L_SystemUnlockStore:getSystemIsOpen(systemId) and not L_TaskManager:checkIsDisableSystem(systemId, true)
end

function this:checkAndTip(systemId)
  local result = L_SystemUnlockStore:getOnlySystemUnlock(systemId)
  if not result then
    local tpl = _systemTpl:getTplById(systemId)
    local tip = _systemTpl:getLockTips(tpl)
    if tip then
      L_FlyMsgManager:showNormalMsg(tip)
    end
  elseif L_SystemBreakManager:checkBanSystemAndTip(systemId) then
    result = false
  elseif L_TaskManager:checkIsDisableSystem(systemId) then
    result = false
  end
  return result
end

function this:tryShowOrHide(systemId, dependent, isUnlock)
  local tpl = _systemTpl:getTplById(systemId)
  local isNeedShow = _systemTpl:getButtonShow(tpl)
  if not isNeedShow then
    dependent.gameObject:ActiveTrans(isUnlock)
  end
  return isNeedShow
end

function this:isSystemShowHelpButton(systemId)
  local tpl = _systemTpl:getTplById(systemId)
  return _systemTpl:isHelpButtonShow(tpl)
end

function this:getSystemUnlockConditionDesc(systemId)
  if self:check(systemId) then
    return string.format("systemId:%d \229\183\178\232\167\163\233\148\129\239\188\140\228\184\141\233\156\128\232\166\129\230\152\190\231\164\186\232\167\163\233\148\129\230\157\161\228\187\182", systemId)
  end
  local systemUnlockTpl = _systemTpl:getTplById(systemId)
  local unlockConditionList = _systemTpl:getUnlockCondi(systemUnlockTpl)
  for _, unlockCondition in pairs(unlockConditionList) do
    if not L_ConditionManager:singleIsComplete(unlockCondition) then
      return L_ConditionManager:getSingleDesc(unlockCondition)
    end
  end
  errorf(string.format("\231\179\187\231\187\159\230\156\170\232\167\163\233\148\129\239\188\140\228\189\134\232\167\163\233\148\129\230\157\161\228\187\182\229\133\168\233\131\189\230\187\161\232\182\179, systemId: %d", systemId))
  return string.format("\231\179\187\231\187\159\230\156\170\232\167\163\233\148\129\239\188\140\228\189\134\232\167\163\233\148\129\230\157\161\228\187\182\229\133\168\233\131\189\230\187\161\232\182\179, systemId: %d", systemId)
end

return this
