//
//  DoubleColorBallCompoents.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/25.
//

import SwiftUI

struct DoubleColorBallSection: View {
    @Binding var numbers: String
    
    @State private var redNumbers: [Int] = []
    @State private var blueNumbers: [Int] = []
    @State private var betCount = 1
    @State private var selectedType = LotteryType.doubleColorBall
    
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 头部标题
            SectionHeader(title: "选择号码", icon: "number.circle")
            
            // 红球选择区
            VStack(alignment: .leading, spacing: 12) {
                NumberSelectionGrid(
                    lotteryType: .doubleColorBall,
                    title: "红球区",
                    color: .red,
                    range: 1...33,
                    maxSelection: 6,
                    selectedNumbers: $redNumbers
                )
            }
            
            // 蓝球选择区
            VStack(alignment: .leading, spacing: 12) {
                NumberSelectionGrid(
                    lotteryType: .doubleColorBall,
                    title: "蓝球区",
                    color: .blue,
                    range: 1...16,
                    maxSelection: 1,
                    selectedNumbers: $blueNumbers
                )
            }
            
            // 快速操作栏
            QuickActionsBar2(
                maxSelection: betCount,
                count: redNumbers.count + blueNumbers.count,
                isEmpty: redNumbers.isEmpty && blueNumbers.isEmpty,
                onRandom: generateRandomNumbers,
                onClear: clearAllNumbers,
                onSmartRandom: generateRandomNumbers
            )
            
            
            // 红球/蓝球已选显示
            if (!redNumbers.isEmpty || !blueNumbers.isEmpty) {
                SelectedNumbersViewDoubleColorBall(
                    reds: redNumbers,
                    blues: blueNumbers,
                    maxCount: 7,
                    onClear: {
                        redNumbers.removeAll()
                        blueNumbers.removeAll()
                    }
                )
                
            }
        }
        .onChange(of: redNumbers) {
            updateNumbers()
        }
        .onChange(of: blueNumbers) {
            updateNumbers()
        }
    }
    
    // MARK: -方法
    private func generateRandomNumbers() {
        withAnimation(.spring(response: 0.4)) {
            redNumbers = Array(Set((1...33).shuffled().prefix(6))).sorted()
            blueNumbers = [Int.random(in: 1...16)]
        }
    }
    
    private func clearAllNumbers() {
        withAnimation(.spring(response: 0.3)) {
            redNumbers.removeAll()
            blueNumbers.removeAll()
        }
    }
    
    private func updateNumbers() {
        let redStr = redNumbers.sorted().map { String(format: "%02d", $0) }.joined(separator: " ")
        let blueStr = blueNumbers.sorted().map { String(format: "%02d", $0) }.joined(separator: " ")
        numbers = "\(redStr) + \(blueStr)"
    }
}

// MARK: - 头部组件
struct SectionHeader: View {
    let title: String
    let icon: String
    
    var body: some View {
        HStack {
            Label(title, systemImage: icon)
                .font(.headline)
                .foregroundColor(.primary)
            
            Spacer()
        }
    }
}


// MARK: - 已选号码显示
struct SelectedNumbersViewDoubleColorBall: View {
    let reds: [Int]
    let blues: [Int]
    let maxCount: Int
    let onClear: () -> Void
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack {
                // 标签
                Text("双色球")
                    .font(.subheadline)
                    .fontWeight(.semibold)
                    .foregroundColor(.primary)
                
            }
                
            HStack {
                // 号码显示
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 6) {
                        ForEach(reds.sorted(), id: \.self) { number in
                            Text(String(format: "%02d", number))
                                .font(.system(size: 16, weight: .bold, design: .rounded))
                                .frame(width: 36, height: 36)
                                .background(Color(.red).opacity(0.15))
                                .foregroundColor(Color(.red))
                                .clipShape(Circle())
                        }
                        
                        ForEach(blues.sorted(), id: \.self) { number in
                            Text(String(format: "%02d", number))
                                .font(.system(size: 16, weight: .bold, design: .rounded))
                                .frame(width: 36, height: 36)
                                .background(Color(.blue).opacity(0.15))
                                .foregroundColor(Color(.blue))
                                .clipShape(Circle())
                        }
                    }
                }
                
                Spacer()
                
                // 清空按钮
                if (!reds.isEmpty || !blues.isEmpty) {
                    Button(action: {
                        withAnimation {
                            onClear()
                        }
                    }) {
                        Image(systemName: "xmark.circle.fill")
                            .font(.system(size: 16))
                            .foregroundColor(.secondary)
                            .padding(4)
                    }
                }
            }
        }
        .padding(16)
        .background(Color(.systemBackground))
        .cornerRadius(12)
        .overlay(
            RoundedRectangle(cornerRadius: 12)
                .stroke(Color.blue.opacity(0.1), lineWidth: 1)
        )
    }
}

