//
//  StatsCard.swift.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/22.
//

import SwiftUI

struct StatsCard: View {
    let totalSpent: Float
    let totalProfit: Float
    let totalTickets: Int
    let winningTickets: Int
    let pendingTickets: Int
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 顶部统计行
            HStack {
                VStack(alignment: .leading, spacing: 6) {
                    Text("总投入")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text(Formatters.formatCurrency(totalSpent))
                        .font(.title2.bold())
                        .foregroundColor(.primary)
                }
                
                Spacer()
                
                VStack(alignment: .trailing, spacing: 6) {
                    Text("总盈亏")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text(Formatters.formatCurrency(totalProfit))
                        .font(.title2.bold())
                        .foregroundColor(profitColor)
                }
            }
            
            Divider()
                .background(Color.gray.opacity(0.3))
            
            // 底部统计行
            HStack(spacing: 20) {
                StatItemView(
                    title: "总票数",
                    value: "\(totalTickets)",
                    icon: "ticket.fill",
                    color: .blue
                )
                
                StatItemView(
                    title: "中奖",
                    value: "\(winningTickets)",
                    icon: "checkmark.circle.fill",
                    color: .green
                )
                
                StatItemView(
                    title: "待开奖",
                    value: "\(pendingTickets)",
                    icon: "clock.fill",
                    color: .orange
                )
            }
        }
        .padding(20)
        .background(
            RoundedRectangle(cornerRadius: 16)
                .fill(Color(.systemBackground))
                .shadow(
                    color: Color.black.opacity(0.1),
                    radius: 8,
                    x: 0,
                    y: 4
                )
        )
        .padding(.horizontal)
    }
    
    private var profitColor: Color {
        totalProfit >= 0 ? .green : .red
    }
}

// 单个统计项目组件
struct StatItemView: View {
    let title: String
    let value: String
    let icon: String
    let color: Color
    
    var body: some View {
        VStack(spacing: 8) {
            // 图标
            ZStack {
                Circle()
                    .fill(color.opacity(0.1))
                    .frame(width: 40, height: 40)
                
                Image(systemName: icon)
                    .font(.system(size: 18))
                    .foregroundColor(color)
            }
            
            // 数值
            Text(value)
                .font(.headline)
            
            // 标题
            Text(title)
                .font(.caption2)
                .foregroundColor(.secondary)
        }
        .frame(maxWidth: .infinity)
    }
}

// 预览
struct StatsCard_Previews: PreviewProvider {
    static var previews: some View {
        StatsCard(
            totalSpent: 256.0,
            totalProfit: 42.5,
            totalTickets: 12,
            winningTickets: 3,
            pendingTickets: 5
        )
        .padding()
        .previewLayout(.sizeThatFits)
    }
}
