package com.drivervault.phoneapp.car

import android.content.Intent
import android.content.pm.ApplicationInfo
import androidx.car.app.CarAppService
import androidx.car.app.Screen
import androidx.car.app.Session
import androidx.car.app.validation.HostValidator

/**
 * DriverVault on the car's own screen, over Android Auto.
 *
 * The head unit runs no Flutter engine: these screens are the Car App Library's
 * templates, drawn by the host, reading the session the phone app has already
 * signed in with (see [VaultStore]) and the translations it already ships (see
 * [CarStrings]). Nothing is signed into, entered or edited here — the car shows
 * what is due, and the phone stays where the garage is kept.
 */
class DriverVaultCarAppService : CarAppService() {

    /**
     * Which hosts may drive this app. The signed allowlist the library ships
     * covers Android Auto and Automotive; a debug build takes any host, which is
     * what lets the Desktop Head Unit connect while developing.
     */
    override fun createHostValidator(): HostValidator =
        if (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
            HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
        } else {
            HostValidator.Builder(applicationContext)
                .addAllowedHosts(androidx.car.app.R.array.hosts_allowlist_sample)
                .build()
        }

    override fun onCreateSession(): Session = object : Session() {
        override fun onCreateScreen(intent: Intent): Screen = GarageScreen(carContext)
    }
}
