package com.adealink.weparty.account.constant

import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.IError
import com.adealink.frame.base.Rlt
import com.adealink.frame.util.ONE_HOUR
import com.adealink.weparty.account.R
import com.adealink.weparty.account.login.data.LoginResult
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.network.ServerCode
import kotlin.math.ceil
import com.adealink.weparty.R as APP_R


//模块：account，功能：login，错误：auth_error
class AccountLoginAuthError(val statusCode: Int?) : IError(getCompatString(R.string.account_auth_error))

//模块：account，功能：login，错误：同一个设备注册多个账号限制
class AccountLoginRegisterLimitError : IError(getCompatString(R.string.account_register_limit))

//模块：account，功能：login，错误：因为%AA，你被封禁账号%BB
class AccountBannedError(reason: String, banTime: String) :
    IError(msg = getCompatString(R.string.account_login_banned,
        reason,
        banTime))

//模块：account，功能：login，错误：服务端请求第三方授权失败
class AccountServerAuthError : IError(getCompatString(R.string.account_server_auth_failed))

//模块：account，功能：login，错误：服务端操作错误
class AccountServerOpError : IError(getCompatString(R.string.account_server_op_failed))

//模块：account，功能：login，错误：服务端异常
class AccountServerExceptionError : IError(getCompatString(R.string.account_server_exception))

/**
 * 未注册
 */
class AccountNoRegisterError : IError()

/**
 * 已注册、没有设置密码
 */
class AccountHasRegisterWithoutPasswordError : IError()

//模块：account，功能：login，错误：auth_cancel
class AccountLoginAuthCancelError : IError()

//模块：account，功能：login，错误：验证码无效
class AccountInvalidVerifyCodeError : IError(getCompatString(R.string.account_invalid_verify_code))

//模块：account，功能：login，错误：密码错误
class AccountPasswordError : IError(getCompatString(R.string.account_password_error))

//模块：account，功能：login，错误：密码频繁错误
class AccountPasswordFrequentlyError : IError(getCompatString(R.string.account_password_frequently_error))

//模块：account，功能：login，错误：验证码受限
class AccountVerifyCodeLimitError : IError(getCompatString(R.string.account_verfiy_code_limit))

class AccountLoginWithInvalidDeviceIdError : IError(getCompatString(R.string.account_login_with_invalid_device_id))

class AccountLoginWithVirtualApkError : IError(getCompatString(R.string.account_login_virtual_apk))

//模块：account，功能：login，错误：fb关联多个账号情况
class AccountFBSelectError : IError()

fun getLoginErrorByServerCode(error: IError): IError {
    val loginRes = error.data as? LoginResult
    return when (error.serverCode) {
        ServerCode.LOGIN_REGISTER_LIMIT.code -> AccountLoginRegisterLimitError()
//        ServerCode.USER_BANNED.code -> AccountBannedError(getBanedReason(loginRes?.reason), getBannedHours((loginRes?.expire ?: 0) * 1000))
        ServerCode.TOKEN_EXPIRE.code -> AccountServerAuthError()
        ServerCode.COMMON_OP_ERROR.code -> AccountServerOpError()
        ServerCode.SERVER_EXCEPTION.code -> AccountServerExceptionError()
        ServerCode.NOT_REGISTER.code -> AccountNoRegisterError()
        ServerCode.HAS_REGISTER_WITHOUT_PASSWORD.code -> AccountHasRegisterWithoutPasswordError()
        ServerCode.INVALID_VERIFY_CODE.code -> AccountInvalidVerifyCodeError()
        ServerCode.PASSWORD_ERROR.code -> AccountPasswordError()
        ServerCode.PASSWORD_ERROR_TOO_FREQUENTLY.code -> AccountPasswordFrequentlyError()
        ServerCode.VERIFY_CODE_LIMIT.code -> AccountVerifyCodeLimitError()
        ServerCode.LOGIN_WITH_INVALID_DEVICE_ID.code -> AccountLoginWithInvalidDeviceIdError()
        ServerCode.LOGIN_WITH_VIRTUAL_APK.code -> AccountLoginWithVirtualApkError()
        else -> IError(if (error.msg.isEmpty()) getCompatString(R.string.account_login_common_failed) else error.msg,
            error.serverCode)
    }
}

fun getBannedHours(timestamp: Long): String {
    if (timestamp <= 0) {
        return getCompatString(APP_R.string.common_hours, "9999999")
    }
    return getCompatString(
        APP_R.string.common_hours,
        ceil(timestamp.toFloat() / ONE_HOUR).toInt()
    )
}

fun showLoginFailedTip(rlt: Rlt.Failed) {
    showFailedToast(rlt)
}