import SwiftUI
import VNCCore

struct SoftKeyboardBar: View {
    let controller: SessionController
    @Binding var isExpanded: Bool

    var body: some View {
        VStack(spacing: 8) {
            HStack(spacing: 8) {
                key("esc", wide: false) { controller.sendEscape() }
                key("tab", wide: false) { controller.sendTab() }
                key("⏎", wide: false) { controller.sendReturn() }
                iconKey("delete.left") { controller.sendBackspace() }
                Spacer(minLength: 6)
                arrowCluster
                Button {
                    withAnimation(.snappy(duration: 0.22)) { isExpanded.toggle() }
                } label: {
                    Image(systemName: isExpanded ? "chevron.down.circle.fill" : "fn")
                        .font(.callout.weight(.semibold))
                }
                .buttonStyle(.plain)
                .frame(width: 36, height: 32)
                .accessibilityLabel(isExpanded ? "Collapse function keys" : "Expand function keys")
            }
            if isExpanded {
                HStack(spacing: 6) {
                    ForEach(1...12, id: \.self) { idx in
                        Button("F\(idx)") {
                            controller.sendFunctionKey(idx)
                        }
                        .buttonStyle(.plain)
                        .font(.caption.weight(.medium))
                        .frame(maxWidth: .infinity)
                        .frame(height: 28)
                        .background(
                            RoundedRectangle(cornerRadius: 8, style: .continuous)
                                .fill(Color.primary.opacity(0.08))
                        )
                    }
                }
            }
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 10)
        .glassSurface(in: RoundedRectangle(cornerRadius: 22, style: .continuous))
        .padding(.horizontal, 12)
    }

    private func key(_ label: String, wide: Bool, action: @escaping () -> Void) -> some View {
        Button(action: action) {
            Text(label)
                .font(.callout.weight(.medium))
                .frame(width: wide ? 64 : 44, height: 32)
                .background(
                    RoundedRectangle(cornerRadius: 10, style: .continuous)
                        .fill(Color.primary.opacity(0.10))
                )
        }
        .buttonStyle(.plain)
    }

    private func iconKey(_ systemName: String, action: @escaping () -> Void) -> some View {
        Button(action: action) {
            Image(systemName: systemName)
                .font(.callout.weight(.medium))
                .frame(width: 44, height: 32)
                .background(
                    RoundedRectangle(cornerRadius: 10, style: .continuous)
                        .fill(Color.primary.opacity(0.10))
                )
        }
        .buttonStyle(.plain)
    }

    private var arrowCluster: some View {
        HStack(spacing: 4) {
            iconKey("arrow.left") { controller.sendArrow(.left) }
            iconKey("arrow.up") { controller.sendArrow(.up) }
            iconKey("arrow.down") { controller.sendArrow(.down) }
            iconKey("arrow.right") { controller.sendArrow(.right) }
        }
    }
}
