import SwiftUI

struct HistoryList: View {
    
    @EnvironmentObject
    var storage: PersistentStorage
    
    var body: some View {
        NavigationView {
            List(storage.dailyMeasurementCounts) { day in
                NavigationLink(destination: {
                    DayView(dateIndex: day.dateIndex)
                        .environmentObject(storage)
                }) {
                    HistoryListRow(entry: day)
                        .swipeActions(edge: .trailing, allowsFullSwipe: true) {
                            Button {
                                deleteRow(for: day.dateIndex)
                            } label: {
                                Label("Delete", systemSymbol: .trash)
                            }
                            .tint(.red)
                        }
                }
            }
            .navigationTitle("History")
            .navigationBarTitleDisplayMode(.large)
        }
    }
    
    private func deleteRow(for dateIndex: Int) {
        storage.removeMeasurements(for: dateIndex)
    }
}

struct HistoryList_Previews: PreviewProvider {
    static var previews: some View {
        HistoryList()
            .environmentObject(PersistentStorage(lastMeasurements: TemperatureMeasurement.mockData))
    }
}
