import SwiftUI

private let df: DateFormatter = {
    let df = DateFormatter()
    df.dateStyle = .short
    df.timeStyle = .medium
    return df
}()

struct LogView: View {

    @EnvironmentObject
    var log: Log

    @EnvironmentObject
    var storage: PersistentStorage

    var body: some View {
        NavigationView {
            List {
                Text("\(storage.numberOfStoredDeviceInfos) device infos")
                    .font(.body)
                    .foregroundColor(.secondary)
                Text("\(storage.numberOfStoredTransfers) transfers")
                    .font(.body)
                    .foregroundColor(.secondary)
                ForEach(log.logEntries) { entry in
                    VStack(alignment: .leading) {
                        HStack {
                            Text(entry.level.description)
                            Spacer()
                            Text(df.string(from: entry.date))
                        }.font(.footnote)
                        Text(entry.message)
                    }
                }
            }
            .navigationTitle("Log")
            .navigationBarTitleDisplayMode(.large)
        }
    }
}

struct LogView_Previews: PreviewProvider {
    static var previews: some View {
        LogView()
            .environmentObject(Log())
    }
}
