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

import SwiftUI

struct ResiliationReasonScreen: View {
    @Environment(\.dismiss) var dismiss
    @ObservedObject var viewModel: ResiliationViewModel
    @State private var selectedIndex: Int? = nil
    @State private var resiliationOtherMotif: String = ""
    @State private var navigateToPromotionalScreen = false

    var body: some View {
        ScrollView {
            VStack(spacing: 16) {
                SQRadio(title: "Aidez-nous à nous améliorer : précisez le motif de votre résiliation",
                        options: viewModel.resiliationReasons.map { SQRadioOption(title: $0.text) },
                        selectedIndex: $selectedIndex)
                if selectedIndex == viewModel.resiliationReasons.count - 1 {
                    SQTextField("", text: $resiliationOtherMotif)
                        .sqPlaceholder("Précisez-nous le motif de votre résiliation")
                }

                HStack {
                    SQButton("Annuler") {
                        dismiss()
                    }
                    .sqButtonType(.glass)
                    SQButton("Continuer") {
                        if let index = selectedIndex {
                            let selectedReason = viewModel.resiliationReasons[index]
                            viewModel.setSelectedReason(selectedReason)
                            navigateToPromotionalScreen = true
                        }
                    }
                    .disabled(selectedIndex == nil)
                }
                Spacer()
            }
        }
        .padding()
        .sqNavigationBar(title: "Demander la résiliation")
        .navigationDestination(isPresented: $navigateToPromotionalScreen) {
            if viewModel.hasNextPromotionalScreen {
                viewModel.getPromotionalScreenNew(for: viewModel.currentPromotionalScreen!)
            } else {
                ResiliationConfirmationScreen()
            }
        }
    }
}

#Preview {
    ResiliationReasonScreen(viewModel: ResiliationViewModel(resiliationType: .apProWithTrial))
}
