import SwiftUI

struct OptionalImagePropertyView: View {

    let title: LocalizedStringKey

    @Binding
    var selectedImage: FileResource?

    let transferImage: (language: ContentLanguage, image: FileResource)?

    let footer: LocalizedStringKey

    @State
    private var showSelectionSheet = false

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Text(title)
                    .font(.headline)
                Spacer()
                if let transferImage {
                    Button("From \(transferImage.language.shortText)") {
                        selectedImage = transferImage.image
                    }
                }
            }

            if let image = selectedImage {
                (image.imageToDisplay ?? Image(systemSymbol: .exclamationmarkTriangle))
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(maxHeight: 300)
                    .cornerRadius(8)
            }

            HStack {
                Text(selectedImage?.identifier ?? "No file selected")
                Spacer()
                Button("Select") {
                    showSelectionSheet = true
                }
            }
            Text(footer)
                .foregroundStyle(.secondary)
                .padding(.bottom)
        }
        .sheet(isPresented: $showSelectionSheet) {
            FileSelectionView(selectedFile: $selectedImage, allowedType: .image)
        }
    }
}
