import SwiftUI
import AppKit

class CustomNSView: NSView {
    private static let invisibleCursor: NSCursor = {
        let image = NSImage(size: NSSize(width: 1, height: 1))
        image.lockFocus()
        NSColor.clear.set()
        NSRect(x: 0, y: 0, width: 1, height: 1).fill()
        image.unlockFocus()
        return NSCursor(image: image, hotSpot: .zero)
    }()
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    private func setupView() {
        wantsLayer = true
        layer?.backgroundColor = NSColor.clear.cgColor
    }
    
    override func resetCursorRects() {
        super.resetCursorRects()
        addCursorRect(bounds, cursor: Self.invisibleCursor)
    }
}

struct HiddenCursorView: NSViewRepresentable {
    func makeNSView(context: Context) -> CustomNSView {
        let view = CustomNSView()
        view.wantsLayer = true
        return view
    }
    
    func updateNSView(_ nsView: CustomNSView, context: Context) {
        nsView.setFrameSize(nsView.frame.size)
    }
}

struct CaptureStatusButton: View {
    let isCapturing: Bool
    let isBoardDetected: Bool
    
    var body: some View {
        HStack {
            Circle()
                .fill(statusColor)
                .frame(width: 12, height: 12)
            Text(statusText)
                .foregroundColor(statusColor)
        }
        .padding()
        .background(
            RoundedRectangle(cornerRadius: 8)
                .stroke(statusColor, lineWidth: 2)
        )
    }
    
    private var statusColor: Color {
        if isCapturing && isBoardDetected {
            return .green
        } else if isCapturing {
            return .yellow
        } else {
            return .gray
        }
    }
    
    private var statusText: String {
        if isCapturing && isBoardDetected {
            return "Capturing"
        } else if isCapturing {
            return "Waiting for board"
        } else {
            return "Not capturing"
        }
    }
}

struct ContentView: View {
    @StateObject private var viewModel = ScreenCaptureViewModel()
    
    var body: some View {
        VStack {
            // Capture status indicator
            CaptureStatusButton(
                isCapturing: viewModel.isCapturing,
                isBoardDetected: viewModel.isBoardDetected
            )
            .onAppear {
                // Start monitoring for chess boards when view appears
                viewModel.startMonitoring()
            }
            .onDisappear {
                // Stop monitoring when view disappears
                viewModel.stopMonitoring()
            }
            
            // Error display
            if let error = viewModel.captureError {
                Text(error.localizedDescription)
                    .foregroundColor(.red)
                    .padding()
            }
            
            // Image display
            HStack {
                VStack {
                    Text("Full Capture")
                    if let image = viewModel.capturedImage {
                        Image(nsImage: image)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(maxWidth: 400)
                    } else {
                        Text("No capture available")
                            .foregroundColor(.gray)
                    }
                }
                
                Divider()
                
                VStack {
                    Text("Chessboard Preview")
                    if let image = viewModel.croppedBoardImage {
                        Image(nsImage: image)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(maxWidth: 400)
                            .overlay(
                                HiddenCursorView()
                                    .allowsHitTesting(true)
                            )
                    } else {
                        Text("No board detected")
                            .foregroundColor(.gray)
                    }
                }
            }
            .padding()
            
            Spacer()
        }
        .frame(minWidth: 800, minHeight: 600)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
