//
//  ResiliationCheckStepsScreen.swift
//  Sequoia
//
//  Created by Victor on 10/10/2024.
//

import SwiftUI

struct ResiliationCheckStepsScreen: View {
    @State var nextStep: Bool = false
    @StateObject var viewModel: ResiliationViewModel
    @State private var selectedSteps: Set<ResiliationCheckStep> = []

    var allStepsSelected: Bool {
        selectedSteps.count == viewModel.resiliationCheckSteps.count
    }

    var body: some View {
        ScrollView {
            VStack(spacing: 16) {
                SQText("En résiliant votre abonnement vous renoncez à :", size: 18, font: .bold)
                    .multilineTextAlignment(.center)
                ForEach(viewModel.resiliationCheckSteps, id: \.self) { step in
                    ResiliationCheckStepView(
                        checkStep: step,
                        isSelected: Binding(
                            get: { selectedSteps.contains(step) },
                            set: { isSelected in
                                if isSelected {
                                    selectedSteps.insert(step)
                                } else {
                                    selectedSteps.remove(step)
                                }
                            }
                        )
                    )
                }

                if viewModel.resiliationType == .apProWithTrial || viewModel.resiliationType == .apProWithTrialWithBooster {
                    TrialWarningCellView()
                }

                HStack {
                    SQButton("Annuler", color: .sqNeutral(100), isFull: false) {

                    }
                    SQButton("Continuer", color: .sqNeutral(100), textColor: .white) {
                        nextStep.toggle()
                    }
                    .disabled(!allStepsSelected)
                }
                .padding()
            }
            .padding()
        }
        .navigationDestination(isPresented: $nextStep) {
            ResiliationReasonScreen(viewModel: viewModel)
        }
        .sqNavigationBar(title: "Demander la résiliation")
    }
}

#Preview {
    ResiliationCheckStepsScreen(viewModel: ResiliationViewModel(resiliationType: .apPart))
}
