local cls = class("pageEntrustTaskEntrance", G_UIPageBase)
local _dungeonEntrustTypeTpl = L_GameTpl:getDungeonEntrustTypeTpl()

function cls.bind()
  return {
    list_entrance = {
      moduleName = "pages/entrustTask/cellEntrustTaskEntrance"
    },
    moduleCommonTop = {
      moduleName = "modulePages/moduleCommonTop"
    },
    active_tips = false,
    txt_tips = ""
  }
end

function cls.methods()
  return {}
end

function cls:preOpen(options)
  cls.super.preOpen(self, options)
  if options and options.npcId then
    local entity = AzurWorld.WorldMapMgr:TryGetEntity(tonumber(options.npcId))
    if not entity then
      errorf("entity is nil, npcId: " .. tostring(options.npcId))
    else
      self:loadVirtualCamera(entity)
      L_AudioUtil.playSound("Play_VO_System_108001_EnterInterface_01")
      local transform = entity:GetTransform()
      self.lookAtCom = transform:GetComponent(typeof(CS.Lens.Gameplay.Utility.LookAt.FinalIK.LookAtIK))
      if L_CommonUtil.isValid(self.lookAtCom) then
        self.lookAtCom.solver.target = L_CameraManager:getVirCamera().gameObject.transform
      end
      self.lookAtCtrlCom = transform:GetComponent(typeof(CS.Lens.Gameplay.Utility.LookAt.FinalIK.LookAtController))
      if L_CommonUtil.isValid(self.lookAtCtrlCom) then
        self.lastAngleLimit = self.lookAtCtrlCom.lookAtAngleLimit
        self.lookAtCtrlCom.lookAtAngleLimit = 360
      end
    end
  end
  local data = {
    name = "\229\156\176\229\140\186\229\167\148\230\137\152",
    helpBtnBool = false,
    mainBtnBool = false,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end,
    helpBtn = function()
    end,
    mainBtn = function()
      L_UI:backToRoot()
    end
  }
  self.modules.moduleCommonTop:initModule(data)
end

function cls:loadVirtualCamera(entity)
  L_CameraManager:switchToNpcEntrustTask()
  L_CameraManager:bindEntity(entity)
  L_PlayerManager:showOrHideAllPlayer(false)
  AzurWorld.HUDMgr:SetAllVisible(false, "PageEntrustTask")
  L_EntityManager:showOrHideRoot(L_Const.WorldNode.PLAYERPETNODE, false)
end

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

function cls:open(options)
  self:initPage()
  self:clearTimer()
  if options and options.npcId then
    self.timer = Timer.once(1, function()
      self.bind.active_tips = true
      self.timer1 = Timer.once(3, function()
        self.bind.active_tips = false
      end, self)
    end, self)
  end
end

function cls:clearTimer()
  if self.timer then
    Timer.remove(self.timer)
    self.timer = nil
  end
  if self.timer1 then
    Timer.remove(self.timer1)
    self.timer1 = nil
  end
end

function cls:initPage()
  self.bind.txt_tips = L_WordsTpl:getValue("ui_dungeon_entrust_01")
  local onClick_select = function(cell)
    local result = L_SystemUnlockManager:checkAndTip(_dungeonEntrustTypeTpl:getSystemUnLockId(cell.bind.cfgData))
    if not result then
      return
    end
    L_UI:open("pageEntrustChallenge", {
      entrustType = cell.bind.typeId
    })
  end
  local info = {}
  for i, tpl in ipairs(_dungeonEntrustTypeTpl:getShowData()) do
    table.insert(info, {
      typeId = _dungeonEntrustTypeTpl:getId(tpl),
      cbk = onClick_select,
      cfgData = tpl,
      toggle_line = i ~= 1
    })
  end
  self.bind.list_entrance:clear()
  self.bind.list_entrance:insert_array(info)
end

function cls:unloadVirtualCamera()
  L_PlayerManager:showOrHideAllPlayer(true)
  local blend = L_CameraManager:getBlendStyle(L_Const.blendStyleMode.custom)
  blend.time = 1000
  L_CameraManager:revertCamController(blend)
  AzurWorld.HUDMgr:SetAllVisible(true, "PageEntrustTask")
  L_EntityManager:showOrHideRoot(L_Const.WorldNode.PLAYERPETNODE, true)
end

function cls:close(options)
  cls.super.close(self, options)
  self:clearTimer()
  if options and options.npcId then
    self:unloadVirtualCamera()
    if L_CommonUtil.isValid(self.lookAtCom) then
      self.lookAtCom.solver.target = nil
    end
    if L_CommonUtil.isValid(self.lookAtCtrlCom) then
      self.lookAtCtrlCom.lookAtAngleLimit = self.lastAngleLimit
    end
  end
end

return cls
