import SwiftUI
import PhotosUI
import Bedrock

struct PhotoPickerFlow: View {
    let aspectRatio: CropAspectRatio
    let allowAspectRatioSelection: Bool
    let isLogoImage: Bool
    let onComplete: (Data?) -> Void

    @State private var selectedPhotoItem: PhotosPickerItem?
    @State private var imageData: Data?
    @State private var showingCropper = false
    @State private var showingLogoEditor = false
    @State private var croppedLogoImage: UIImage?
    @State private var pickerID = UUID()

    var body: some View {
        NavigationStack {
            PhotosPicker(
                selection: $selectedPhotoItem,
                matching: .images,
                photoLibrary: .shared()
            ) {
                EmptyView()
            }
            .photosPickerStyle(.inline)
            .photosPickerDisabledCapabilities([.selectionActions])
            .ignoresSafeArea()
            .id(pickerID)
            .onChange(of: selectedPhotoItem) { _, newValue in
                guard let newValue else { return }
                Task { @MainActor in
                    if let data = try? await newValue.loadTransferable(type: Data.self) {
                        imageData = data
                        showingCropper = true
                    }
                }
            }
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button(String.localized("Cancel")) {
                        onComplete(nil)
                    }
                }
            }
        }
        .overlay {
            if showingCropper, let imageData {
                PhotoCropperSheet(
                    imageData: imageData,
                    aspectRatio: aspectRatio,
                    allowAspectRatioSelection: allowAspectRatioSelection,
                    shouldDismissOnComplete: false
                ) { croppedData in
                    if let croppedData {
                        if isLogoImage, let uiImage = UIImage(data: croppedData) {
                            croppedLogoImage = uiImage
                            showingCropper = false
                            showingLogoEditor = true
                        } else {
                            onComplete(croppedData)
                        }
                    } else {
                        showingCropper = false
                        self.imageData = nil
                        self.selectedPhotoItem = nil
                        pickerID = UUID()
                    }
                }
                .transition(.move(edge: .trailing))
            }

            if showingLogoEditor, let logoImage = croppedLogoImage {
                LogoEditorSheet(logoImage: logoImage) { finalData in
                    if let finalData {
                        onComplete(finalData)
                    } else {
                        showingLogoEditor = false
                        croppedLogoImage = nil
                        self.imageData = nil
                        self.selectedPhotoItem = nil
                        pickerID = UUID()
                    }
                }
                .transition(.move(edge: .trailing))
            }
        }
        .animation(.easeInOut(duration: Design.Animation.quick), value: showingCropper)
        .animation(.easeInOut(duration: Design.Animation.quick), value: showingLogoEditor)
    }
}
