import SwiftUI

struct PostImagesView: View {

    @Environment(\.language)
    private var language

    @ObservedObject
    var post: LocalizedPost

    @ObservedObject
    var other: LocalizedPost

    @State
    private var fileTypeToSelect: FileTypeCategory = .image

    @State
    private var showImagePicker = false

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Text("Images/Video")
                    .font(.headline)
                Button("Images") {
                    fileTypeToSelect = .image
                    showImagePicker = true
                }
                .disabled(post.hasVideos)
                Button("Videos") {
                    fileTypeToSelect = .video
                    showImagePicker = true
                }
                .disabled(post.hasImages)
                Button("Transfer from \(language.next.text)") {
                    post.images = other.images
                }
                .disabled(other.images.isEmpty)
            }
            ScrollView(.horizontal) {
                HStack(alignment: .center, spacing: 8) {
                    ForEach(post.images) { image in
                        PostImageView(image: image)
                    }
                }
            }
        }
        .sheet(isPresented: $showImagePicker) {
            MultiFileSelectionView(
                selectedFiles: $post.images,
                allowedType: fileTypeToSelect)
        }
    }
}
