import SwiftUI

struct TextInputAlert: View {
    let title: String
    let message: String
    let placeholder: String
    let primaryButtonTitle: String
    let secondaryButtonTitle: String
    let onPrimaryAction: (String) -> Void
    let onSecondaryAction: () -> Void
    let onDismiss: () -> Void
    
    @State private var inputText: String = ""
    
    init(
        title: String,
        message: String,
        placeholder: String = "",
        primaryButtonTitle: String = "Save",
        secondaryButtonTitle: String = "Cancel",
        onPrimaryAction: @escaping (String) -> Void,
        onSecondaryAction: @escaping () -> Void = {},
        onDismiss: @escaping () -> Void = {}
    ) {
        self.title = title
        self.message = message
        self.placeholder = placeholder
        self.primaryButtonTitle = primaryButtonTitle
        self.secondaryButtonTitle = secondaryButtonTitle
        self.onPrimaryAction = onPrimaryAction
        self.onSecondaryAction = onSecondaryAction
        self.onDismiss = onDismiss
    }
    
    var body: some View {
        ZStack {
            // Background overlay
            Constants.Colors.Background.darkOverlay
                .ignoresSafeArea()
                .onTapGesture {
                    onDismiss()
                }
            
            // Alert container
            VStack(spacing: 32) {
                // Title and message section
                VStack(spacing: 8) {
                    Text(title)
                        .font(Constants.Typography.largeTitle)
                        .foregroundColor(Constants.Colors.Foreground.primary)
                        .tracking(0.36)
                        .multilineTextAlignment(.center)
                        .frame(maxWidth: .infinity)
                    
                    Text(message)
                        .font(Constants.Typography.mediumRegular)
                        .foregroundColor(Constants.Colors.Foreground.secondary)
                        .tracking(0.32)
                        .multilineTextAlignment(.center)
                        .frame(maxWidth: .infinity)
                }
                
                // Text input field
                TextField("", text: $inputText, prompt: Text(placeholder)
                    .foregroundColor(Constants.Colors.Foreground.primary.opacity(0.6)))
                    .font(Constants.Typography.small)
                    .foregroundColor(Constants.Colors.Foreground.primary)
                    .tracking(0.28)
                    .multilineTextAlignment(.center)
                    .padding(.horizontal, 16)
                    .padding(.vertical, 16)
                    .frame(height: 56)
                    .background(Color.clear)
                    .overlay(
                        RoundedRectangle(cornerRadius: 12)
                            .stroke(Color.white.opacity(0.1), lineWidth: 1)
                    )
                
                // Buttons section
                HStack(spacing: 8) {
                    LargeButton.secondary(secondaryButtonTitle) {
                        onSecondaryAction()
                        onDismiss()
                    }
                    
                    LargeButton(title: primaryButtonTitle, variant: .primary) {
                        onPrimaryAction(inputText)
                        onDismiss()
                    }
                }
            }
            .padding(32)
            .background(Constants.Colors.Background.secondary)
            .clipShape(RoundedRectangle(cornerRadius: 24))
            .frame(maxWidth: .infinity)
            .padding(.horizontal, 32)
        }
    }
}

// MARK: - Alert Modifier
extension View {
    func textInputAlert(
        isPresented: Binding<Bool>,
        title: String,
        message: String,
        placeholder: String = "",
        primaryButtonTitle: String = "Save",
        secondaryButtonTitle: String = "Cancel",
        onPrimaryAction: @escaping (String) -> Void,
        onSecondaryAction: @escaping () -> Void = {}
    ) -> some View {
        self.overlay(
            Group {
                if isPresented.wrappedValue {
                    TextInputAlert(
                        title: title,
                        message: message,
                        placeholder: placeholder,
                        primaryButtonTitle: primaryButtonTitle,
                        secondaryButtonTitle: secondaryButtonTitle,
                        onPrimaryAction: onPrimaryAction,
                        onSecondaryAction: onSecondaryAction,
                        onDismiss: {
                            withAnimation(.easeInOut(duration: 0.2)) {
                                isPresented.wrappedValue = false
                            }
                        }
                    )
                    .transition(.opacity.combined(with: .scale(scale: 0.95)))
                    .animation(.easeInOut(duration: 0.2), value: isPresented.wrappedValue)
                }
            }
        )
    }
}

// MARK: - Convenience Extensions
extension TextInputAlert {
    /// Creates a Save Lyrics alert
    static func saveLyrics(
        onSave: @escaping (String) -> Void,
        onCancel: @escaping () -> Void = {},
        onDismiss: @escaping () -> Void = {}
    ) -> TextInputAlert {
        TextInputAlert(
            title: "Save Lyrics",
            message: "Give your lyrics a title so it's easy to remember",
            placeholder: "Lonely road",
            primaryButtonTitle: "Save",
            secondaryButtonTitle: "Cancel",
            onPrimaryAction: onSave,
            onSecondaryAction: onCancel,
            onDismiss: onDismiss
        )
    }
    
    /// Creates a Save Style alert
    static func saveStyle(
        onSave: @escaping (String) -> Void,
        onCancel: @escaping () -> Void = {},
        onDismiss: @escaping () -> Void = {}
    ) -> TextInputAlert {
        TextInputAlert(
            title: "Save Style",
            message: "Give your style a name so you can find it later",
            placeholder: "Pop Rock",
            primaryButtonTitle: "Save",
            secondaryButtonTitle: "Cancel",
            onPrimaryAction: onSave,
            onSecondaryAction: onCancel,
            onDismiss: onDismiss
        )
    }
}

// MARK: - Preview
#Preview {
    VStack(spacing: 20) {
        Text("Background Content")
            .font(Constants.Typography.largeTitle)
            .foregroundColor(Constants.Colors.Foreground.primary)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Constants.Colors.Background.primary)
    .overlay(
        TextInputAlert.saveLyrics(
            onSave: { title in
                print("Save lyrics with title: \(title)")
            },
            onCancel: {
                print("Cancel save lyrics")
            },
            onDismiss: {
                print("Alert dismissed")
            }
        )
    )
}
