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

import SwiftUI
internal import CoreData

struct TicketRow: View {
    @ObservedObject var ticket: LotteryTicket
    
    var body: some View {
        HStack(spacing: 16) {
            // 左侧图标
            lotteryIcon
            
            // 中间信息
            VStack(alignment: .leading, spacing: 6) {
                HStack {
                    Text(ticket.lotteryType.rawValue)
                        .font(.headline)
                        .foregroundColor(.primary)
                    
                    Spacer()
                    
                    Text(Formatters.formatCurrency(ticket.amount))
                        .font(.headline)
                        .foregroundColor(.primary)
                }
                
                HStack(spacing: 12) {
                    Text("\(ticket.betCount)注")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    
                    // 修复这里：正确检查 numbers 是否为空
                    if let numbers = ticket.numbers, !numbers.isEmpty {
                        Text("·")
                            .foregroundColor(.secondary)
                        
                        Text(numbers)
                            .font(.caption)
                            .foregroundColor(.secondary)
                            .lineLimit(1)
                    }
                    
                    Spacer()
                    
                    statusBadge
                }
            }
        }
        .padding(.vertical, 10)
        .padding(.horizontal, 4)
    }
    
    // 彩票类型图标
    private var lotteryIcon: some View {
        ZStack {
            Circle()
                .fill(iconColor.opacity(0.1))
                .frame(width: 50, height: 50)
            
            Image(systemName: iconName)
                .font(.system(size: 22))
                .foregroundColor(iconColor)
        }
    }
    
    // 状态徽章
    private var statusBadge: some View {
        Text(ticket.ticketStatus.rawValue)
            .font(.system(size: 12, weight: .medium))
            .padding(.horizontal, 10)
            .padding(.vertical, 4)
            .background(statusColor.opacity(0.1))
            .foregroundColor(statusColor)
            .clipShape(Capsule())
    }
    
    // 根据彩票类型选择图标和颜色
    private var iconName: String {
        switch ticket.lotteryType {
        case .doubleColorBall:
            return "circle.grid.2x2.fill"
        case .superLotto:
            return "8.circle.fill"
        case .happy8:
            return "3.circle.fill"
        case .sevenStar:
            return "star.fill"
        case .other:
            return "ticket.fill"
        }
    }
    
    private var iconColor: Color {
        switch ticket.lotteryType {
        case .doubleColorBall:
            return .red
        case .superLotto:
            return .blue
        case .happy8:
            return .green
        case .sevenStar:
            return .purple
        case .other:
            return .gray
        }
    }
    
    // 根据状态选择颜色
    private var statusColor: Color {
        switch ticket.ticketStatus {
        case .pending:
            return .orange
        case .won:
            return .green
        case .lost:
            return .gray
        }
    }
}

// 预览
struct TicketRow_Previews: PreviewProvider {
    static var previews: some View {
        let context = PersistenceController.preview.container.viewContext
        
        // 创建预览票券
        let ticket = LotteryTicket(context: context)
        ticket.id = UUID()
        ticket.type = LotteryType.doubleColorBall.rawValue
        ticket.numbers = "01 02 03 04 05 06 07"  // 有号码的情况
        ticket.betCount = 2
        ticket.amount = 4
        ticket.date = Date()
        ticket.drawDate = Date().addingTimeInterval(86400)
        ticket.status = TicketStatus.pending.rawValue
        ticket.prizeAmount = 0
        
        // 测试无号码的情况
        let ticket2 = LotteryTicket(context: context)
        ticket2.id = UUID()
        ticket2.type = LotteryType.superLotto.rawValue
        ticket2.numbers = ""  // 空字符串
        ticket2.betCount = 1
        ticket2.amount = 2
        ticket2.date = Date()
        ticket2.drawDate = Date().addingTimeInterval(86400)
        ticket2.status = TicketStatus.won.rawValue
        ticket2.prizeAmount = 10
        
        return Group {
            TicketRow(ticket: ticket)
                .padding()
                .previewLayout(.sizeThatFits)
                .previewDisplayName("有号码")
            
            TicketRow(ticket: ticket2)
                .padding()
                .previewLayout(.sizeThatFits)
                .previewDisplayName("无号码")
        }
    }
}
