import Foundation
import SwiftUI
import AVKit
import Adamantium

struct LiveCameraDemoView: View {
    
    @ObservedObject var captureCoordinator = CaptureCoordinator()

    let width: CGFloat
    let height: CGFloat
    
    var body: some View {
        ZStack {
//            let photoURL = captureCoordinator.capturedPhotoURL
//            let videoURL = captureCoordinator.capturedVideoURL
//            
//            var needsPreview: Bool {
//                return videoURL == nil && photoURL == nil
//            }
//            
//            if let videoURL {
//                VideoPlayer(player: AVPlayer(url: videoURL))
//            } else if let photoURL {
//                AsyncImage(
//                    url: photoURL) { image in
//                        image
//                            .resizable()
//                            .aspectRatio(contentMode: .fit)
//                    } placeholder: {
//                        ZStack {
//                            Color.clear
//                            ProgressView()
//                                .progressViewStyle(.circular)
//                        }
//                    }
//            }
//
//            Group {
//                CaptureView(
//                    captureCoordinator,
//                    shouldComposeOnCapture: false,
//                    onCapturePhoto: { url in
//                        print("Captured Photo", url)
//                    },
//                    onCaptureVideo: { url in
//                        print("Captured Video", url)
//                    },
//                    onCaptureAudio: { url in
//                        print("Captured Audio", url)
//                    },
//                    onComposeVideoAndAudio: { url in
//                        print("Composed Video", url)
//                    }, 
//                    onFrameUpdate: { frameMetaData in
//                        print("Update At Delta Time: ", frameMetaData.deltaTime, "state: ", frameMetaData.captureRecordingState)
//                    })
//                    .onDisappear {
//                        captureCoordinator.endVideoRecording()
//                    }
//                captureControls
//            }
//            .offset(x: needsPreview ? .zero : width)
//            
//            if !needsPreview {
//                VStack {
//                    HStack {
//                        Spacer()
//                        Button {
//                            captureCoordinator.resetAfterActiveSession()
//                        } label: {
//                            ZStack {
//                                RoundedRectangle(cornerRadius: .infinity)
//                                    .fill(Color.black.opacity(0.25))
//                                Image(systemName: "xmark")
//                                    .font(.system(size: 12.0))
//                                    .foregroundStyle(.white)
//                            }
//                        }
//                        .frame(width: 44.0, height: 44.0)
//                    }
//                    .padding(.vertical, 32.0)
//                    .padding(.horizontal, 32.0)
//                    Spacer()
//                }
//            }
        }
    }
}

extension LiveCameraDemoView {
    
    @ViewBuilder
    var captureControls: some View {
        VStack {
            Spacer()
            ZStack {
                Group {
                    flipButtonRow
                    mainButtonRow
                }
                .padding(.horizontal, 32.0)
            }
            .frame(height: 128.0)
            .background {
                Color.black.opacity(0.2)
            }
        }
    }
    
    @ViewBuilder
    var flipButtonRow: some View {
        HStack {
            Spacer()
            Button {
                captureCoordinator.flipCamera()
            } label: {
                ZStack {
                    RoundedRectangle(cornerRadius: 4.0)
                        .stroke(.white, lineWidth: 2.0)
                        .frame(width: 42.0, height: 42.0)
                    
                    Image(systemName: "camera")
                        .font(.system(size: 16.0, weight: .bold))
                        .foregroundStyle(.white)
                }
            }
        }
    }
    
    @ViewBuilder
    var mainButtonRow: some View {
        
        HStack {
            Spacer()
            RoundedRectangle(cornerRadius: .infinity)
                .fill(.red)
                .frame(width: 64.0, height: 64.0)
                .gesture(tapGesture
                    .exclusively(before: dragGesture)
                )
            
            Spacer()
        }
    }
}

private extension LiveCameraDemoView {
    
    var tapGesture: some Gesture {
        TapGesture()
        .onEnded({
            captureCoordinator.capturePhoto()
        })
    }
    
    var dragGesture: some Gesture {
        DragGesture(minimumDistance: .zero)
        .onChanged({ gesture in
            var scalar: CGFloat {
                if gesture.translation.height < .zero {
                    return gesture.translation.height.magnitude / width
                } else {
                    return .zero
                }
            }
            
            var newScale = 1.0 + scalar
            newScale = newScale * newScale
            captureCoordinator.zoom = max(newScale, 1.0)
            
            captureCoordinator.startVideoRecording()
        })
        .onEnded({ gesture in
            captureCoordinator.endVideoRecording()
            captureCoordinator.zoom = 1.0
        })
    }
}
