package com.pixelized.headache.ui.page.event.edit

import android.icu.util.Calendar
import com.pixelized.headache.repository.event.Event
import com.pixelized.headache.ui.theme.color.HeadacheColorPalette
import com.pixelized.headache.utils.extention.event
import java.util.Date
import javax.inject.Inject

class EventEditFactory @Inject constructor() {

    fun convertToUio(
        event: Event?,
    ): EventEditBottomSheetUio {
        return when (event) {
            null -> EventEditBottomSheetUio(
                id = null,
                date = Date(),
                pills = listOf(
                    EventPillEditUio(
                        id = Event.Pill.Id.IBUPROFENE_400.value,
                        color = HeadacheColorPalette.Pill.Ibuprofene400,
                        label = "Paracétamol 1000",
                        amount = 0,
                    ),
                    EventPillEditUio(
                        id = Event.Pill.Id.PARACETAMOL_1000.value,
                        color = HeadacheColorPalette.Pill.Paracetamol1000,
                        label = "Ibuprofène 400",
                        amount = 0,
                    ),
                    EventPillEditUio(
                        id = Event.Pill.Id.SPIFEN_400.value,
                        color = HeadacheColorPalette.Pill.Spifen400,
                        label = "Spifen 400",
                        amount = 0,
                    ),
                    EventPillEditUio(
                        id = Event.Pill.Id.ELETRIPTAN_40.value,
                        color = HeadacheColorPalette.Pill.Eletriptan40,
                        label = "Élétriptan 40",
                        amount = 0,
                    ),
                ),
            )

            else -> {
                val date = Calendar.getInstance().apply { this.event = event.date }.time

                val ibuprofeneAmount = event.pills.sumOf {
                    if (it.id == Event.Pill.Id.IBUPROFENE_400) it.amount else 0
                }
                val paracetamolAmount = event.pills.sumOf {
                    if (it.id == Event.Pill.Id.PARACETAMOL_1000) it.amount else 0
                }
                val spifenAmount = event.pills.sumOf {
                    if (it.id == Event.Pill.Id.SPIFEN_400) it.amount else 0
                }
                val eletriptanAmount = event.pills.sumOf {
                    if (it.id == Event.Pill.Id.ELETRIPTAN_40) it.amount else 0
                }

                EventEditBottomSheetUio(
                    id = event.id,
                    date = date,
                    pills = listOf(
                        EventPillEditUio(
                            id = Event.Pill.Id.IBUPROFENE_400.value,
                            color = HeadacheColorPalette.Pill.Ibuprofene400,
                            label = "Ibuprofène 400",
                            amount = ibuprofeneAmount,
                        ),
                        EventPillEditUio(
                            id = Event.Pill.Id.PARACETAMOL_1000.value,
                            color = HeadacheColorPalette.Pill.Paracetamol1000,
                            label = "Paracétamol 1000",
                            amount = paracetamolAmount,
                        ),
                        EventPillEditUio(
                            id = Event.Pill.Id.SPIFEN_400.value,
                            color = HeadacheColorPalette.Pill.Spifen400,
                            label = "Spifen 400",
                            amount = spifenAmount,
                        ),
                        EventPillEditUio(
                            id = Event.Pill.Id.ELETRIPTAN_40.value,
                            color = HeadacheColorPalette.Pill.Eletriptan40,
                            label = "Élétriptan 40",
                            amount = eletriptanAmount,
                        ),
                    ),
                )
            }
        }
    }

    fun convertToDescription(
        pills: List<EventPillEditUio>,
    ): String {
        return pills
            .mapNotNull { it.takeIf { it.amount > 0 } }
            .joinToString(separator = "\n") {
                when (it.amount) {
                    1 -> it.label
                    else -> "${it.label} x${it.amount}"
                }
            }
    }
}