local this = {}

function this.socket_5011(data, errorCode)
  L_PlayerStore:setPlayerId(data.player_id)
end

function this.socket_5001(data, errorCode)
  L_FormationManager:setReviveHeroState(nil)
  if errorCode == L_Const.errorCode.ErrCodeSucc then
    if data.reconnect then
      this.initGame(data, errorCode)
    else
      if data.time_zone and data.time and data.time_msec then
        C_TimeUtility.SyncServerTime(data.time, data.time_msec, data.time_zone)
      end
      local content = tostring(data.player_id .. " " .. L_TimeUtil.getWaterMaskTime())
      C_AlibabaCloudHelper.LoadWaterMask("", content, function(status, code)
        if status == true then
          this.initGame(data, errorCode)
        elseif L_LoginManager:waterMaskFailed() then
          L_LoginManager:logoutAccount()
          L_GameUtil.showCommonTip({
            txtContent = L_WordsTpl:getValue("ui_hintWatermark_01", {
              [0] = code
            }),
            hideConfirm = true,
            hideCancel = true
          })
        else
          local startTime = L_GameConstTpl:getValue("WATER_MASK_START_TIME", true, 60)
          if startTime < 45 then
            startTime = 45
          end
          L_LoginManager:startReloadWaterMask(startTime, content)
          this.initGame(data, errorCode)
        end
      end)
    end
  else
    this.initGame(data, errorCode)
  end
end

function this.socket_5007(data, errorCode)
  if errorCode == L_Const.errorCode.ErrCodeSucc then
    this.initGame(data, errorCode)
  end
end

function this.socket_5008(data, errorCode, isInit)
  if data == nil then
    return
  end
  local bagInfos = data.sbag_infos
  if bagInfos then
    L_BagStore:rsp_syncBagInfo(bagInfos)
  end
  local basicInfo = data.basic_info
  L_PlayerStore:rsp_syncPlayerInfo(data)
  local spiritron_infos = data.spiritron_infos
  if spiritron_infos then
    L_SpiritronStore:rsp_syncSpiritron(spiritron_infos, isInit)
  end
  local redpoints = data.redpoints
  if redpoints then
    L_RedDotHelper:syncFromProto(redpoints)
  end
  local guide_infos = data.guide_infos
  if guide_infos then
    L_GuideStore:rsp_syncGuideData(guide_infos)
  end
  local weapon_app_infos = data.weapon_app_infos
  if weapon_app_infos then
    L_WeaponStore:rsp_syncWeapon(weapon_app_infos)
  end
  local systemLock = basicInfo and basicInfo.lock_system
  if systemLock then
    L_SystemUnlockStore:rsp_syncLockSystem(systemLock)
  end
end

function this.socket_5101()
  L_GameStore:callDayChange()
end

function this.socket_10006(data, errorCode)
  if not data or not data.heros then
    return
  end
  for _, v in ipairs(data.heros) do
    if v.type == L_Const.FightObjType.FO_Hero then
      L_HeroStore:rsp_syncHeroInfoes(v)
    elseif v.type == L_Const.FightObjType.FO_Pet then
      L_PetStore:rsp_syncPetInfos(v)
    elseif v.type == L_Const.FightObjType.FO_Trial_Pet then
      L_PetStore:rsp_syncPetInfos(v)
    end
  end
end

function this.initGame(data, errorCode)
  L_Net:onLogin(data.reconnect, errorCode, data.data ~= nil)
  L_LoginManager:waterMaskCountDelete()
  if errorCode == L_Const.errorCode.ErrCodeSucc then
    this.enterGame(data, errorCode)
  elseif errorCode == L_Const.errorCode.ErrCodeHaveNotCreatedPlayer then
    this.createPlayerFinish(data)
  else
    L_LoadingManager:closeLoading()
  end
  L_GameStore:dealEnterGame()
end

function this.enterGame(data, errorCode)
  if data.time_zone and data.time and data.time_msec then
    C_TimeUtility.SyncServerTime(data.time, data.time_msec, data.time_zone)
  end
  this.socket_5008(data.data, errorCode, true)
  if data.data then
    if data.reconnect then
      if not L_PlayerStore:getIsNewPlayer() or L_LoginManager:getOpenInterim() then
        L_UI:backToRoot()
        AzurWorld.service:RequestEnterScene(AzurWorld.proxyCenter.defaultSceneId, 0)
      end
    else
      L_SettingStore:injectAndApplyCache(L_PlayerStore:getPlayerId())
      if L_PlayerStore:getIsNewPlayer() then
        if L_LoginManager:getOpenInterim() then
          AzurWorld.proxyCenter:RequestTransfer(L_GameConstTpl:getValue("INIT_SWITCHSCENE_ID", true, 102))
        else
          L_LoginManager:notOpenInterim()
        end
      else
        AzurWorld.service:RequestEnterScene(AzurWorld.proxyCenter.defaultSceneId, 0)
        L_PhotoManager:checkHeadTex()
      end
    end
    L_ReddotManager:setAllLeavesDirty()
    L_GuideManager:startAllGuide()
  else
    L_LoadingManager:closeLoading()
  end
end

function this.createPlayerFinish(data)
  L_LoadingManager:closeLoading()
  L_GameStore:req_createPlayer()
end

return this
