//
//  CreateWorkoutItemPicker.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/25/23.
//

import SwiftUI
import Combine

struct CreateWorkoutItemPickerModel {
    let id: Int
    let name: String
}

class CreateWorkoutItemPickerViewModel: Identifiable, ObservableObject {
    let allValues: [CreateWorkoutItemPickerModel]
    @Published var selectedIds: [Int]
    
    init(allValues: [CreateWorkoutItemPickerModel], selectedIds: [Int]) {
        self.allValues = allValues
        self.selectedIds = selectedIds
    }
    
    func toggleAll() {
        if selectedIds.isEmpty {
            selectedIds.append(contentsOf: allValues.map({ $0.id }))
        } else {
            selectedIds.removeAll()
        }
    }
}

struct CreateWorkoutItemPickerView: View {
    @ObservedObject var viewModel: CreateWorkoutItemPickerViewModel
    var completed: (([Int]) -> Void)
    @Environment(\.dismiss) var dismiss
    @State var searchString: String = ""

    var body: some View {
        VStack {
            List() {
                ForEach(viewModel.allValues, id:\.self.id) { value in
                    if searchString.isEmpty || value.name.lowercased().contains(searchString.lowercased()) {
                        HStack {
                            Circle()
                                .stroke(.blue, lineWidth: 1)
                                .background(Circle().fill(viewModel.selectedIds.contains(value.id) ? .blue :.clear))
                                .frame(width: 33, height: 33)
                            
                            Text(value.name)
                        }
                        .contentShape(Rectangle())
                        .onTapGesture {
                            if viewModel.selectedIds.contains(value.id) {
                                if let idx = viewModel.selectedIds.firstIndex(of: value.id){
                                    viewModel.selectedIds.remove(at: idx)
                                }
                            } else {
                                viewModel.selectedIds.append(value.id)
                            }
                        }
                    }
                }
            }
            
            TextField("Filter", text: $searchString)
                .padding()
            
            
            HStack {
                Button(action: {
                    viewModel.toggleAll()
                }, label: {
                    Image(systemName: "checklist")
                        .font(.title)
                })
                .frame(maxWidth: 44, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.green)
                .background(.white)
                .cornerRadius(8)
                .padding()

                Button(action: {
                    completed(viewModel.selectedIds)
                    dismiss()
                }, label: {
                    Text("done")
                })
                .frame(maxWidth: .infinity, alignment: .center)
                .frame(height: 44)
                .foregroundColor(.blue)
                .background(.yellow)
                .cornerRadius(8)
                .padding()
                .frame(maxWidth: .infinity)
            }
        }
    }
}

struct CreateWorkoutItemPickerView_Previews: PreviewProvider {
    static let fakeValues = [CreateWorkoutItemPickerModel(id: 1, name: "one"),
                             CreateWorkoutItemPickerModel(id: 2, name: "two"),
                             CreateWorkoutItemPickerModel(id: 3, name: "three")]
    
    static var previews: some View {
        CreateWorkoutItemPickerView(viewModel: CreateWorkoutItemPickerViewModel(allValues: fakeValues, selectedIds: [1]), completed: { selectedIds in
            
        })
    }
}
