import SwiftUI
import SFSafeSymbols

private struct PostSelectionView: View {

    @ObservedObject
    var tag: Tag

    @ObservedObject
    var post: Post

    @Environment(\.language)
    private var language

    var body: some View {
        HStack {
            let isSelected = post.contains(tag)
            Image(systemSymbol: isSelected ? .checkmarkCircleFill : .circle)
                .foregroundStyle(Color.blue)
            Text(post.title(in: language))
        }
        .contentShape(Rectangle())
        .onTapGesture {
            post.toggle(tag)
        }
    }
}

struct PostTagAssignmentView: View {

    @ObservedObject
    var tag: Tag

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    @Environment(\.dismiss)
    private var dismiss: DismissAction

    var body: some View {
        VStack {
            List {
                ForEach(content.posts) { post in
                    PostSelectionView(tag: tag, post: post)
                }
            }.frame(minHeight: 400)
            Button("Done") {
                dismiss()
            }.padding(.bottom)
        }
    }
}

#Preview {
    PostTagAssignmentView(tag: .Mock.hiking)
        .environmentObject(Content.mock)
}
