package com.manalejandro.odb2bluetooth.data.obd

/**
 * Generic OBD2 commands for diagnostics
 */
object ObdCommands {

    /**
     * Generic safe diagnostic commands
     */
    val SAFE_DIAGNOSTIC_COMMANDS = listOf(
        ObdCommand(
            name = "Vehicle Speed",
            command = "010D",
            description = "Current vehicle speed in km/h",
            unit = "km/h",
            isSafe = true
        ),
        ObdCommand(
            name = "Engine RPM",
            command = "010C",
            description = "Engine revolutions per minute",
            unit = "RPM",
            isSafe = true
        ),
        ObdCommand(
            name = "Coolant Temperature",
            command = "0105",
            description = "Engine coolant temperature",
            unit = "°C",
            isSafe = true
        ),
        ObdCommand(
            name = "Throttle Position",
            command = "0111",
            description = "Throttle position percentage",
            unit = "%",
            isSafe = true
        ),
        ObdCommand(
            name = "Fuel Level",
            command = "012F",
            description = "Fuel tank level input",
            unit = "%",
            isSafe = true
        ),
        ObdCommand(
            name = "Intake Air Temperature",
            command = "010F",
            description = "Intake air temperature",
            unit = "°C",
            isSafe = true
        ),
        ObdCommand(
            name = "MAF Air Flow Rate",
            command = "0110",
            description = "Mass air flow sensor air flow rate",
            unit = "g/s",
            isSafe = true
        ),
        ObdCommand(
            name = "Engine Load",
            command = "0104",
            description = "Calculated engine load value",
            unit = "%",
            isSafe = true
        )
    )

    /**
     * DTC (Diagnostic Trouble Codes) commands
     */
    val DTC_COMMANDS = listOf(
        ObdCommand(
            name = "Read DTCs",
            command = "03",
            description = "Request diagnostic trouble codes",
            unit = "",
            isSafe = true
        ),
        ObdCommand(
            name = "Clear DTCs",
            command = "04",
            description = "Clear diagnostic trouble codes and stored values",
            unit = "",
            isSafe = false
        ),
        ObdCommand(
            name = "Pending DTCs",
            command = "07",
            description = "Request pending diagnostic trouble codes",
            unit = "",
            isSafe = true
        )
    )

    /**
     * Parse OBD2 response for common PIDs
     */
    fun parseResponse(command: String, response: String): String? {
        if (response.contains("NO DATA") || response.contains("ERROR")) {
            return null
        }

        // Remove spaces and common prefixes
        val cleanResponse = response.replace(" ", "").replace("41", "").replace("43", "")

        return when (command) {
            "010C" -> { // RPM
                try {
                    val value = cleanResponse.substring(2, 6).toInt(16)
                    "${value / 4} RPM"
                } catch (e: Exception) { null }
            }
            "010D" -> { // Speed
                try {
                    val value = cleanResponse.substring(2, 4).toInt(16)
                    "$value km/h"
                } catch (e: Exception) { null }
            }
            "0105" -> { // Coolant temp
                try {
                    val value = cleanResponse.substring(2, 4).toInt(16) - 40
                    "$value °C"
                } catch (e: Exception) { null }
            }
            "010F" -> { // Intake air temp
                try {
                    val value = cleanResponse.substring(2, 4).toInt(16) - 40
                    "$value °C"
                } catch (e: Exception) { null }
            }
            "0111", "0104", "012F" -> { // Percentage values
                try {
                    val value = (cleanResponse.substring(2, 4).toInt(16) * 100) / 255
                    "$value %"
                } catch (e: Exception) { null }
            }
            "0110" -> { // MAF
                try {
                    val value = cleanResponse.substring(2, 6).toInt(16) / 100.0
                    String.format("%.2f g/s", value)
                } catch (e: Exception) { null }
            }
            else -> cleanResponse
        }
    }
}

/**
 * Data class representing an OBD command
 */
data class ObdCommand(
    val name: String,
    val command: String,
    val description: String,
    val unit: String,
    val isSafe: Boolean
)

