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

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

class EventFactory @Inject constructor(
    private val pillFactory: PillFactory,
) {
    private val calendar = Calendar.getInstance()

    fun parseEvent(
        start: Long,
        id: Long,
        title: String,
        description: String,
    ): Event {
        val date = calendar.apply { time = Date(start) }.event
        val pills = description
            .takeIf { it.isNotBlank() }
            ?.split("\n")
            ?.mapNotNull {
                when {
                    it.isBlank() -> null
                    else -> pillFactory.parsePill(pill = it)
                }
            }
            ?: listOf()

        return Event(
            id = id,
            title = title,
            description = description,
            date = date,
            pills = pills,
            isValid = pills.all { it.isValid },
            isMisspelled = pills.any { it.isMisspelled },
        )
    }
}