local network = class("network")
local tag = "auxiliary:network"
local errorMsgCode = require("proto.ErrCode")
local _errorCodeTpl = L_GameTpl:getErrorCodeTpl()
local C_NetManager = CS.Azur.Gameplay.Core.Net.NetManager

function network:initialize()
  L_Net:addListener(MsgGenCode.CSProtoRetrun2Login, function(rsp, errorCode)
    L_Net:dispose(C_NetChannel.Main)
    local func = function()
      if errorCode == errorMsgCode.ErrCodeKickByGS then
        self:onKicked()
        return
      end
      if errorCode == errorMsgCode.ErrCodeKickPrevLogin then
      else
        self:back2Login()
      end
    end
    if errorCode == 107 then
      self:back2Login()
    end
    local tpl = _errorCodeTpl:getTplById(errorCode)
    local paras = {}
    if errorCode == errorMsgCode.ErrCodeBan then
      local banTime = rsp.u32s[1]
      if banTime then
        paras[0] = L_TimeUtil.getDisplayTime(banTime)
      else
        errorf("\229\144\142\231\171\175\230\178\161\229\143\145\229\176\129\231\166\129\230\151\182\233\151\180")
      end
    end
    local content = _errorCodeTpl:getName(tpl, paras)
    self:showNetCommonTip({
      txtTitle = L_WordsTpl:getValue("ui_logout_text"),
      txtContent = content,
      closeCallback = func,
      hideConfirm = true,
      hideCancel = true
    })
    return true
  end, C_NetChannel.Main)
  L_Net:addListener(MsgGenCode.CSProtoKickOffline, function(rsp, errorCode)
    L_Net:dispose(C_NetChannel.Main)
    local func = function()
      self:onKicked()
    end
    local tpl = _errorCodeTpl:getTplById(errorCode)
    local paras = {}
    if errorCode == errorMsgCode.ErrCodeBan then
      local banTime = rsp.u32s[1]
      if banTime then
        paras[0] = L_TimeUtil.getDisplayTime(banTime)
      else
        errorf("\229\144\142\231\171\175\230\178\161\229\143\145\229\176\129\231\166\129\230\151\182\233\151\180")
      end
    end
    self:showNetCommonTip({
      txtContent = _errorCodeTpl:getName(tpl, paras),
      closeCallback = func,
      hideConfirm = true,
      hideCancel = true
    })
    return true
  end, C_NetChannel.Main)
  C_CDNManager.AddSendAction(handler(self, function()
    C_NetManager.ShowNetMask("faceImgUpload start")
  end))
  C_CDNManager.AddSuccessAction(handler(self, function()
    C_NetManager.HideNetMask("faceImgUploadSuccess")
  end))
  C_CDNManager.AddFailedAction(handler(self, function()
    C_NetManager.HideNetMask("faceImgUploadFailed")
  end))
end

function network:back2Login()
  errorf("\229\183\178\231\153\187\229\135\186\239\188\129")
  L_LoginManager:logoutAccount()
end

function network:onKicked()
  errorf("\232\162\171\232\184\162\228\184\139\231\186\191\239\188\129")
  L_LoginManager:onKicked()
end

function network:prepareConnect(channel)
end

function network:showNetCommonTip(data)
  L_Net:showNetCommonTip(data)
end

return network
