//
//  SQChips.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 24/10/2024.
//

import SwiftUI

struct SQChipOption: Identifiable, Equatable {
    var id: Int
    let label: String
    let bgColor: Color
}

struct SQChips: View {
    var options: [SQChipOption]
    @Binding var selectedOption: SQChipOption

    var body: some View {
        HStack {
            ForEach(options) { option in
                SQChip(option: option, selectedOption: $selectedOption)
                    .onTapGesture {
                        self.selectedOption = option
                    }
            }
        }
    }
}

fileprivate struct SQChip: View {
    var option: SQChipOption
    @Binding var selectedOption: SQChipOption
    private var isSelected: Bool {
        selectedOption.id == option.id
    }

    var body: some View {
        SQText(option.label, size: 14, font: .demiBold, textColor: isSelected ? .white : Color.sqNeutral(100))
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(isSelected ? option.bgColor : Color.clear)
            .cornerRadius(8, corners: .allCorners)
            .overlay(
                RoundedRectangle(cornerRadius: 8)
                    .stroke(
                        isSelected && option.bgColor != .white ? option.bgColor : Color.sqNeutral(100), lineWidth: 1
                    )
            )
    }
}

#Preview {
    SQChips(options: [
        SQChipOption(id: 0, label: "Toutes", bgColor: .sqNeutral(100)),
        SQChipOption(id: 1, label: "Non lues", bgColor: .sqSemanticBlue),
        SQChipOption(id: 2, label: "Archivées", bgColor: .sqSemanticCritical)
    ], selectedOption: .constant(SQChipOption(id: 1, label: "Non lues", bgColor: .sqSemanticBlue)))
}
