import ComponentLibrary
import Foundation
import Localization
import SwiftUI

public enum HooksPostHookStatus: Equatable {
    case loading // Covers both uploading and awaiting readiness states
    case uploadFailed // Video upload failed (network/connection issues)
    case hookProcessingFailed // Network error while polling for hook status
    case hookFailedModeration // Hook failed moderation on server
    case success
    case failure // General hook creation failures
}

public struct HooksPostHookToast: View {
    public struct ToastState: Equatable {
        public var status: HooksPostHookStatus
        public var thumbnailImage: UIImage?
        public var isVisible: Bool

        public init(status: HooksPostHookStatus, thumbnailImage: UIImage? = nil, isVisible: Bool = true) {
            self.status = status
            self.thumbnailImage = thumbnailImage
            self.isVisible = isVisible
        }

        var statusText: String {
            switch status {
            case .loading:
                return "\(L10n.FeatureHooks.postingHook)..."
            case .success:
                return L10n.FeatureHooks.hookPosted
            case .uploadFailed:
                return L10n.FeatureHooks.videoUploadFailed
            case .failure:
                return L10n.FeatureHooks.somethingWentWrong
            case .hookProcessingFailed:
                return L10n.FeatureHooks.somethingWentWrong
            case .hookFailedModeration:
                return L10n.FeatureHooks.failedToPostHook
            }
        }

        var statusSubtitleText: String? {
            switch status {
            case .loading, .success:
                return nil
            case .uploadFailed:
                return L10n.FeatureHooks.pleaseTryAgain
            case .failure:
                return L10n.FeatureHooks.pleaseTryAgain
            case .hookProcessingFailed:
                return L10n.FeatureHooks.pleaseTryAgain
            case .hookFailedModeration:
                return L10n.FeatureHooks.failedModeration
            }
        }

        var buttonText: String? {
            switch status {
            case .loading:
                return L10n.FeatureHooks.cancel
            case .success:
                return L10n.FeatureHooks.share
            case .uploadFailed, .failure, .hookProcessingFailed, .hookFailedModeration:
                return nil
            }
        }
    }

    let toastState: ToastState
    let buttonAction: () -> Void

    public init(toastState: ToastState, buttonAction: @escaping () -> Void) {
        self.toastState = toastState
        self.buttonAction = buttonAction
    }

    public var body: some View {
        HStack(spacing: 0) {
            thumbnailImage
            statusTextView
            Spacer()
            postActionButton
        }
        .background(Color.SemanticV1.backgroundSecondary)
        .cornerRadius(14)
    }

    @ViewBuilder
    private var thumbnailImage: some View {
        ZStack {
            if let thumbnailImage = toastState.thumbnailImage {
                Image(uiImage: thumbnailImage)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
            } else {
                Color.SemanticV1.backgroundTertiary
            }

            if toastState.status == .loading {
                GradientSpinner(size: .large,
                                startColor: .SemanticV2.accentPink.opacity(0.2),
                                endColor: .SemanticV2.accentPink)
            } else if toastState.status == .success {
                checkView
            }
        }
        .frame(width: 36, height: 44)
        .clipShape(RoundedRectangle(cornerRadius: 8))
        .padding(8)
    }

    @ViewBuilder
    private var statusTextView: some View {
        VStack(alignment: .leading, spacing: 0) {
            Text(toastState.statusText)
                .multilineTextAlignment(.leading)
                .typographyV1(.statusText.kerning(0.32))
                .foregroundStyle(Color.SemanticV1.textPrimary)

            if let subtitleText = toastState.statusSubtitleText {
                Text(subtitleText)
                    .multilineTextAlignment(.leading)
                    .typographyV1(.statusSubtitleText)
                    .foregroundStyle(Color.SemanticV1.textPrimary)
                    .opacity(0.4)
                    .padding(.top, 4)
            }
        }
        .padding(.leading, 4)
        .padding(.vertical, 8)
    }

    @ViewBuilder
    private var postActionButton: some View {
        if let buttonText = toastState.buttonText {
            Button {
                buttonAction()
            } label: {
                Text(buttonText)
                    .typographyV1(.buttonText)
                    .foregroundStyle(Color.SemanticV2.accentPink)
            }
            .padding(.trailing, 18)
        }
    }

    @ViewBuilder
    private var checkView: some View {
        Image.Icon.circleCheck
            .resizable()
            .frame(width: 24, height: 24)
            .foregroundStyle(Color.SemanticV2.accentPink)
    }
}

private extension TypographyV1 {
    static let statusText: TypographyV1 = .init(
        name: "Status Text",
        size: 16,
        style: .body,
        weight: .ppNeueMontrealMedium,
        lineHeight: 18
    )

    static let statusSubtitleText: TypographyV1 = .init(
        name: "Status Subtitle Text",
        size: 14,
        style: .body,
        weight: .ppNeueMontrealRegular,
        lineHeight: 16
    )

    static let buttonText: TypographyV1 = .init(
        name: "Button Text",
        size: 14,
        style: .body,
        weight: .ppNeueMontrealMedium,
        lineHeight: 18
    )
}

// MARK: - Previews
#Preview {
    HooksPostHookToast(
        toastState: .init(
            status: HooksPostHookStatus.success,
            thumbnailImage: nil
        ),
        buttonAction: {
        }
    )
    .frame(maxHeight: .infinity, alignment: .top)
}
