//
//  TicketDetailView.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/24.
//

import SwiftUI
internal import CoreData

struct TicketDetailView: View {
    let ticket: LotteryTicket
    @Environment(\.dismiss) private var dismiss
    @Environment(\.managedObjectContext) private var context
    
    var body: some View {
        ScrollView {
            VStack(spacing: 20) {
                // 顶部卡片
                VStack(spacing: 16) {
                    Image(systemName: "ticket.fill")
                        .font(.system(size: 60))
                        .foregroundColor(ticketColor)
                    
                    Text(ticket.type!)
                        .font(.largeTitle)
                        .fontWeight(.bold)
                    
                    Text(Formatters.formatCurrency(ticket.amount))
                        .font(.title2)
                        .foregroundColor(.secondary)
                }
                .padding()
                .frame(maxWidth: .infinity)
                .background(ticketColor.opacity(0.1))
                .cornerRadius(20)
                .padding()
                
//                // 号码信息
                VStack(alignment: .leading, spacing: 12) {
                    Text("彩票号码")
                        .font(.headline)
                    
                    LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 5), spacing: 10) {
                        ForEach(ticket.numberArray, id: \.self) { number in
                            Text("\(number)")
                                .font(.title3)
                                .fontWeight(.semibold)
                                .frame(width: 50, height: 50)
                                .background(Color.blue)
                                .foregroundColor(.white)
                                .clipShape(Circle())
                        }
                    }
                }
                .padding()
                .background(Color(.systemBackground))
                .cornerRadius(15)
                .shadow(color: .black.opacity(0.05), radius: 5)
                .padding(.horizontal)
                
                // 日期信息
                VStack(alignment: .leading, spacing: 8) {
                    InfoRow(title: "购买时间", value: ticket.formattedDate)
                    InfoRow(title: "开奖时间", value: ticket.formattedDrawDate)
                    InfoRow(title: "状态", value: (ticket.ticketStatus != .pending) ? "已开奖" : "待开奖")
                    
                    if ticket.ticketStatus != .pending {
                        InfoRow(
                            title: "中奖金额",
                            value: Formatters.formatCurrency(ticket.prizeAmount)
                        )
                        .foregroundColor(ticket.profit > 0 ? .green : .red)
                    }
                }
                .padding()
                .background(Color(.systemBackground))
                .cornerRadius(15)
                .shadow(color: .black.opacity(0.05), radius: 5)
                .padding(.horizontal)
            }
            .padding(.vertical)
        }
        .navigationTitle("彩票详情")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button("删除") {
                    deleteTicket()
                }
                .foregroundColor(.red)
            }
        }
    }
    
    private func deleteTicket() {
        context.delete(ticket)
        do {
            try context.save()
            dismiss()
        } catch {
            print("删除失败: \(error)")
        }
    }
    
    private var ticketColor: Color {
        switch ticket.lotteryType {
        case .doubleColorBall: return .red
        case .superLotto: return .blue
        case .happy8: return .purple
        case .sevenStar: return .green
        case .other: return .pink
        }
    }
}

struct InfoRow: View {
    let title: String
    let value: String
    
    init(title: String, value: String) {
        self.title = title
        self.value = value
    }
    
    init(title: String, value: Date, formatter: DateFormatter.Style = .medium) {
        self.title = title
        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = formatter
        self.value = dateFormatter.string(from: value)
    }
    
    var body: some View {
        HStack {
            Text(title)
                .foregroundColor(.secondary)
            Spacer()
            Text(value)
                .fontWeight(.medium)
        }
        .padding(.vertical, 4)
    }
}
