local this = {}

function this.story_createDefaultPlayerModel()
  if not L_PlayerStore:getIsNewPlayer() then
    local player = L_PlayerManager:createCustomPlayerModel()
    return player
  end
  return nil
end

function this.story_isPageMaskOpen()
  local page = L_UI:getPage("pageMask")
  return page ~= nil
end

function this.story_openPageMask(data, killAll)
  if killAll then
    local page = L_UI:getPage("pageMask")
    if page ~= nil and page.cls then
      page.cls:killAll()
    end
  end
  local _fadeInCallBack
  if data.fadeInCallBack then
    function _fadeInCallBack(reason)
      data.fadeInCallBack(reason)
    end
  end
  local loadCompleteCallBack
  if data.loadCompleteCallBack then
    function loadCompleteCallBack(reason)
      data.loadCompleteCallBack(reason)
    end
  end
  local _callback
  if data.callback then
    function _callback(reason)
      data.callback(reason)
    end
  end
  L_GameUtil.openMask({
    raycast = true,
    coverdTime = data.coverdTime,
    fadeOut = data.fadeOut,
    fadeIn = data.fadeIn,
    isNewStory = data.isNewStory,
    isCompleteClose = data.isCompleteClose,
    isChangeColor = data.isChangeColor,
    fadeInCallBack = _fadeInCallBack,
    loadCompleteCallBack = loadCompleteCallBack,
    callback = _callback
  })
end

function this.story_fadeOutPageMask()
  local page = L_UI:getPage("pageMask")
  if page ~= nil then
    page.cls:DoStoryChangeState()
  end
end

function this.story_closePageMask()
  L_UI:close("pageMask")
end

function this.story_switchCamera()
  local blendMode = L_Const.blendStyleMode.easeIn
  local blendStyle = L_CameraManager:getBlendStyle(blendMode)
  blendStyle:stopBlend()
  local params = {time = 1000}
  blendStyle:init(params)
  L_CameraManager:switchTo(L_Const.cameraMode.story, blendStyle)
  this.story_freeCamera(false)
end

function this.story_revertCamera(onComplete)
  if L_CameraManager:getCameraMode() == L_Const.cameraMode.story then
    local tactics = L_CameraManager:getCurCamController()
    local povH, povV = tactics:getPovHV()
    local blendMode = L_Const.blendStyleMode.easeIn
    local blendStyle = L_CameraManager:getBlendStyle(blendMode)
    blendStyle:stopBlend()
    local params = {
      time = 1000,
      switchInParams = {povH = povH, povV = povV},
      callback = function()
        Timer.once(0.5, function()
          C_CameraManager.SetBrainBlendStyle(L_Const.blendStyle.cut)
          if onComplete ~= nil then
            onComplete()
          end
        end)
      end
    }
    blendStyle:init(params)
    L_CameraManager:revertCamController(blendStyle)
  elseif onComplete ~= nil then
    onComplete()
  end
end

function this.story_getPlayerSex()
  return L_PlayerStore:getSex() == L_Const.sexType.male and 0 or 1
end

function this.story_getPlayerName()
  return L_PlayerStore:getPlayerName()
end

function this.story_openUI(pageName, param)
  print("storyManager:" .. "StoryOpenUI," .. tostring(pageName) .. "," .. tostring(param))
  L_UI:open(pageName, {
    performanceId = tonumber(param),
    closeCB = function()
      C_LBlurStack.SetBlurRadius(0)
      AzurWorld.StoryMgr:SkipStory()
    end
  })
end

function this.story_closeUI(pageName)
  print("storyManager:" .. "StoryCloseUI," .. tostring(pageName))
  L_UI:close(pageName)
end

function this.story_SetCameraInputDrag(posX, posY)
  if L_CameraManager:getCameraMode() == L_Const.cameraMode.story then
    L_CameraManager:getCameraTactics():SetCameraInputDrag(posX, posY)
  end
end

function this.story_freeCamera(useWorldCamera)
  local cameraTactics = L_CameraManager:getCameraTactics()
  if useWorldCamera then
    return
  end
  if L_CameraManager.lastCamController == nil then
    printf("storyManager", "freeCamera error lastCamController is nil ")
    return
  end
  local lastCameraTactics = L_CameraManager.lastCamController:getCameraTactics()
  if lastCameraTactics == nil then
    printf("storyManager", "freeCamera error lastCameraTactics is nil ")
    return
  end
  cameraTactics:SetFreeRotationCamera(1, 3, 90, -50)
  local lookAtPos
  if lastCameraTactics.cineMachineCamera then
    if lastCameraTactics.cineMachineCamera.LookAt then
      lookAtPos = L_Vector3.getRelativePos(lastCameraTactics.cineMachineCamera.LookAt)
    else
      printf("storyManager", "freeCamera error lastCameraTactics.cineMachineCamera.LookAt is nil ")
    end
    if lastCameraTactics.cineMachineCamera.m_Lens then
      cameraTactics.cineMachineCamera.m_Lens = lastCameraTactics.cineMachineCamera.m_Lens
      cameraTactics:SetCameraFov(math.max(cameraTactics.cineMachineCamera.m_Lens.FieldOfView - 15, 45))
    end
  else
    printf("storyManager", "freeCamera error lastCameraTactics.cineMachineCamera is nil ")
  end
  if lookAtPos ~= nil then
    cameraTactics:SetLookAtPosition(lookAtPos)
  else
    printf("storyManager", "freeCamera error lookAtPos is nil ")
  end
  if lastCameraTactics.transposer then
    cameraTactics:SetCameraTrackedObjectOffset(lastCameraTactics.transposer.m_TrackedObjectOffset)
    cameraTactics:SetCameraDis(lastCameraTactics.transposer.m_CameraDistance)
  else
    printf("storyManager", "freeCamera error lastCameraTactics.transposer is nil ")
  end
  cameraTactics:SetCameraDragActive(true)
  if lastCameraTactics then
    local povH = lastCameraTactics:GetCameraPovH()
    local povV = lastCameraTactics:GetCameraPovV()
    cameraTactics:SetCameraPOVHV(povH, povV)
  end
  cameraTactics:SetCameraActive(true)
end

function this.story_registerShortCut(pageName, type, func)
  L_ShortCutManager:registerShortCut(pageName, type, function()
    if func then
      func()
    end
  end)
end

function this.story_unregisterShortCut(pageName)
  L_ShortCutManager:removeShortCut(pageName)
end

function this.story_setInputmap_cur()
  local uiListener = L_UI._auxiliaries.uiListener.cls
  C_InputManager.SetInputmap(uiListener.curMapType, uiListener.enableUI)
end

function this.story_showCommonTip(title, closeCallback, cancelCallback, confirmCallback)
  L_GameUtil.showCommonTip({
    txtContent = title,
    closeCallback = function()
      if closeCallback then
        closeCallback()
      end
    end,
    cancelCallback = function()
      if cancelCallback then
        cancelCallback()
      end
    end,
    confirmCallback = function()
      if confirmCallback then
        confirmCallback()
      end
    end,
    showNoMoreType = L_Const.ShowNoMoreType.curLogin,
    overrideSortingOrder = 1000
  })
end

return this
