import SwiftUI
import SFSafeSymbols

private struct PageSelectionView: View {

    @ObservedObject
    var tag: Tag

    @ObservedObject
    var page: Page

    @Environment(\.language)
    private var language

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

struct PageTagAssignmentView: 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.pages) { page in
                    PageSelectionView(tag: tag, page: page)
                }
            }.frame(minHeight: 400)
            Button("Done") {
                dismiss()
            }.padding(.bottom)
        }
    }
}

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