//
//  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个",
        .welfare3D: "3位数字，000-999",
        .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.allCases) { type in
                        LotteryTypeCard(
                            type: type,
                            description: typeDescriptions[type] ?? "",
                            isSelected: selectedType == type
                        )
                        .onTapGesture {
                            withAnimation(.spring()) {
                                selectedType = type
                            }
                        }
                    }
                }
                .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)
                
                Image(systemName: iconName)
                    .font(.system(size: 24))
                    .foregroundColor(iconColor)
            }
            
            // 类型名称
            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)
        }
        .padding(.vertical, 12)
        .padding(.horizontal, 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 .welfare3D: 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 .welfare3D: return .green
        case .sevenStar: return .purple
        case .other: return .gray
        }
    }
    
    private var backgroundColor: Color {
        isSelected ? iconColor.opacity(0.1) : Color(.systemGray6)
    }
}

// 预览
struct LotteryTypePicker_Previews: PreviewProvider {
    static var previews: some View {
        StatefulPreviewWrapper(LotteryType.doubleColorBall) { binding in
            VStack {
                LotteryTypePicker(selectedType: binding)
                    .padding()
                
                Text("当前选择: \(binding.wrappedValue.rawValue)")
                    .padding()
            }
        }
    }
}

// 用于预览的状态包装器
struct StatefulPreviewWrapper<T, Content: View>: View where Content: View {
    @State var value: T
    let content: (Binding<T>) -> Content
    
    var body: some View {
        content($value)
    }
    
    init(_ value: T, @ViewBuilder content: @escaping (Binding<T>) -> Content) {
        self._value = State(initialValue: value)
        self.content = content
    }
}
