package com.pixelized.server.lwa.utils.extentions

import com.pixelized.server.lwa.server.exception.MissingParameterException
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.http.Parameters

inline fun <reified T> Parameters.param(
    name: String,
    code: APIResponse.ErrorCode,
): T {
    return when (T::class) {
        String::class -> this[name] as? T
        Float::class -> this[name]?.toFloatOrNull() as? T
        Boolean::class -> this[name]?.toBooleanStrictOrNull() as? T
        else -> null
    } ?: throw MissingParameterException(
        name = name,
        code = code,
    )
}

val Parameters.characterSheetId: String
    get() = param(
        name = "characterSheetId",
        code = APIResponse.ErrorCode.CharacterSheetId,
    )

val Parameters.alterationId: String
    get() = param(
        name = "alterationId",
        code = APIResponse.ErrorCode.AlterationId,
    )

val Parameters.inventoryId: String
    get() = param(
        name = "inventoryId",
        code = APIResponse.ErrorCode.InventoryId,
    )

val Parameters.itemId: String
    get() = param(
        name = "itemId",
        code = APIResponse.ErrorCode.ItemId,
    )

val Parameters.mapId: String
    get() = param(
        name = "mapId",
        code = APIResponse.ErrorCode.MapId,
    )

val Parameters.count: Float
    get() = param(
        name = "count",
        code = APIResponse.ErrorCode.Count,
    )

val Parameters.create: Boolean
    get() = param(
        name = "create",
        code = APIResponse.ErrorCode.Create,
    )

val Parameters.active: Boolean
    get() = param(
        name = "active",
        code = APIResponse.ErrorCode.Active,
    )

val Parameters.equip: Boolean
    get() = param(
        name = "equip",
        code = APIResponse.ErrorCode.Equip,
    )