local debugActions = {}
local tag = "debugActions"
debugActions.isLightOpen = true
debugActions.isModelOpen = true
debugActions.isEffectOpen = true
debugActions.isOpenUwaLauncher = false
debugActions.isShowLogConsole = true
debugActions.refreshPet = true
debugActions.isUIOpen = true
debugActions.isDamageText = true

function debugActions.SendGMCommand(command)
  local commands = string.split(command, " ")
  local commandType = commands[1]
  if type(commandType) ~= "string" or not (0 < #commandType) then
    printf(tag, "[SendGMCommand] invalid command")
    return
  end
  local msg = {
    command = commandType,
    args = {}
  }
  for i = 2, #commands do
    table.insert(msg.args, commands[i])
  end
  L_Net:sendMessage(MsgGenCode.CSProtoGMCommand, msg, nil)
end

function debugActions.changeLightObjShow(type, isShow)
  local parentObj = Unity.GameObject.Find(type)
  if parentObj == nil then
    printf("changeTypeObjShow", "\230\156\170\230\137\190\229\136\176" .. type .. "\231\177\187\229\158\139")
    return
  end
  local objs = parentObj:GetComponentsInChildren(typeof(CS.UnityEngine.Light), true)
  for i = 0, objs.Length - 1 do
    objs[i].gameObject:SetActive(isShow)
  end
end

function debugActions.changeModelObjShow(type, isShow)
  local parentObj = Unity.GameObject.Find(type)
  if parentObj == nil then
    printf("changeTypeObjShow", "\230\156\170\230\137\190\229\136\176" .. type .. "\231\177\187\229\158\139")
    return
  end
  local objs = parentObj:GetComponentsInChildren(typeof(CS.UnityEngine.Renderer), true)
  for i = 0, objs.Length - 1 do
    objs[i].gameObject:SetActive(isShow)
  end
end

function debugActions.changeEffectObjShow(type, isShow)
  local parentObj = Unity.GameObject.Find(type)
  if parentObj == nil then
    printf("changeTypeObjShow", "\230\156\170\230\137\190\229\136\176" .. type .. "\231\177\187\229\158\139")
    return
  end
  local objs = parentObj:GetComponentsInChildren(typeof(CS.UnityEngine.ParticleSystem), true)
  for i = 0, objs.Length - 1 do
    objs[i].gameObject:SetActive(isShow)
  end
end

function debugActions.changeGuideActive(isActive)
  G_GuideManager.isActivateGuide = isActive
end

function debugActions.setLanguage_debug(lang)
  lang = lang and string.trim(lang)
  if not C_I18NConst.LanguageTag2Enum[lang] then
    errorf("\233\157\158\230\179\149\231\154\132\232\175\173\232\168\128\228\187\163\231\160\129")
    errorf(_langMap)
    errorf(lang)
    return
  end
  C_Define.language = lang
  C_LText.RefreshLanguage()
  C_LTextMeshProUGUI.RefreshLanguage()
  C_LLangImage.ChangeLanguage()
end

function debugActions.startWebRuntimeInspector(port)
  local inspector = debugActions._proximaInspector
  if inspector == nil then
    local go = C_GameObject("[Proxima Inspector]")
    go:SetActive(false)
    inspector = go:AddComponent(typeof(CS.Proxima.ProximaInspector))
    inspector.DisplayName = "(*^\226\128\148\194\176)b"
    inspector.UseHttps = true
    inspector.Certificate = nil
    inspector.StartOnEnable = false
    inspector.InstantiateStatusUI = false
    inspector.InstantiateConnectUI = false
    debugActions._proximaInspector = inspector
    go:SetActive(true)
  end
  local status = inspector.Status
  if status.IsRunning then
    L_GameUtil.showCommonTip({
      txtContent = L_WordsTpl:getValue("notice_ProximaInspector_01", {
        [0] = tostring(status.ConnectInfo),
        [1] = inspector.Password
      })
    })
  else
    local randomPW = string.format("%04d", math.random(0, 9999))
    inspector.Port = port or 7759
    inspector.Password = randomPW
    inspector:Run()
    local timer = Timer.once(1, function()
      if inspector.Status.IsRunning then
        L_GameUtil.showCommonTip({
          txtContent = L_WordsTpl:getValue("notice_ProximaInspector_02", {
            [0] = tostring(status.ConnectInfo),
            [1] = inspector.Password
          })
        })
      else
        L_GameUtil.showCommonTip({
          txtContent = L_WordsTpl:getValue("notice_ProximaInspector_03", {
            [0] = tostring(status.Error)
          })
        })
      end
    end)
    timer:start()
  end
end

function debugActions.stopWebRuntimeInspector()
  local inspector = debugActions._proximaInspector
  if inspector then
    inspector:Stop()
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_ProximaInspector_04"))
  end
end

local m_logConsoleHandle

function debugActions.showLogConsole(isShow)
  if not Unity.Debug.isDebugBuild then
    isShow = false
  end
  return isShow
end

function debugActions.exeLuaScriptString(luaScriptStr)
  if luaScriptStr then
    load(luaScriptStr)()
  end
end

debugActions._UICamCullingMaskBackup = nil
debugActions._UICamNearClipPlane = nil
debugActions._UICamFarClipPlane = nil

function debugActions.getUIVisible()
  return debugActions._UICamCullingMaskBackup == nil
end

function debugActions.setUIVisible(visible)
  if visible then
    if not debugActions.getUIVisible() then
      debugActions._UICamCullingMaskBackup = nil
      L_CameraManager:getUICamera().nearClipPlane = debugActions._UICamNearClipPlane
      L_CameraManager:getUICamera().farClipPlane = debugActions._UICamFarClipPlane
    end
  else
    debugActions._UICamNearClipPlane = L_CameraManager:getUICamera().nearClipPlane
    debugActions._UICamFarClipPlane = L_CameraManager:getUICamera().farClipPlane
    L_CameraManager:getUICamera().nearClipPlane = 1
    L_CameraManager:getUICamera().farClipPlane = 1.01
    debugActions._UICamCullingMaskBackup = true
  end
end

function debugActions.transfer(pos)
  local ret = string.toVector3(pos, " ")
  L_PlayerManager:transfer(ret)
end

function debugActions.checkAndTransfer(pos)
  local sceneId = AzurWorldInstance.CurWorldId
  if sceneId ~= 100 then
    local num = 1
    Timer.once(1, function()
      AzurWorld.proxyCenter:RequestTransfer(100)
      printf("\228\184\141\229\156\168100\229\156\186\230\153\175\239\188\140\229\133\136\228\188\160\233\128\129\229\136\176100\229\156\186\230\153\175")
    end)
    num = num + 8
    Timer.once(num, function()
      debugActions.transfer(pos)
      printf("\229\134\141\228\188\160\233\128\129\229\136\176\229\175\185\229\186\148\229\156\186\230\153\175\231\154\132\229\155\186\229\174\154\228\189\141\231\189\174")
    end)
  else
    debugActions.transfer(pos)
  end
end

return debugActions
