import SwiftUI

public struct BlackNotificationScreen: View {
    let didFinish: () -> Void
    let requestAuthorization: () async throws -> Void

    public init(
        didFinish: @escaping () -> Void,
        requestAuthorization: @escaping () async throws -> Void
    ) {
        self.didFinish = didFinish
        self.requestAuthorization = requestAuthorization
    }

    @State private var prePrompt = true
    @State private var prePromptResponse: Bool?
    @State private var lineHeight: CGFloat = 40

    public var body: some View {
        Rectangle()
            .foregroundStyle(.black)
            .ignoresSafeArea()
            .overlay {
                if prePrompt == false {
                    glowingRectAndArrow
                }
            }
            .modifier(if: prePrompt, then: {
                $0
                    .alert("Suno Would Like to Send You Notifications", isPresented: $prePrompt, actions: {
                        Button(
                            action: { didFinish() },
                            label: { Text("Don't Allow") }
                        )
                        Button(
                            action: {
                                prePromptResponse = true
                                Task {
                                    do {
                                        try await requestAuthorization()
                                    } catch {
                                        print(error)
                                    }
                                    didFinish()
                                }
                            },
                            label: { Text("Allow") }
                        )
                    }, message: {
                        Text("Notifications may include alerts, sounds, and icon badges. These can be configured in Settings.")
                    })
            })
    }

    let gradientColors: [Color] = [
        Color(red: 199 / 256, green: 61 / 256, blue: 102 / 256),
        Color(red: 136 / 256, green: 45 / 256, blue: 210 / 256),
    ]

    @ViewBuilder private var glowingRectAndArrow: some View {
        RoundedRectangle(cornerRadius: 22, style: .continuous)
            .stroke(
                LinearGradient(colors: gradientColors, startPoint: .topLeading, endPoint: .bottomTrailing),
                lineWidth: 2
            )
            // Yes, a triple shadow. this adds more intensity to the shadow
            .shadow(color: gradientColors.first!, radius: 8)
            .shadow(color: gradientColors.first!, radius: 8)
            .shadow(color: gradientColors.first!, radius: 8)
            .frame(width: 300, height: 205)
            .overlay(alignment: .bottomTrailing) {
                ZStack {
                    Image(systemName: "arrow.up")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 30, height: 30)
                        .offset(y: -(lineHeight / 2))
                        .offset(y: 10)

                    RoundedRectangle(cornerRadius: 2, style: .continuous)
                        .frame(width: 3, height: lineHeight)
                        .offset(x: -0.1, y: -2)
                }
                .foregroundStyle(
                    LinearGradient(colors: gradientColors, startPoint: .topLeading, endPoint: .bottomTrailing)
                )
                // Yes, a double shadow. this adds more intensity to the shadow
                .shadow(color: gradientColors.first!, radius: 8)
                .shadow(color: gradientColors.first!, radius: 8)
                .padding(70)
                .offset(x: 0, y: 180)
                .onAppear {
                    withAnimation(
                        .easeInOut(duration: 0.7)
                            .repeatForever(autoreverses: true)
                    ) {
                        lineHeight = 80
                    }
                }
            }
    }
}

#Preview {
    BlackNotificationScreen(didFinish: {}, requestAuthorization: {})
}
