package mg.dot.feufaro.solfa

enum class LineType {
    ROUND,
    SQUARE
}
class UnderlineSpec (
    var x: Int,
    val lineType: LineType,
    val isNewParen: Boolean = false
) {
    var y: Int = -1
    var okX = false
}
class AnnotatedTUO (originalString: String, val voiceNumber: Int){
    var finalText: String = ""
    companion object {
        var inRoundParen = mutableListOf<Boolean>()
        var inSquareParen = mutableListOf<Boolean>()
    }
    var underlineSpec = mutableListOf<UnderlineSpec>()
    private fun toggleInRoundParen(voiceNumber: Int, value: Boolean, x: Int) {
        if (value && (
                    (inRoundParen.size > voiceNumber && inRoundParen[voiceNumber])
                            || (inSquareParen.size > voiceNumber && inSquareParen[voiceNumber]) )) {
            return
        }
        if (!value && (inRoundParen.size <= voiceNumber || !inRoundParen[voiceNumber])) {
            return
        }
        while (inRoundParen.size <= voiceNumber) {
            inRoundParen.add(false)
        }
        val newParen = value && !inRoundParen[voiceNumber]
        inRoundParen[voiceNumber] = value
        if (value) {
            underlineSpec.add(UnderlineSpec(x, LineType.ROUND, newParen))
        } else {
            underlineSpec.lastOrNull()?.y = x
        }
    }
    private fun toggleInSquareParen(voiceNumber: Int, value: Boolean, x: Int) {
        if (value && (
                    (inRoundParen.size > voiceNumber && inRoundParen[voiceNumber])
                 || (inSquareParen.size > voiceNumber && inSquareParen[voiceNumber]) )) {
            return
        }
        if (!value && (inSquareParen.size <= voiceNumber || !inSquareParen[voiceNumber])) {
            return
        }
        while (inSquareParen.size <= voiceNumber) {
            inSquareParen.add(false)
        }
        val newParen = value && !inSquareParen[voiceNumber]
        inSquareParen[voiceNumber] = value
        if (value) {
            underlineSpec.add(UnderlineSpec(x, LineType.SQUARE, newParen))
        } else {
            underlineSpec.lastOrNull()?.y = x
        }
    }
    init {
        var x = -1
        if (voiceNumber > 0) {
            if (inRoundParen.size > voiceNumber && inRoundParen[voiceNumber]) {
                underlineSpec.add(UnderlineSpec(-1, LineType.ROUND))
            }
            if (inSquareParen.size > voiceNumber && inSquareParen[voiceNumber]) {
                underlineSpec.add(UnderlineSpec(-1, LineType.SQUARE))
            }
            originalString.toCharArray().forEach {
                when (it) {
                    '(' -> toggleInRoundParen(voiceNumber, true, x)
                    '[' -> toggleInSquareParen(voiceNumber, true, x)
                    ')' -> toggleInRoundParen(voiceNumber, false, x)
                    ']' -> toggleInSquareParen(voiceNumber, false, x)
                    '>' -> {}
                    else -> {
                        x++
                        finalText += it
                        if (it in listOf('d', 'r', 'm', 'f', 's', 'l', 't', '-', '―')) {
                            val lastUnderLineSpec = underlineSpec.lastOrNull()
                            if (lastUnderLineSpec?.okX == false) {
                                lastUnderLineSpec.okX = true
                                lastUnderLineSpec.x = x
                            }
                        }
                    }
                }
            }
        }
    }
}