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

import android.icu.text.Collator
import android.icu.util.Calendar
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.intl.Locale
import com.pixelized.headache.repository.event.Event
import com.pixelized.headache.ui.page.summary.monthly.item.MonthSummaryItemUio
import com.pixelized.headache.ui.page.summary.monthly.item.MonthSummaryPillItemUio
import com.pixelized.headache.ui.page.summary.monthly.item.MonthSummaryTitleUio
import javax.inject.Inject

class MonthSummaryFactory @Inject constructor() {
    private val calendar = Calendar.getInstance()
    private val locale = Locale.current.let {
        java.util.Locale.forLanguageTag(it.toLanguageTag())
    }

    fun convertToItemUio(
        events: Map<Int, Map<Int, Map<Int, List<Event>>>>,
    ): Map<MonthSummaryTitleUio, List<MonthSummaryItemUio>> {
        return events
            .flatMap { yearEntry ->
                yearEntry.value.map { monthEntry ->
                    val pills = monthEntry.value.values
                        .fold(
                            initial = hashMapOf<Event.Pill.Id, PillFolder>(),
                        ) { acc, events ->
                            events.map { event ->
                                event.pills.forEach { pill ->
                                    val value = acc.getOrElse(
                                        key = pill.id,
                                        defaultValue = {
                                            PillFolder(label = pill.label, color = pill.color)
                                        },
                                    )
                                    value.amount += pill.amount
                                    acc[pill.id] = value
                                }
                            }
                            acc
                        }
                        .map { entry ->
                            MonthSummaryPillItemUio(
                                label = entry.value.label,
                                color = entry.value.color,
                                amount = entry.value.amount,
                            )
                        }
                        .toList()
                        .sortedWith(Comparator { s1, s2 ->
                            Collator.getInstance(locale).compare(s1.label, s2.label)
                        })

                    MonthSummaryItemUio(
                        date = calendar.apply {
                            set(Calendar.YEAR, yearEntry.key)
                            set(Calendar.MONTH, monthEntry.key)
                        }.time,
                        days = monthEntry.value.values.sumOf { it.size },
                        pills = pills,
                    )
                }
            }
            .sortedByDescending { it.date }
            .groupByMonth()
            .toSortedMap{s1, s2 ->
                when{
                    s1.date < s2.date -> 1
                    s1.date > s2.date -> -1
                    else -> 0
                }
            }
    }

    fun List<MonthSummaryItemUio>.groupByMonth(): Map<MonthSummaryTitleUio, List<MonthSummaryItemUio>> {
        return this.groupBy {
            MonthSummaryTitleUio(
                date = calendar.apply {
                    time = it.date
                    set(Calendar.DAY_OF_YEAR, 1)
                    set(Calendar.MILLISECONDS_IN_DAY, 0)
                }.time,
            )
        }
    }

    private class PillFolder(
        val label: String,
        val color: Color,
        var amount: Int = 0,
    )
}