//
//  FlyerTemplateSelectionView.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 25/11/2024.
//

import SwiftUI

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

    var image: SQImage {
        switch self {
        case .template1:
            SQImage("flyer_template_1", height: 284)
        case .template2:
            SQImage("flyer_template_2", height: 284)
        case .template3:
            SQImage("flyer_template_3", height: 284)
        case .template4:
            SQImage("flyer_template_4", height: 284)
        }
    }

    var nbOfPictures: Int {
        switch self {
        case .template1:
            1
        case .template2:
            3
        case .template3:
            4
        case .template4:
            2
        }
    }

    func imageColorTemplate(isLight: Bool) -> SQImage {
        switch self {
        case .template1:
            if isLight {
                return SQImage("flyer_color_template_1_black", height: 284)
            } else {
                return SQImage("flyer_color_template_1_white", height: 284)
            }
        case .template2:
            return SQImage("flyer_color_template_2", height: 284)
        case .template3:
            if isLight {
                return SQImage("flyer_color_template_3_black", height: 284)
            } else {
                return SQImage("flyer_color_template_3_white", height: 284)
            }
        case .template4:
            if isLight {
                return SQImage("flyer_color_template_4_black", height: 284)
            } else {
                return SQImage("flyer_color_template_4_white", height: 284)
            }
        }
    }
}

struct FlyerTemplateSelectionView: View {
    @State var goToNext: Bool = false
    @State var selectedTemplate: FlyerTemplate?

    var body: some View {
        VStack {
            ScrollView {
                LazyVStack(spacing: 16) {
                    ForEach(FlyerTemplate.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) {
//            FlyerColorSelectionView(selectedTemplate: selectedTemplate ?? FlyerTemplate.template1)
        }
    }
}

#Preview {
    FlyerTemplateSelectionView()
}
