import SwiftUI

struct CardBackPickerView: View {
    @Binding var selectedBack: CardBackDesign

    var body: some View {
        LazyVGrid(columns: [GridItem(.adaptive(minimum: 60))], spacing: 8) {
            ForEach(CardBackDesign.allCases, id: \.self) { design in
                Button {
                    selectedBack = design
                } label: {
                    cardBackImage(design)
                        .frame(width: 55, height: 77)
                        .clipShape(RoundedRectangle(cornerRadius: 4))
                        .overlay {
                            if design == selectedBack {
                                RoundedRectangle(cornerRadius: 4)
                                    .stroke(Color.accentColor, lineWidth: 3)
                            }
                        }
                }
                .buttonStyle(.plain)
            }
        }
    }

    @ViewBuilder
    private func cardBackImage(_ design: CardBackDesign) -> some View {
        #if os(macOS)
        if let image = NSImage(named: design.imageName) {
            Image(nsImage: image)
                .resizable()
                .aspectRatio(contentMode: .fit)
        } else {
            RoundedRectangle(cornerRadius: 4)
                .fill(.blue)
        }
        #else
        if let image = UIImage(named: design.imageName) {
            Image(uiImage: image)
                .resizable()
                .aspectRatio(contentMode: .fit)
        } else {
            RoundedRectangle(cornerRadius: 4)
                .fill(.blue)
        }
        #endif
    }
}
