//
//  PointSelectionView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 27/02/2023.
//

import SwiftUI

struct PointSelectionView: View {
    @Binding var valueSelected: Int?
    var values: [Int]
    var possibleValues: [Int]
    var disableValues: [Int] = []
    var deleteAction: () -> ()
    let columns = Array(repeating: GridItem(.flexible()), count: 3)

    
    init(valueSelected: Binding<Int?>, values: [Int], possibleValues: [Int], disableValues: [Int], deleteAction: @escaping () -> Void) {
        _valueSelected = valueSelected
        self.values = values
        self.possibleValues = Set(values + possibleValues).sorted().reversed()
        self.disableValues = disableValues
        self.deleteAction = deleteAction
    }
    
    var body: some View {
        
        LazyVGrid(columns: columns, alignment: .center, spacing: 8) {
            ForEach(possibleValues, id: \.self) { value in
                Button {
                    valueSelected = value
                } label: {
                    PointView(value: "\(value).circle.fill")
                        .frame(maxWidth: .infinity)
                }
                .buttonStyle(.borderedProminent)
                .controlSize(.large)
                .disabled(disableValues.contains(value) || values.contains(value) == false )
            }
            Button {
                deleteAction()
            } label: {
                PointView(value: "delete.left.fill")
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
        }
        .padding(8)
    }
}
