package com.pixelized.headache.ui.page.summary.report

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 javax.inject.Inject

class ReportFactory @Inject constructor() {

    fun convertToUio(
        maxPillAmountPerMonth: Int,
        events: Map<Int, Map<Int, Map<Int, List<Event>>>>,
    ): List<ReportBoxUio> {
        return events
            .map { yearEntry ->
                ReportBoxUio(
                    year = yearEntry.key,
                    months = yearEntry.value.map { monthEntry ->
                        var headache = 0
                        var pills = 0
                        monthEntry.value.values.forEach { events ->
                            headache += events.size
                            pills += events.sumOf { it.pills.size }
                        }
                        val dayInMonth = Calendar.getInstance().let {
                            it.set(Calendar.YEAR, yearEntry.key)
                            it.set(Calendar.MONTH, monthEntry.key + 1)
                            it.set(Calendar.DAY_OF_MONTH, 1)
                            it.add(Calendar.DAY_OF_YEAR, -1)
                            it.get(Calendar.DAY_OF_MONTH)
                        }
                        ReportBoxUio.Month(
                            date = Calendar.getInstance().apply {
                                event = Event.Date(
                                    day = 1,
                                    month = monthEntry.key,
                                    year = yearEntry.key
                                )
                            }.time,
                            stats = listOfNotNull(
                                if (headache > 0) {
                                    ReportBoxUio.Bar(
                                        color = HeadacheColorPalette.Calendar.Headache,
                                        label = "$headache",
                                        ratio = headache.toFloat() / dayInMonth.toFloat(),
                                    )
                                } else {
                                    null
                                },
                                if (pills > 0) {
                                    ReportBoxUio.Bar(
                                        color = HeadacheColorPalette.Calendar.Pill,
                                        label = "$pills",
                                        ratio = pills.toFloat() / maxPillAmountPerMonth.toFloat(),
                                    )
                                } else {
                                    null
                                },
                            ).sortedByDescending { it.ratio },
                        )
                    }
                )
            }
            .sortedByDescending { it.year }
    }
}