package com.somabits.spanish.regularity

import com.somabits.spanish.comparison.ConjugationComparison
import com.somabits.spanish.comparison.SubstringComparisonResult
import com.somabits.spanish.comparison.compareSubstrings
import com.somabits.spanish.verb.Conjugation
import com.somabits.spanish.verb.VerbForm

class ConjugationRegularityChecker(
    override val conjugation: Conjugation,
    val regularConjugation: Conjugation? = conjugation.findCorrespondingRegularConjugation(),
) : ConjugationComparison(
    conjugation = conjugation,
    targetConjugation = regularConjugation,
) {
    val isRegular = super.isMatching
}

class VerbFormRegularityChecker(
    val verbForm: VerbForm,
    private val regularVerbForm: VerbForm = verbForm.getCorrespondingRegularVerbForm(),
) {
    val substringRegularities by lazy {
        verbForm.conjugatedForm.compareSubstrings(regularVerbForm.conjugatedForm)
    }

    val isRegular: Boolean = verbForm.conjugatedForm == regularVerbForm.conjugatedForm
}

fun Conjugation.findCorrespondingRegularConjugation(): Conjugation? {
    return VerbConjugation(
        infinitive,
    ).getTenseConjugation(tense)
}

fun VerbForm.getCorrespondingRegularVerbForm(): VerbForm {
    return when (this) {
        is VerbForm.Participio -> VerbConjugation(
            infinitive,
        ).participio

        is VerbForm.Gerundio -> VerbConjugation(
            infinitive,
        ).gerundio
    }
}

val List<SubstringComparisonResult>.isMatching: Boolean
    get() = all { it.isMatching }
