package mg.dot.feufaro.musicXML

import kotlinx.serialization.Serializable
import mg.dot.feufaro.solfa.Transpose
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlSerialName

@Serializable
@XmlSerialName("pitch ", "", "")
data class MXPitch (
@Serializable
@XmlElement
@XmlSerialName("octave", "", "")
    var octave: Int? = null,
@Serializable
@XmlElement
@XmlSerialName("step", "", "")
    var step: String? = null,
@Serializable
@XmlElement
@XmlSerialName("alter", "", "")
    var alter: Int? = null,
@Serializable
@XmlElement
var unpitched: String? = null,

) {
    companion object {
        val stepToSolfa: Map<String, String> = mapOf(
            "C" to "d",
            "D" to "r",
            "E" to "m",
            "F" to "f",
            "G" to "s",
            "A" to "l",
            "B" to "t"
        )
        val octaveToSolfa = listOf("₄", "₃", "₂", "₁", "", "¹", "²", "³", "⁴")
    }
    fun toCNote(numPart: Int = 1): String {
        val note = stepToSolfa.getOrElse(step ?: "") { "" }
        val height = octaveToSolfa.getOrElse((octave ?: 0) + numPart - 1) { "" }
        return note + height
    }
    fun toKey(key: String, numPart: Int): String {
        if (unpitched != null) return "z"
        return Transpose.transpose(toCNote(numPart), "C", key, alter ?: 0)
    }
}
