//
//  ResiliationViewModel.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 14/10/2024.
//

import Foundation
import SwiftUICore

class ResiliationViewModel: ObservableObject {
    @Published var resiliationType: ResiliationType
    @Published var resiliationCheckSteps: [ResiliationCheckStep]
    @Published var resiliationReasons: [ResiliationReason]
    @Published var selectedReason: ResiliationReason?
    @Published var promotionalScreens: [ResiliationPromotionalScreen] = []
    @Published var currentPromotionalScreenIndex: Int = 0
    @Published var shouldShowConfirmationScreen: Bool = false

    var currentPromotionalScreen: ResiliationPromotionalScreen? {
        guard currentPromotionalScreenIndex < promotionalScreens.count else {
            return nil
        }
        return promotionalScreens[currentPromotionalScreenIndex]
    }

    var hasNextPromotionalScreen: Bool {
        currentPromotionalScreenIndex < promotionalScreens.count
    }

    var nextPromotionalScreen: ResiliationPromotionalScreen? {
        hasNextPromotionalScreen ? promotionalScreens[currentPromotionalScreenIndex + 1] : nil
    }

    init(resiliationType: ResiliationType) {
        self.resiliationType = resiliationType
        self.resiliationCheckSteps = ResiliationCheckStep.getCheckSteps(for: resiliationType)
        self.resiliationReasons = ResiliationReason.getRadioResiliation(for: resiliationType)
    }

    func setSelectedReason(_ reason: ResiliationReason) {
        selectedReason = reason
        promotionalScreens = selectResiliationPromotions(
            availablePromotions: getAvailablePromotionalScreens(for: reason),
            eligibleScreens: getEligiblePromotionalScreens()
        )
        currentPromotionalScreenIndex = 0
        shouldShowConfirmationScreen = false
    }

    // OK
    private func selectResiliationPromotions(availablePromotions: [ResiliationPromotionalScreen], eligibleScreens: [ResiliationPromotionalScreen]) -> [ResiliationPromotionalScreen] {
        let promotionsShuffuled = availablePromotions.shuffled()

        let commonPromotions = promotionsShuffuled.filter { promotion in
            eligibleScreens.contains(promotion)
        }

        return Array(commonPromotions.prefix(2))
    }

    private func getAvailablePromotionalScreens(for reason: ResiliationReason) -> [ResiliationPromotionalScreen] {
        switch reason {
        case .noPositiveResponses:
            return [.externalReview, .webinarPresentation, .profileCompletion]
        case .uninterestingRequests:
            return [.webinarPresentation, .editPerimeter, .onlyProSearches]
        case .lowDemand:
            return [.externalReview, .profileCompletion, .editPerimeter, .oneMonthBoosterOffered]
        case .betterPlatforms:
            // TODO: Rajouter présentation Logiciel
            return [.oneMonthBoosterOffered, .allovoisinsPromotion, .extendMyTrialPeriod, .webinarPresentation]
        case .notPro:
            return [.askPartProStatusChange]
        case .insufficientTime:
            return [.extendMyTrialPeriod]
        case .misunderstoodPremier:
            // TODO: Check si c'est le bon .cusrommerSupport*
            return [.webinarPresentation, .customerSupportForPro]
        default:
            return [.allovoisinsPromotion]
        }
    }

    private func getEligiblePromotionalScreens() -> [ResiliationPromotionalScreen] {
        return [.extendMyTrialPeriod, .profileCompletion, .allovoisinsPromotion, .externalReview, .webinarPresentation, .editPerimeter, .onlyProSearches, .askPartProStatusChange, .customerSupportForPro]
    }

    @ViewBuilder
    func getPromotionalScreenNew(for screen: ResiliationPromotionalScreen) -> some View {
        switch screen {
        case .webinarPresentation:
            WebinaireScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .profileCompletion:
            ProfileCompletionScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .editPerimeter:
            ResizePerimeterScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .onlyProSearches:
            OnlyProRequestsScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .oneMonthBoosterOffered:
            BoosterSubscriptionSelectionScreen()
        case .allovoisinsPromotion:
            AlloVoisinReputationScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .extendMyTrialPeriod:
            MoreTimeScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .askPartProStatusChangeWithCancellation, .askPartProStatusChange:
            StatusChangeScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        case .customerSupportForPro, .customerSupportForPart:
            PersonalizedSupportScreen(viewModel: PromotionalScreenViewModel(nextPromotionalScreen: nextPromotionalScreen))
        default:
            EmptyView()
        }
    }
}
