import SwiftUI
import SFSafeSymbols

struct TextWithPopup: View {

    let symbol: SFSymbol

    let title: LocalizedStringKey

    let text: LocalizedStringKey

    let items: [String]

    @State
    private var showSheet = false

    var body: some View {
        HStack {
            Image(systemSymbol: symbol)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 16, height: 16)
            Text(text)
        }
        .contentShape(Rectangle())
        .onTapGesture {
            if items.count > 0 {
                showSheet.toggle()
            }
        }
        .sheet(isPresented: $showSheet) {
            ListPopup(title: title, items: items)
                .onTapGesture {
                    showSheet.toggle()
                }
        }
    }
}

struct PopupWithList<ListContent>: View where ListContent: View {

    let symbol: SFSymbol

    let text: LocalizedStringKey

    let canShowPopup: Bool

    let content: ListContent

    init(symbol: SFSymbol, text: LocalizedStringKey, canShowPopup: Bool, @ViewBuilder content: () -> ListContent) {
        self.symbol = symbol
        self.text = text
        self.canShowPopup = canShowPopup
        self.content = content()
    }

    @State
    private var showSheet = false

    var body: some View {
        HStack {
            Image(systemSymbol: symbol)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 16, height: 16)
            Text(text)
        }
        .contentShape(Rectangle())
        .onTapGesture {
            if canShowPopup {
                showSheet.toggle()
            }
        }
        .sheet(isPresented: $showSheet) {
            Popup(content: content)
        }
    }
}

private struct Popup<Content>: View where Content: View {

    @Environment(\.dismiss)
    var dismiss

    let content: Content

    var body: some View {
        VStack {
            content
            Button("Dismiss") { dismiss() }
        }.padding()
    }
}
