//
//  GenericDestinationPickerView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 31/03/2024.
//

import SwiftUI

struct GenericDestinationPickerView<T: Identifiable & Selectable & Equatable >: View {
    @EnvironmentObject var dataStore: DataStore
    @Binding var selectedDestination: T?
    let destinations: [T]
    let nilDestinationIsValid: Bool

    var body: some View {
        ScrollViewReader { proxy in
            ScrollView(.horizontal) {
                HStack {
                    if nilDestinationIsValid {
                        Button {
                            selectedDestination = nil
                        } label: {
                            Image(systemName: "wrench.and.screwdriver")
                                .foregroundColor(selectedDestination == nil ? .white : .black)
                        }
                        .padding()
                        .background {
                            Circle()
                                .fill(selectedDestination == nil ? .master : .beige)
                        }
                        .buttonStyle(.plain)
                        .id("settings")
                    }
                    
                    ForEach(destinations) { destination in
                        Button {
                            selectedDestination = destination
                        } label: {
                            Text(destination.selectionLabel())
                                .foregroundStyle(selectedDestination?.id == destination.id ? .white : .black)
                        }
                        .padding()
                        .background {
                            Capsule()
                                .fill(selectedDestination?.id == destination.id ? .master : .beige)
                        }
                        .id(destination.id)
                        .buttonStyle(.plain)
                        .overlay(alignment: .bottomTrailing) {
                            if destination.displayImageIfValueZero() {
                                let count : Int? = destination.badgeValue()
                                if let count, count == 0, let badge = destination.badgeImage() {
                                    Image(systemName: badge.systemName())
                                        .foregroundColor(badge.color())
                                        .imageScale(.medium)
                                        .background (
                                            Color(.systemBackground)
                                                .clipShape(.circle)
                                        )
                                        .offset(x: 3, y: 3)
                                } else if let count, count > 0 {
                                    Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill")
                                        .foregroundColor(destination.badgeValueColor() ?? .red)
                                        .imageScale(.medium)
                                        .background (
                                            Color(.systemBackground)
                                                .clipShape(.circle)
                                        )
                                        .offset(x: 3, y: 3)
                                }
                            } else {
                                if let badge = destination.badgeImage() {
                                    Image(systemName: badge.systemName())
                                        .foregroundColor(badge.color())
                                        .imageScale(.medium)
                                        .background (
                                            Color(.systemBackground)
                                                .clipShape(.circle)
                                        )
                                        .offset(x: 3, y: 3)
                                } else if let count = destination.badgeValue(), count > 0 {
                                    Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill")
                                        .foregroundColor(destination.badgeValueColor() ?? .red)
                                        .imageScale(.medium)
                                        .background (
                                            Color(.systemBackground)
                                                .clipShape(.circle)
                                        )
                                        .offset(x: 3, y: 3)
                                }
                            }
                        }
                    }
                }
                .fixedSize()
                .padding(8)
            }
            .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
            .background(Material.ultraThinMaterial)
            .overlay {
                VStack(spacing: 0) {
                    Spacer()
                    Divider()
                }
            }
            .onAppear {
                if let selectedDestination {
                    proxy.scrollTo(selectedDestination.id)
                } else {
                    proxy.scrollTo("settings")
                }
            }
        }
    }
}
