//
//  ImageSelectionView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 27/01/2023.
//

import SwiftUI

struct ImageSelectionView: View {
    
    var showBinding: Binding<Bool>
    
    var imageBinding: Binding<CoolPic>

    private let columns: [GridItem] = [
        GridItem(spacing: 10.0),
        GridItem(spacing: 10.0),
    ]

    var body: some View {
        
        NavigationStack {
            
            GeometryReader { reader in
                let width: CGFloat = reader.size.width / 2.0 - 10.0

                ScrollView {
                    LazyVGrid(
                        columns: columns,
                        spacing: 10.0
                    ) {
                        
                        ForEach(CoolPic.allCases) { coolPic in
                            ZStack {
                                Image(coolPic.rawValue)
                                    .resizable()
                                    .frame(width: width, height: width)
                                    .cornerRadius(40.0)
                            }
                            .onTapGesture {
                                self.imageBinding.wrappedValue = coolPic
                                self.showBinding.wrappedValue = false
                                print("coolPic = \(coolPic), image = \(String(describing: self.imageBinding.wrappedValue))")
                            }
                            
                        }
                    }.padding(10.0)
                }
            }
            .navigationTitle("Background")
            
        }
    }
}

struct ImageSelectionView_Previews: PreviewProvider {
    static var previews: some View {
        ImageSelectionView(showBinding: .constant(true),
                           imageBinding: .constant(.pic3))
    }
}
