import SwiftUI

struct DetailRow: View {
    
    let title: String
    
    let value: String
    
    init(_ title: String, value: String) {
        self.title = title
        self.value = value
    }
    
    init(_ title: String, value: CustomStringConvertible?) {
        self.title = title
        self.value = value?.description ?? "-"
    }

    init(_ title: String, time value: Date?) {
        self.title = title
        self.value = value?.timeText ?? "-"
    }

    init(_ title: String, date value: Date?) {
        self.title = title
        self.value = value?.timeAndDateText ?? "-"
    }
    
    init(_ title: String, kilometer: Double?) {
        self.title = title
        self.value = kilometer?.distanceAsKilometer ?? "-"
    }
    
    init(_ title: String, duration: TimeInterval?) {
        self.title = title
        self.value = duration?.durationString ?? "-"
    }
    
    var body: some View {
        HStack {
            Text(title)
                .foregroundStyle(.secondary)
            Spacer()
            Text(value)
                .foregroundStyle(.primary)
        }
    }
}

#Preview {
    List {
        DetailRow("Title", value: "Some")
        DetailRow("Convertible", value: 123)
        DetailRow("Optional", value: nil)
        DetailRow("Date", date: .now)
        DetailRow("Distance", kilometer: 123.4)
        DetailRow("Duration", duration: 678.9)
    }
}
