//
//  ExpensesTabView.swift
//  ExpenseTracker

import SwiftUI
import CoreData

struct ExpensesTabView: View {
    
    @Environment(\.managedObjectContext)
        var context: NSManagedObjectContext
    
    @State private var searchText : String = ""
    @State private var searchBarHeight: CGFloat = 0
    @State private var sortType = SortType.date
    @State private var sortOrder = SortOrder.descending
    
    @State var selectedCategories: Set<Category> = Set()
    @State var isAddFormPresented: Bool = false
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                SearchBar(text: $searchText, keyboardHeight: $searchBarHeight, placeholder: "Search expenses")
                SelectSortOrderView(sortType: $sortType, sortOrder: $sortOrder)
                Divider()
                FilterCategoriesView(selectedCategories: $selectedCategories)
                Divider()
                LogListView(predicate: ExpenseLog.predicate(with: Array(selectedCategories), searchText: searchText), sortDescriptor: ExpenseLogSort(sortType: sortType, sortOrder: sortOrder).sortDescriptor)
            }
            .padding(.bottom, searchBarHeight)
            .sheet(isPresented: $isAddFormPresented) {
                LogFormView(context: self.context)
            }
            .navigationBarItems(trailing: Button(action: addTapped) { Image(systemName: "plus.circle.fill") })
            .navigationBarTitle("Expense Logs", displayMode: .inline)
        }
    }
    
    func addTapped() {
        isAddFormPresented = true
    }
    
    
    
}
//
//struct ExpensesTabView_Previews: PreviewProvider {
//    static var previews: some View {
//        ExpensesTabView()
//    }
//}
