import ComponentLibrary
import ShareAssetClient
import SwiftUI

struct ShareButton<Config: ShareButtonConfigurable>: View {
    let config: Config
    let action: () -> Void

    var body: some View {
        Button {
            action()
        } label: {
            VStack(spacing: 10) {
                config.icon
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 48, height: 48)
                    .clipShape(.circle)
                    .foregroundStyle(Color.SemanticV1.iconPrimary)
                    .overlay(alignment: .topTrailing) {
                        if config.requiresVideoRendering {
                            playIcon
                                .offset(x: 7, y: -6)
                        }
                    }
                    .offset(y: 6)

                Text(config.title)
                    .typographyV1(.caption3)
                    .lineLimit(1)
                    .foregroundStyle(Color.SemanticV1.textPrimary)
            }
            .frame(width: 72)
            .aspectRatio(1, contentMode: .fit)
        }
        .buttonStyle(.borderless)
    }

    @ViewBuilder
    private var playIcon: some View {
        ZStack {
            Circle()
                .fill(Color.SemanticV1.backgroundSecondary)
                .frame(width: 24, height: 24)

            Image.ShareIcon.playIcon
                .resizable()
                .frame(width: 20, height: 20)
        }
    }
}

#Preview {
    ScrollView {
        ForEach(ShareableTarget.defaultOrder) { action in
            ShareButton(config: action) { /* no-op */ }
        }
    }
    .frame(maxWidth: .infinity)
    .background(Color.SemanticV1.backgroundSecondary)
}
