import SwiftUI
import VNCCore

struct SessionToolbar: View {
    let controller: SessionController
    @Binding var inputMode: InputMode
    @Binding var showKeyboardBar: Bool
    @Binding var selectedScreenID: UInt32?
    var onScreenshot: () -> Void
    var onDisconnect: () -> Void

    var body: some View {
        HStack(spacing: 10) {
            modePicker
                .frame(maxWidth: 180)

            iconButton(systemName: "keyboard",
                       label: "Toggle keyboard bar",
                       isOn: showKeyboardBar) {
                showKeyboardBar.toggle()
            }

            if controller.screens.count > 1 {
                screenMenu
            }

            iconButton(systemName: "camera",
                       label: "Take screenshot") {
                onScreenshot()
            }

            Spacer(minLength: 0)

            iconButton(systemName: "xmark",
                       label: "Disconnect",
                       tint: .red) {
                onDisconnect()
            }
        }
        .padding(.horizontal, 14)
        .padding(.vertical, 8)
        .glassSurface(in: Capsule())
        .padding(.horizontal, 12)
    }

    private var modePicker: some View {
        Picker("Input mode", selection: $inputMode) {
            Image(systemName: "hand.tap.fill")
                .accessibilityLabel("Touch")
                .tag(InputMode.touch)
            Image(systemName: "rectangle.and.hand.point.up.left.filled")
                .accessibilityLabel("Trackpad")
                .tag(InputMode.trackpad)
        }
        .pickerStyle(.segmented)
    }

    private var screenMenu: some View {
        Menu {
            Button {
                selectedScreenID = nil
            } label: {
                Label("All screens",
                      systemImage: selectedScreenID == nil ? "checkmark" : "rectangle.on.rectangle")
            }
            ForEach(controller.screens) { screen in
                Button {
                    selectedScreenID = screen.id
                } label: {
                    Label(
                        "Screen \(String(screen.id))  \(Int(screen.frame.width))×\(Int(screen.frame.height))",
                        systemImage: selectedScreenID == screen.id ? "checkmark" : "display"
                    )
                }
            }
        } label: {
            iconBadge(systemName: "rectangle.on.rectangle", tint: .primary)
        }
        .accessibilityLabel("Choose monitor")
    }

    private func iconButton(systemName: String,
                            label: String,
                            isOn: Bool = false,
                            tint: Color = .primary,
                            action: @escaping () -> Void) -> some View {
        Button(action: action) {
            iconBadge(systemName: systemName, tint: tint, isOn: isOn)
        }
        .accessibilityLabel(label)
        .accessibilityIdentifier(label)
        .buttonStyle(.plain)
    }

    private func iconBadge(systemName: String, tint: Color = .primary, isOn: Bool = false) -> some View {
        Image(systemName: systemName)
            .font(.callout.weight(.semibold))
            .foregroundStyle(tint == .red ? Color.red : tint)
            .frame(width: 34, height: 34)
            .background(
                Circle().fill(isOn ? tint.opacity(0.20) : Color.clear)
            )
            .contentShape(Rectangle())
    }
}
