//
//  NumberSelectionGrid.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/26.
//

import SwiftUI

// MARK: - 数字选择网格 (双色球/大乐透/快乐八)
struct NumberSelectionGrid: View {
    let lotteryType: LotteryType
    let title: String
    let color: Color
    let range: ClosedRange<Int>
    let maxSelection: Int
    @Binding var selectedNumbers: [Int]
    
    private let columns = Array(repeating: GridItem(.flexible()), count: 10)
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            // 标题和计数
            HStack {
                Text(title)
                    .font(.subheadline)
                    .fontWeight(.semibold)
                    .foregroundColor(color)
                
                Spacer()
                
                Text("\(selectedNumbers.count)/\(maxSelection)")
                    .font(.caption)
                    .fontWeight(.medium)
                    .foregroundColor(.secondary)
                    .padding(.horizontal, 8)
                    .padding(.vertical, 2)
                    .background(
                        Capsule()
                            .fill(color.opacity(0.1))
                    )
            }
            .padding(.horizontal, 4)
            
            // 号码网格
            LazyVGrid(columns: columns, spacing: 10) {
                ForEach(range, id: \.self) { number in
                    NumberButton(
                        lotteryType: lotteryType,
                        number: number,
                        color: color,
                        isSelected: selectedNumbers.contains(number),
                        isSelectable: selectedNumbers.count < maxSelection || selectedNumbers.contains(number)
                    ) {
                        toggleNumber(number)
                    }
                }
            }
        }
        .padding(16)
        .background(Color(.secondarySystemBackground))
        .cornerRadius(12)
        .overlay(
            RoundedRectangle(cornerRadius: 12)
                .stroke(.primary.opacity(0.2), lineWidth: 1)
        )
    }
    
    private func toggleNumber(_ number: Int) {
        withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) {
            if selectedNumbers.contains(number) {
                selectedNumbers.removeAll { $0 == number }
            } else if selectedNumbers.count < maxSelection {
                selectedNumbers.append(number)
                // 触觉反馈
                let generator = UIImpactFeedbackGenerator(style: .light)
                generator.impactOccurred()
//            } else {
//                // 已达上限提示
//                let generator = UINotificationFeedbackGenerator()
//                generator.notificationOccurred(.warning)
            }
        }
    }
}


// MARK: - 号码按钮
struct NumberButton: View {
    let lotteryType: LotteryType
    let number: Int
    let color: Color
    let isSelected: Bool
    let isSelectable: Bool
    let action: () -> Void
    
    @State private var isPressed = false
    
    var body: some View {
        Button(action: action) {
            ZStack {
                // 背景
                Circle()
                    .fill(backgroundColor)
                    .frame(width: 32, height: 32)
                    .shadow(color: isSelected ? Color.purple.opacity(0.3) : Color.clear,
                           radius: isSelected ? 3 : 0,
                           x: 0, y: isSelected ? 1 : 0)
                
                // 数字
                Text(String(format: "%02d", number))
                    .font(.system(size: 16, weight: isSelected ? .bold : .semibold, design: .rounded))
                    .foregroundColor(textColor)
                
                // 选中标记
                if isSelected {
                    Circle()
                        .stroke(borderColor, lineWidth: 2)
                        .frame(width: 32, height: 32)
                    
                    Image(systemName: "checkmark.circle.fill")
                        .font(.system(size: 10, weight: .black))
                        .foregroundColor(.white)
                        .frame(width: 12, height: 12)
                        .background(borderColor)
                        .clipShape(Circle())
                        .offset(x: 12, y: -12)
                }
            }
        }
        .buttonStyle(PressableButtonStyle())
        .scaleEffect(isSelected ? 0.94 : 1.0)
        .opacity(isSelectable || isSelected ? 1.0 : 0.5)
        .disabled(!isSelectable && !isSelected)
        .animation(.spring(response: 0.2), value: isSelected)
    }
    
    private var backgroundColor: Color {
        isSelected ? borderColor.opacity(0.15) : numberColor.opacity(0.1)
    }
    
    private var textColor: Color {
        isSelected ? borderColor : numberColor
    }
    
    private var borderColor: Color {
        numberColor
    }
    
    private var numberColor: Color {
        if (lotteryType == .doubleColorBall || lotteryType == .superLotto) {
            return color
        } else {
            // 根据数字范围分配颜色
            switch number {
            case 1...20: return .blue
            case 21...40: return .green
            case 41...60: return .orange
            case 61...80: return .purple
            default: return .gray
            }
        }
    }
}

// MARK: - 按压球样式
struct PressableButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .scaleEffect(configuration.isPressed ? 0.95 : 1.0)
            .opacity(configuration.isPressed ? 0.8 : 1.0)
            .animation(.easeInOut(duration: 0.15), value: configuration.isPressed)
    }
}

// MARK: - 快速倍数按钮
struct ChipButton: View {
    let title: String
    let isSelected: Bool
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            Text(title)
                .font(.system(size: 14, weight: .medium))
                .padding(.horizontal, 16)
                .padding(.vertical, 8)
                .background(
                    Capsule()
                        .fill(isSelected ? Color.blue : Color.gray.opacity(0.1))
                )
                .foregroundColor(isSelected ? .white : .primary)
                .overlay(
                    Capsule()
                        .stroke(isSelected ? Color.blue : Color.gray.opacity(0.3), lineWidth: 1)
                )
        }
    }
}

// MARK: -已选号码展示区



// MARK: - 快速操作栏
struct QuickActionsBar: View {
    let onRandom: () -> Void
    let onClear: () -> Void
    
    var body: some View {
        HStack(spacing: 12) {
            // 随机生成
            ActionButton(
                title: "随机一注",
                icon: "dice.fill",
                color: .purple,
                action: onRandom
            )
            
            // 清空全部
            ActionButton(
                title: "清空重选",
                icon: "trash",
                color: .gray,
                action: onClear
            )
        }
    }
}

struct QuickActionsBar2: View {
    let maxSelection: Int
    let count: Int
    let isEmpty: Bool
    let onRandom: () -> Void
    let onClear: () -> Void
    let onSmartRandom: () -> Void
    
    var body: some View {
        HStack(spacing: 12) {
            // 随机生成
            ActionButton(
                title: "随机\(maxSelection)注",
                icon: "dice.fill",
                color: .purple,
                action: onRandom
            )
            
            // 清空
            ActionButton(
                title: isEmpty ? "清空" : "清空(\(count))",
                icon: "trash",
                color: isEmpty ? .gray : .red,
                action: onClear
            )
            
            // 智能选号
            ActionButton(
                title: "智能",
                icon: "brain.head.profile",
                color: .orange,
                action: onSmartRandom
            )
        }
        .padding(.vertical, 4)
    }
}

struct ActionButton: View {
    let title: String
    let icon: String
    let color: Color
    let action: () -> Void
    
    var body: some View {
        Button(action: {
            action()
            let generator = UIImpactFeedbackGenerator(style: .light)
            generator.impactOccurred()
        }) {
            HStack(spacing: 6) {
                Image(systemName: icon)
                    .font(.system(size: 14))
                
                Text(title)
                    .font(.caption)
                    .fontWeight(.medium)
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 10)
            .frame(maxWidth: .infinity)
            .background(color.opacity(0.1))
            .foregroundColor(color)
            .cornerRadius(10)
        }
    }
}

