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

import android.icu.util.Calendar
import com.pixelized.headache.repository.event.Event
import com.pixelized.headache.utils.extention.event
import javax.inject.Inject


class EventItemFactory @Inject constructor() {
    private val calendar = Calendar.getInstance()

    fun convertToUio(
        events: Collection<Event>,
    ): List<EventItemUio> {
        return events
            .sortedByDescending { it.date }
            .map { convertToUio(it) }
    }

    fun convertToUio(
        event: Event,
    ): EventItemUio {
        val date = calendar.apply { this.event = event.date }.time

        return EventItemUio(
            id = event.id,
            date = date,
            pills = event.pills.map { convertToUio(it) },
            isValid = event.isValid,
            isMisspelled = event.isMisspelled,
        )
    }

    fun convertToUio(
        pill: Event.Pill,
    ): PillUio {
        return PillUio(
            label = pill.label,
            amount = pill.amount,
            description = pill.description,
            color = pill.color,
            isValid = pill.isValid,
            isMisspelled = pill.isMisspelled,
        )
    }
}