package com.drivervault.phoneapp.car

import androidx.car.app.CarContext
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.SectionedItemList
import androidx.car.app.model.Template

/** One car's next service and everything else it has coming. */
data class CarDue(
    val service: VaultService?,
    val status: CarStatus,
    val reminders: List<VaultReminder>,
)

/**
 * What one car wants doing: its odometer and next service up top, then the
 * reminders the server holds for it — the ones typed in and the ones it derives
 * from documents and the service schedule alike, in the order it sorted them,
 * outstanding first.
 *
 * It is the Reminders tab and the service badge of the phone's car page, and
 * deliberately nothing else: a head unit has no room for a service history, and
 * the driver has no attention for one.
 */
class CarDueScreen(carContext: CarContext, private val car: VaultCar) :
    VaultScreen<CarDue>(carContext) {

    override val title: String get() = car.name

    override fun read(api: VaultApi): CarDue {
        val latest = api.latestService(car.id)
        return CarDue(
            service = latest,
            status = format.serviceStatus(latest, car.currentKm),
            reminders = api.reminders(car.id),
        )
    }

    override fun template(data: CarDue): Template {
        val service = ItemList.Builder()
            .addItem(
                Row.Builder()
                    .setTitle(strings.t("dashboard.currentOdometer"))
                    .addText(format.km(car.currentKm))
                    .build(),
            )
            .addItem(
                Row.Builder()
                    .setTitle(strings.t("dashboard.nextDue"))
                    .addText(nextDue(data.service))
                    .addText(tinted(data.status))
                    .build(),
            )
            .build()

        // Two rows are spent on the service section, so the reminders get what
        // the host has left.
        val room = (listLimit() - 2).coerceAtLeast(1)
        val reminders = ItemList.Builder()
        if (data.reminders.isEmpty()) {
            reminders.addItem(Row.Builder().setTitle(strings.t("car.reminders.empty")).build())
        } else {
            for (reminder in data.reminders.take(room)) {
                reminders.addItem(
                    Row.Builder()
                        .setTitle(reminder.title)
                        .addText(tinted(format.reminderStatus(reminder)))
                        .build(),
                )
            }
        }

        return ListTemplate.Builder()
            .setTitle(title)
            .setHeaderAction(headerAction)
            .setActionStrip(refreshStrip)
            .addSectionedList(
                SectionedItemList.create(service, strings.t("dashboard.serviceLife")),
            )
            .addSectionedList(
                SectionedItemList.create(reminders.build(), strings.t("car.reminders.title")),
            )
            .build()
    }

    /** The date and the odometer reading a service falls due on, whichever arrives first. */
    private fun nextDue(service: VaultService?): String = listOf(
        format.date(service?.nextServiceDate),
        format.km(service?.nextServiceKm),
    ).joinToString(" · ")
}
