 //
//  CreditsInfoCard.swift
//  wake
//
//  Created by fairclip on 2025/8/19.
//

import SwiftUI

// MARK: - 积分信息卡片组件
struct CreditsInfoCard: View {
    let totalCredits: Int
    let onInfoTap: (() -> Void)?
    let onDetailTap: (() -> Void)?
    
    @State private var showInfoPopover = false
    
    init(
        totalCredits: Int,
        onInfoTap: (() -> Void)? = nil,
        onDetailTap: (() -> Void)? = nil
    ) {
        self.totalCredits = totalCredits
        self.onInfoTap = onInfoTap
        self.onDetailTap = onDetailTap
    }
    
    var body: some View {
        Button(action: {
            onDetailTap?()
        }) {
            mainCreditsSection
        }
        .buttonStyle(PlainButtonStyle())
        .background(Theme.Colors.primaryLight)
        .cornerRadius(Theme.CornerRadius.extraLarge)
        .shadow(color: Theme.Shadows.small, radius: Theme.Shadows.cardShadow.radius, x: Theme.Shadows.cardShadow.x, y: Theme.Shadows.cardShadow.y)
    }
    
    // MARK: - 主要积分显示区域
    private var mainCreditsSection: some View {
        HStack(spacing: Theme.Spacing.md) {
            // 积分图标和数量
            HStack(spacing: Theme.Spacing.sm) {
                Text("Credits:")
                    .font(Typography.font(for: .subtitle, family: .quicksandBold))
                    .foregroundColor(Theme.Colors.textPrimary)
                
                Text("\(totalCredits)")
                    .font(Typography.font(for: .subtitle, family: .quicksandBold))
                    .foregroundColor(Theme.Colors.textPrimary)
            }
            
            
            // 操作按钮区域
            HStack(spacing: Theme.Spacing.sm) {
                // 信息按钮
                Button(action: {
                    showInfoPopover = true
                    onInfoTap?()
                }) {
                    Image(systemName: "questionmark.circle")
                        .foregroundColor(Theme.Colors.textSecondary)
                        .font(.system(size: 16))
                }
                .popover(isPresented: $showInfoPopover, attachmentAnchor: .point(.bottom), arrowEdge: .top) {
                    Text("Credits can be used for material indexing (1 credit per photo or per second of video) and for buying blind boxes (100 crediteach)")
                        .font(Typography.font(for: .caption, family: .quicksandRegular))
                        .multilineTextAlignment(.center)
                        .presentationBackground(Theme.Gradients.backgroundGradient)
                        .frame(minWidth: 240, maxWidth: UIScreen.main.bounds.width * 0.6)
                        .presentationCompactAdaptation(.popover)
                        .padding(.horizontal, Theme.Spacing.md)
                        .padding(.vertical, Theme.Spacing.sm)
                }

                Spacer()
                
                // 详情按钮
                Image(systemName: "chevron.right")
                    .foregroundColor(Theme.Colors.textPrimary)
                    .font(.system(size: 14, weight: .medium))
            }
        }
        .padding(Theme.Spacing.lg)
    }
    
    
}


// MARK: - 预览
#Preview("Credits Info Card") {
    VStack(spacing: 20) {
        CreditsInfoCard(
            totalCredits: 3290,
            onInfoTap: {
                print("Info tapped")
            },
            onDetailTap: {
                print("Detail tapped")
            }
        )
    }
    .padding()
    .background(Color(.systemGroupedBackground))
}
