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

import SwiftUI

enum MessageCenterFilter: CaseIterable, Equatable {
    case all
    case unread
    case archived

    var chipOption: SQChipOption {
        switch self {
        case .all:
            return SQChipOption(id: 0, label: "Toutes", bgColor: Color.sqNeutral(100))
        case .unread:
            return SQChipOption(id: 1, label: "Non lues", bgColor: Color.sqSemanticBlue)
        case .archived:
            return SQChipOption(id: 2, label: "Archivées", bgColor: Color.sqSemanticCritical)
        }
    }
}

struct MessageCenterChipFiltersCellView: View {
    @State var selectedOption: SQChipOption
    let options: [SQChipOption]
    let onFilterSelected: (MessageCenterFilter) -> Void

    init(onFilterSelected: @escaping (MessageCenterFilter) -> Void) {
        self.options = [
            MessageCenterFilter.all.chipOption,
            MessageCenterFilter.unread.chipOption,
            MessageCenterFilter.archived.chipOption
        ]
        self._selectedOption = State(initialValue: MessageCenterFilter.all.chipOption)
        self.onFilterSelected = onFilterSelected
    }

    var body: some View {
        VStack {
            SQChips(options: options, selectedOption: $selectedOption)
                .onChange(of: selectedOption) { newValue in
                    if let filter = MessageCenterFilter.allCases.first(where: { $0.chipOption.id == newValue.id }) {
                        onFilterSelected(filter)
                    }
                }
                .padding()
        }
    }
}

#Preview {
    MessageCenterChipFiltersCellView { _ in
    }
}
