import SwiftUI

struct FilesPropertyView: View {

    let title: LocalizedStringKey

    let footer: LocalizedStringKey

    @Binding
    var selectedFiles: [FileResource]

    let allowedType: FileTypeCategory?

    init(
        title: LocalizedStringKey,
        footer: LocalizedStringKey,
        selectedFiles: Binding<[FileResource]>,
        allowedType: FileTypeCategory? = nil
    ) {
        self.title = title
        self.footer = footer
        self._selectedFiles = selectedFiles
        self.allowedType = allowedType
    }

    private var selectedText: String {
        guard !selectedFiles.isEmpty else {
            return "No file selected"
        }
        guard selectedFiles.count == 1 else {
            return "\(selectedFiles.count) files selected"
        }
        return selectedFiles[0].identifier
    }

    @State
    private var showFileSelectionSheet = false

    var body: some View {
        GenericPropertyView(title: title, footer: footer) {
            HStack {
                Text(selectedText)
                Spacer()
                Button("Select") {
                    showFileSelectionSheet = true
                }
            }
        }
        .sheet(isPresented: $showFileSelectionSheet) {
            MultiFileSelectionView(selectedFiles: $selectedFiles, allowedType: allowedType)
        }
    }
}
