//
//  CardTemplateSelectionView.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 12/11/2024.
//

import SwiftUI

enum CardTemplate: String, CaseIterable {
    case template1
    case template2
    case template3
    case template4

    var image: SQImage {
        switch self {
        case .template1:
            SQImage("visit_card_template_1", height: 222)
        case .template2:
            SQImage("visit_card_template_2", height: 222)
        case .template3:
            SQImage("visit_card_template_3", height: 222)
        case .template4:
            SQImage("visit_card_template_4", height: 222)
        }
    }

    func imageColorTemplate(isLight: Bool) -> SQImage {
        switch self {
        case .template1:
            return SQImage("visit_card_color_template_1", height: 222)
        case .template2:
            return SQImage("visit_card_color_template_2", height: 222)
        case .template3:
            return SQImage("visit_card_color_template_3", height: 222)
        case .template4:
            if isLight {
                return SQImage("visit_card_color_template_4_black", height: 222)
            } else {
                return SQImage("visit_card_color_template_4_white", height: 222)
            }
        }
    }
}

struct CardTemplateSelectionView: View {
    @State var goToNext: Bool = false
    @State var selectedTemplate: CardTemplate?

    var body: some View {
        VStack {
            ScrollView {
                LazyVStack(spacing: 16) {
                    ForEach(CardTemplate.allCases, id: \.self) { template in
                        template.image
                            .overlay(
                                Rectangle()
                                    .inset(by: selectedTemplate == template ? 1 : 0.5)
                                    .stroke(selectedTemplate == template ? Color.sqNeutral(100) : Color.sqNeutral(20), lineWidth: selectedTemplate == template ? 2 : 1)
                            )
                            .opacity(selectedTemplate != nil ? selectedTemplate == template ? 1 : 0.5 : 1)
                            .onTapGesture {
                                selectedTemplate = template
                            }
                    }
                }
            }
            SQFooter {
                SQButton("Continuer") {
                    if selectedTemplate != nil {
                        goToNext.toggle()
                    }
                }
                .disabled(selectedTemplate == nil)
            }
        }
        .sqNavigationBar(title: "Choix du modèle")
        .navigationDestination(isPresented: $goToNext) {
            CardColorSelectionView(selectedTemplate: selectedTemplate ?? CardTemplate.template1)
        }
    }
}

#Preview {
    CardTemplateSelectionView()
}
