package com.pixelized.headache.repository.event.factory

import com.pixelized.headache.repository.event.Event
import com.pixelized.headache.ui.theme.color.HeadacheColorPalette
import java.util.regex.Pattern
import javax.inject.Inject

class PillFactory @Inject constructor() {
    private val amountSplitRegex = Pattern.compile("""\s[xX]\s*""")

    @Throws(Exception::class)
    fun parsePill(pill: String): Event.Pill {
        val split = pill.split(regex = amountSplitRegex)
        val label = split.getOrNull(0) ?: error("missing label")
        val amount = split.getOrNull(1)?.trim()?.toIntOrNull() ?: 1

        return when {
            label == SPIFEN_400_LABEL -> {
                Event.Pill(
                    id = Event.Pill.Id.SPIFEN_400,
                    label = SPIFEN_400_LABEL,
                    color = HeadacheColorPalette.Pill.Spifen400,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = false,
                )
            }

            label.contains(ELETRIPTAN_40_LABEL, ignoreCase = true) -> {
                Event.Pill(
                    id = Event.Pill.Id.ELETRIPTAN_40,
                    label = ELETRIPTAN_40_LABEL,
                    color = HeadacheColorPalette.Pill.Eletriptan40,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = false,
                )
            }

            label.contains(IBUPROFENE_400_LABEL, ignoreCase = true) -> {
                Event.Pill(
                    id = Event.Pill.Id.IBUPROFENE_400,
                    label = IBUPROFENE_400_LABEL,
                    color = HeadacheColorPalette.Pill.Ibuprofene400,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = false,
                )
            }

            label.contains(PARACETAMOL_1000_LABEL, ignoreCase = true) -> {
                Event.Pill(
                    id = Event.Pill.Id.PARACETAMOL_1000,
                    label = PARACETAMOL_1000_LABEL,
                    color = HeadacheColorPalette.Pill.Paracetamol1000,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = false,
                )
            }

            SPIFEN_400_LIST.any { label.contains(it, ignoreCase = true) } -> {
                Event.Pill(
                    id = Event.Pill.Id.SPIFEN_400,
                    label = SPIFEN_400_LABEL,
                    color = HeadacheColorPalette.Pill.Spifen400,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = true,
                )
            }

            ELETRIPTAN_40_LIST.any { label.contains(it, ignoreCase = true) } -> {
                Event.Pill(
                    id = Event.Pill.Id.ELETRIPTAN_40,
                    label = ELETRIPTAN_40_LABEL,
                    color = HeadacheColorPalette.Pill.Eletriptan40,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = true,
                )
            }

            IBUPROFENE_400_LIST.any { label.contains(it, ignoreCase = true) } -> {
                Event.Pill(
                    id = Event.Pill.Id.IBUPROFENE_400,
                    label = IBUPROFENE_400_LABEL,
                    color = HeadacheColorPalette.Pill.Ibuprofene400,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = true,
                )
            }

            PARACETAMOL_1000_LIST.any { label.contains(it, ignoreCase = true) } -> {
                Event.Pill(
                    id = Event.Pill.Id.PARACETAMOL_1000,
                    label = PARACETAMOL_1000_LABEL,
                    color = HeadacheColorPalette.Pill.Paracetamol1000,
                    amount = amount,
                    description = pill,
                    isValid = true,
                    isMisspelled = true,
                )
            }

            else -> {
                Event.Pill(
                    id = Event.Pill.Id.UNKNOWN,
                    label = "?",
                    color = HeadacheColorPalette.Pill.Unknown,
                    amount = amount,
                    description = pill,
                    isValid = false,
                    isMisspelled = false,
                )
            }
        }
    }

    companion object {
        private const val PARACETAMOL_1000_LABEL = "Paracétamol 1000"
        private val PARACETAMOL_1000_LIST = listOf<String>(
            "Doliprane 1000",
        )

        private const val IBUPROFENE_400_LABEL = "Ibuprofène 400"
        private val IBUPROFENE_400_LIST = listOf(
            "Ibuprofene 400",
            "Ibuprofen 400",
            "Ibuprofen",
        )

        private const val SPIFEN_400_LABEL = "Spifen 400"
        private val SPIFEN_400_LIST = listOf<String>(
            "Spifen",
        )

        private const val ELETRIPTAN_40_LABEL = "Élétriptan 40"
        private val ELETRIPTAN_40_LIST = listOf(
            "Eletriptan 40",
            "Életriptan 40",
            "Élitriptan 40",
            "Elitriptan 40",
            "Eletripan 40",
            "Elétripan 40",
            "Elitripan 40",
            "Elitripan",
        )
    }
}