import ComponentLibrary
import ComposableArchitecture
import Foundation
import Localization
import SwiftUI

public struct BrandedAlertHookStatusView: View {
    @Bindable var store: StoreOf<BrandedAlertHookStatus>

    public var body: some View {
        content
            .padding(.horizontal, 50)
    }

    @ViewBuilder
    private var content: some View {
        VStack(spacing: 0) {
            Text(L10n.FeatureHooks.videoFailedToPost)
                .typographyV1(.titleFont)
                .multilineTextAlignment(.center)
                .fixedSize(horizontal: false, vertical: true)
                .foregroundColor(.SemanticV1.textPrimary)
                .padding(.bottom, 8)

            HStack(spacing: 0) {
                Text("\(L10n.FeatureHooks.reason) ")
                    .typographyV1(.reasonPrefixFont)
                    .fixedSize(horizontal: false, vertical: true)
                    .foregroundColor(.SemanticV1.textSecondary)

                Text(store.style.formattedReasons)
                    .typographyV1(.reasonFont)
                    .multilineTextAlignment(.center)
                    .fixedSize(horizontal: false, vertical: true)
                    .foregroundColor(.SemanticV1.textSecondary)
            }
            .padding(.bottom, 16)

            Text(contentGuidelinesAttributedString())
                .typographyV1(.detailsFont)
                .multilineTextAlignment(.center)
                .fixedSize(horizontal: false, vertical: true)
                .foregroundColor(.SemanticV1.textSecondary)
                .padding(.bottom, 32)
                .environment(\.openURL, OpenURLAction { url in
                    store.send(.openURL(url))
                    return .handled
                })

            Button {
                store.send(.close)
            } label: {
                Text(L10n.FeatureHooks.close)
                    .typographyV1(.buttonTextFont)
                    .foregroundColor(.SemanticV1.textPrimary)
                    .frame(maxWidth: .infinity)
                    .padding(.vertical, 20)
                    .background(Color.SemanticV1.backgroundTertiary)
            }
            .cornerRadius(12)
        }
        .padding(32)
        .background(Color.SemanticV1.backgroundSecondary)
        .cornerRadius(24)
    }

    private func contentGuidelinesAttributedString() -> AttributedString {
        let fullText = L10n.FeatureHooks.contentGuidelinesDetails(
            HooksGuidelinesConstants.guidelinesLinkText,
            HooksGuidelinesConstants.supportEmailText
        )

        var attributedString = AttributedString(fullText)

        // Add link attribute to guidelines text
        if let guidelinesRange = attributedString.range(of: HooksGuidelinesConstants.guidelinesLinkText) {
            attributedString[guidelinesRange].link = HooksGuidelinesConstants.guidelinesURL
            attributedString[guidelinesRange].underlineStyle = .single
            attributedString[guidelinesRange].foregroundColor = .SemanticV1.textSecondary
            attributedString[guidelinesRange].font = TypographyV1.detailsFontBold.font
        }

        // Add link attribute to email text
        if let emailRange = attributedString.range(of: HooksGuidelinesConstants.supportEmailText) {
            attributedString[emailRange].link = HooksGuidelinesConstants.supportEmailURL
            attributedString[emailRange].underlineStyle = .single
            attributedString[emailRange].foregroundColor = .SemanticV1.textSecondary
            attributedString[emailRange].font = TypographyV1.detailsFontBold.font
        }

        return attributedString
    }
}

public enum HooksGuidelinesConstants {
    public static let supportEmailText = "support@suno.com"
    public static let supportEmailURL = URL(string: "mailto:support@suno.com")!
    public static let guidelinesLinkText = L10n.FeatureHooks.contentGuidelines
    public static let guidelinesURL = URL(string: "https://suno.com/community-guidelines")!
}

private extension TypographyV1 {
    static let titleFont: TypographyV1 = .init(
        name: "Title Font",
        size: 18,
        style: .body,
        weight: .ppNeueMontrealBold,
        kerning: 0.36,
        lineHeight: 24
    )

    static let reasonPrefixFont: TypographyV1 = .init(
        name: "Reason Prefix Font",
        size: 16,
        style: .body,
        weight: .ppNeueMontrealRegular,
        kerning: 0.32,
        lineHeight: 24
    )

    static let reasonFont: TypographyV1 = .init(
        name: "Reason Font",
        size: 16,
        style: .body,
        weight: .ppNeueMontrealBold,
        kerning: 0.32,
        lineHeight: 24
    )

    static let detailsFont: TypographyV1 = .init(
        name: "Details Font",
        size: 14,
        style: .body,
        weight: .ppNeueMontrealRegular,
        kerning: 0.28,
        lineHeight: 20
    )

    static let detailsFontBold: TypographyV1 = .init(
        name: "Details Font Bold",
        size: 14,
        style: .body,
        weight: .ppNeueMontrealBold,
        kerning: 0.28,
        lineHeight: 20
    )

    static let buttonTextFont: TypographyV1 = .init(
        name: "Button Text Font",
        size: 18,
        style: .body,
        weight: .ppNeueMontrealMedium,
        kerning: 0.36,
        lineHeight: 24
    )
}
