import SwiftUI

struct FileSelectionView: View {

    @Environment(\.dismiss)
    private var dismiss

    @Binding
    private var selectedFile: FileResource?

    let allowedType: FileTypeCategory?

    init(selectedFile: Binding<FileResource?>, allowedType: FileTypeCategory? = nil) {
        self._selectedFile = selectedFile
        self.newSelection = selectedFile.wrappedValue
        self.allowedType = allowedType
    }

    @State
    private var newSelection: FileResource?

    var body: some View {
        VStack {
            FileListView(selectedFile: $newSelection, allowedType: allowedType)
                .frame(minHeight: 500, idealHeight: 600)
            HStack {
                Button("Cancel") {
                    DispatchQueue.main.async {
                        dismiss()
                    }
                }
                Button("Remove") {
                    DispatchQueue.main.async {
                        selectedFile = nil
                        dismiss()
                    }
                }
                Button("Select") {
                    selectedFile = newSelection
                    dismiss()
                }
            }
        }
        .padding()
    }
}
