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

import SwiftUI

struct SuperLottoSection: View {
    @Binding var numbers: String
    
    @State private var frontNumbers: [Int] = []  // 前区号码
    @State private var backNumbers: [Int] = []   // 后区号码
    @State private var betCount = 1
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 头部标题
            SectionHeader(title: "选择号码", icon: "number.circle")
            
            // 前区选择（红球）
            VStack(alignment: .leading, spacing: 12) {
                NumberSelectionGrid(
                    lotteryType: .superLotto,
                    title: "前区",
                    color: .orange,  // 使用橙色区分双色球的红色
                    range: 1...35,
                    maxSelection: 5,
                    selectedNumbers: $frontNumbers
                )
            }
            
            // 后区选择（蓝球）
            VStack(alignment: .leading, spacing: 12) {
                NumberSelectionGrid(
                    lotteryType: .superLotto,
                    title: "后区",
                    color: .blue,
                    range: 1...12,
                    maxSelection: 2,
                    selectedNumbers: $backNumbers
                )
            }
            
            // 快速操作栏
            QuickActionsBar2(
                maxSelection: betCount,
                count: frontNumbers.count + backNumbers.count,
                isEmpty: frontNumbers.isEmpty && backNumbers.isEmpty,
                onRandom: generateRandomNumbers,
                onClear: clearAllNumbers,
                onSmartRandom: generateRandomNumbers
            )
            
            // 已选号码显示
            if !frontNumbers.isEmpty || !backNumbers.isEmpty {
                SelectedNumbersViewSuperLotto(
                    fronts: frontNumbers,
                    backs: backNumbers,
                    onClear: {
                        frontNumbers.removeAll()
                        backNumbers.removeAll()
                    }
                )
            }
        }
        .onChange(of: frontNumbers) {
            updateNumbers()
        }
        .onChange(of: backNumbers) {
            updateNumbers()
        }
    }
    
    // MARK: - 方法
    
    private func generateRandomNumbers() {
        withAnimation(.spring(response: 0.4)) {
            // 前区：从1-35中随机选择5个不重复的号码
            frontNumbers = Array(Set((1...35).shuffled().prefix(5))).sorted()
            
            // 后区：从1-12中随机选择2个不重复的号码
            backNumbers = Array(Set((1...12).shuffled().prefix(2))).sorted()
        }
    }
    
    private func clearAllNumbers() {
        withAnimation(.spring(response: 0.3)) {
            frontNumbers.removeAll()
            backNumbers.removeAll()
        }
    }
    
    private func updateNumbers() {
        let frontStr = frontNumbers.sorted().map { String(format: "%02d", $0) }.joined(separator: " ")
        let backStr = backNumbers.sorted().map { String(format: "%02d", $0) }.joined(separator: " ")
        numbers = "\(frontStr) + \(backStr)"
    }
    
}

// MARK: - 大乐透专用已选号码视图
struct SelectedNumbersViewSuperLotto: View {
    let fronts: [Int]
    let backs: [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(fronts.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.orange.opacity(0.15))
                                .foregroundColor(Color.orange)
                                .clipShape(Circle())
                        }
                        
                        // 后区号码
                        ForEach(backs.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 !fronts.isEmpty || !backs.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)
        )
    }
}
