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

import SwiftUI

struct LotteryTypePicker: View {
    @Binding var selectedType: LotteryType
    
    // 彩票类型描述
    private let typeDescriptions: [LotteryType: String] = [
        .doubleColorBall: "红球6 + 篮球1",
        .superLotto: "前区5 + 后区2",
        .happy8: "3位数 每位0-9",
        .sevenStar: "7位数 每位0-9",
        .other: "其他彩票类型"
    ]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("彩票类型")
                .font(.headline)
                .foregroundColor(.primary)
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 12) {
                    ForEach([LotteryType.doubleColorBall, LotteryType.superLotto, LotteryType.happy8]) { type in
//                    ForEach(LotteryType.allCases) { type in
                        LotteryTypeCard(
                            type: type,
                            description: typeDescriptions[type] ?? "",
                            isSelected: selectedType == type
                        )
                        .onTapGesture {
                            withAnimation(.spring()) {
                                selectedType = type
                            }
                        }
                    }
                }
                .padding(.vertical, 2)
                .padding(.horizontal, 4)
            }
        }
    }
}

// 单个彩票类型卡片
struct LotteryTypeCard: View {
    let type: LotteryType
    let description: String
    let isSelected: Bool
    
    var body: some View {
        VStack(spacing: 8) {
            // 图标
            ZStack {
                Circle()
                    .fill(backgroundColor)
                    .frame(width: 60, height: 60)
                    .scaleEffect(isSelected ? 1.15 : 1.0)
                
                Image(systemName: iconName)
                    .font(.system(size: 24))
                    .foregroundColor(iconColor)
                    .scaleEffect(isSelected ? 1.2 : 1.0)
            }
            
            // 类型名称
            Text(type.rawValue)
                .font(.system(size: 14, weight: .semibold))
                .foregroundColor(isSelected ? .primary : .secondary)
                .lineLimit(1)
                .minimumScaleFactor(0.8)
            
            // 描述
            Text(description)
                .font(.system(size: 10))
                .foregroundColor(.secondary)
                .multilineTextAlignment(.center)
                .lineLimit(2)
                .frame(width: 80)
        }
        // 放大5%，需要额外padding来补偿
        .padding(.vertical, isSelected ? 10 : 12)
        .padding(.horizontal, isSelected ? 6 : 8)
        .background(
            RoundedRectangle(cornerRadius: 12)
                .fill(isSelected ? Color.blue.opacity(0.05) : Color(.systemBackground))
                .overlay(
                    RoundedRectangle(cornerRadius: 12)
                        .stroke(isSelected ? Color.blue : Color.gray.opacity(0.3), lineWidth: isSelected ? 2 : 1)
                )
        )
        .scaleEffect(isSelected ? 1.05 : 1.0)
    }
    
    private var iconName: String {
        switch type {
        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 type {
        case .doubleColorBall: return .red
        case .superLotto: return .blue
        case .happy8: return .green
        case .sevenStar: return .purple
        case .other: return .gray
        }
    }
    
    private var backgroundColor: Color {
        isSelected ? iconColor.opacity(0.1) : Color(.systemGray6)
    }
}
