import SwiftUI
import HealthKit

struct WorkoutTypeSelection: View {

    @Binding
    var selected: HKWorkoutActivityType?

    @Binding
    var available: [(type: HKWorkoutActivityType, count: Int)]

    var body: some View {
        ScrollView(.horizontal) {
            HStack {
                Text("All")
                    .padding(.horizontal)
                    .padding(.vertical, 8)
                    .background(selected == nil ? Color.accentColor : Color.gray.opacity(0.7))
                    .clipShape(RoundedRectangle(cornerRadius: 50))
                    .foregroundStyle(selected == nil ? Color.black : Color.white)
                    .onTapGesture {
                        selected = nil
                    }
                    //.padding(.leading)
                ForEach(available, id: \.type) { element in
                    Text("\(element.type)")
                        .padding(.horizontal)
                        .padding(.vertical, 8)
                        .background(element.type == selected ? Color.accentColor : Color.gray.opacity(0.7))
                        .clipShape(RoundedRectangle(cornerRadius: 50))
                        .foregroundStyle(element.type == selected ? Color.black : Color.white)
                        .onTapGesture {
                            selected = element.type
                        }
                }
            }
            .font(.headline)
        }
        .scrollIndicators(.hidden)
    }
}

#Preview {
    WorkoutTypeSelection(
        selected: .constant(.running),
        available: .constant([
            (type: .running, count: 13),
            (type: .soccer, count: 10),
            (type: .cycling, count: 7),
        ]))
    .preferredColorScheme(.dark)
}
