package com.aiosman.ravenow.data.api

import android.content.Context
import android.widget.Toast
import com.aiosman.ravenow.R

//
enum class ErrorCode(val code: Int) {
    USER_EXIST(40001),
    USER_NOT_EXIST(40002),
    InvalidateCaptcha(40004),
    IncorrectOldPassword(40005),
    // 未知错误
    UNKNOWN(99999)
}

fun ErrorCode.toErrorMessage(context: Context): String {
    return context.getErrorMessageCode(code)
}

fun ErrorCode.showToast(context: Context) {
    Toast.makeText(context, toErrorMessage(context), Toast.LENGTH_SHORT).show()
}

// code to ErrorCode
fun Int.toErrorCode(): ErrorCode {
    return when (this) {
        40001 -> ErrorCode.USER_EXIST
        40002 -> ErrorCode.USER_NOT_EXIST
        40004 -> ErrorCode.InvalidateCaptcha
        40005 -> ErrorCode.IncorrectOldPassword
        else -> ErrorCode.UNKNOWN
    }
}

fun Context.getErrorMessageCode(code: Int?): String {
    return when (code) {
        40001 -> getString(R.string.error_10001_user_exist)
        ErrorCode.IncorrectOldPassword.code -> getString(R.string.error_incorrect_old_password)
        else -> getString(R.string.error_unknown)
    }
}