import SwiftUI
import WebRTC

struct CallView: View {
    @EnvironmentObject var viewModel: CallViewModel
    
    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(.all)
            
            // Remote Video (Full Screen)
            // Remote Video (Full Screen)
            if let remoteTrack = viewModel.remoteVideoTrack {
                VideoView(track: remoteTrack, onRenderStart: {
                    print("Remote video is visible! Notifying peer...")
                    viewModel.notifyVideoSeen()
                })
                    .edgesIgnoringSafeArea(.all)
            } else {
                Text("Waiting for video...")
                    .foregroundColor(.white)
            }
            
            // Local Video (PiP)
            VStack {
                HStack {
                    Spacer()
                    // Local Video
                    if let localTrack = viewModel.localVideoTrack {
                         VideoView(track: localTrack)
                            .frame(width: 100, height: 150)
                            .cornerRadius(8)
                            .padding()
                            .shadow(radius: 5)
                    } else {
                        Rectangle()
                            .fill(Color.gray)
                            .frame(width: 100, height: 150)
                            .overlay(Text("Local").foregroundColor(.white))
                            .cornerRadius(8)
                            .padding()
                    }
                }
                Spacer()
            }
            
            // Controls
            VStack {
                 // Toast Notification
                 if viewModel.showVideoVisibleToast {
                     Text("Your video now visible on the other end.")
                         .font(.callout)
                         .padding(10)
                         .background(Color.black.opacity(0.7))
                         .foregroundColor(.white)
                         .cornerRadius(8)
                         .padding(.top, 50)
                         .transition(.opacity)
                         .animation(.easeInOut, value: viewModel.showVideoVisibleToast)
                 }
                
                Spacer()
                Button(action: {
                    viewModel.hangup()
                }) {
                    Image(systemName: "phone.down.fill")
                        .font(.title)
                        .padding()
                        .background(Color.red)
                        .foregroundColor(.white)
                        .clipShape(Circle())
                }
                .padding(.bottom, 50)
            }
        }
        .onAppear {
            // Force keyboard dismissal
            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        }
        .ignoresSafeArea(.keyboard)
    }
}

// NOTE: To make video render, you need a SwiftUI wrapper around RTCMTLVideoView.
// This is non-trivial boilerplate that depends on the specific WebRTC package structure.
