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

import SwiftUI

struct TicketInputView: View {
    @Binding var selectedType: LotteryType
    @Binding var numbers: String
    @Binding var betCount: Int
    @Binding var amountPerBet: Int
    
    @FocusState private var focusedField: Field?
    
    enum Field {
        case numbers
        case amount
    }
    
    let totalAmount: Float
    
    // 常用倍数选项
    private let betCountOptions = [1, 5, 10, 20]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            // 投注选择
            if (selectedType == .doubleColorBall) {
                DoubleColorBallSection(numbers: $numbers)
            } else if (selectedType == .superLotto) {
                SuperLottoSection(numbers: $numbers)
            } else if (selectedType == .happy8) {
                HappyEightSection(numbers: $numbers)
            } else if (selectedType == .sevenStar) {
                numbersInputSection
            } else {
                
            }
            
            // 注数选择
            betCountSection
            
            Divider()
            
//            // 总计显示 TODO 添加多注时用到
//            totalAmountSection
            
        }
        .padding(.vertical, 8)
    }
    
    
    // MARK: - 组件
    
    private var numbersInputSection: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("投注号码")
                .font(.headline)
            VStack {
                HStack {
                    Spacer()
                    
                    Button("随机生成") {
                        numbers = generateSampleNumbers(for: selectedType)
                    }
                    .font(.caption)
                    .foregroundColor(.blue)
                }
                
                VStack(alignment: .leading, spacing: 8) {
                    Text("请输入号码，用空格或逗号分隔")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    
                    TextEditor(text: $numbers)
                        .font(.system(.body, design: .monospaced))
                        .frame(minHeight: 80)
                        .padding(8)
                        .background(Color(.systemBackground))
                        .cornerRadius(8)
                        .overlay(
                            RoundedRectangle(cornerRadius: 8)
                                .stroke(focusedField == .numbers ? Color.blue : Color.gray.opacity(0.3), lineWidth: 1)
                        )
                        .focused($focusedField, equals: .numbers)
                    
                    Text("例如：01 05 12 23 28 33 + 09")
                        .font(.caption2)
                        .foregroundColor(.secondary)
                }
            }
            .padding()
            .background(Color(.systemBackground))
            .cornerRadius(12)
            .shadow(color: Color.black.opacity(0.05), radius: 5, x: 0, y: 2)
        }
    }
    
    private var totalAmountSection: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("总计")
                .font(.headline)
            
            HStack {
                VStack(alignment: .leading, spacing: 4) {
                    Text("\(betCount) 倍 × ¥\(Double(amountPerBet), specifier: "%.2f")/注")
                        .font(.subheadline)
                        .foregroundColor(.secondary)
                    
                    Text(Formatters.formatCurrency(totalAmount))
                        .font(.system(size: 32, weight: .bold))
                        .foregroundColor(.blue)
                }
                
                Spacer()
                
                Image(systemName: "creditcard.fill")
                    .font(.system(size: 36))
                    .foregroundColor(.green.opacity(0.7))
            }
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 12)
                    .fill(Color.blue.opacity(0.05))
                    .overlay(
                        RoundedRectangle(cornerRadius: 12)
                            .stroke(Color.blue.opacity(0.2), lineWidth: 1)
                    )
            )
        }
    }
    
    private var betCountSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            SectionHeader(title: "投注设置", icon: "banknote")
            
            // 金额显示卡片
            VStack(spacing: 12) {
                HStack {
                    VStack(alignment: .leading, spacing: 4) {
                        Text("投注金额")
                            .font(.caption)
                            .foregroundColor(.secondary)
                        
                        Text(Formatters.formatCurrency(totalAmount))
                            .font(.system(size: 28, weight: .bold, design: .rounded))
                            .foregroundColor(.blue)
                    }
                    
                    Spacer()
                    
                    VStack(alignment: .trailing, spacing: 4) {
                        Text("倍数")
                            .font(.caption)
                            .foregroundColor(.secondary)
                        
                        Text("\(betCount)倍")
                            .font(.system(size: 20, weight: .semibold, design: .rounded))
                            .foregroundColor(.primary)
                    }
                }
                
                // 滑块选择器
                VStack(spacing: 8) {
                    HStack {
                        Text("1倍")
                            .font(.caption)
                            .foregroundColor(.secondary)
                        
                        Slider(
                            value: Binding(
                                get: { Double(betCount) },
                                set: { betCount = Int($0) }
                            ),
                            in: 1...50,
                            step: 1
                        )
                        .accentColor(.blue)
                        
                        Text("50倍")
                            .font(.caption)
                            .foregroundColor(.secondary)
                    }
                    
                    // 快速倍数
                    ScrollView(.horizontal, showsIndicators: false) {
                        HStack(spacing: 10) {
                            ForEach(betCountOptions, id: \.self) { count in
                                ChipButton(
                                    title: "\(count)倍",
                                    isSelected: betCount == count,
                                    action: { withAnimation { betCount = count } }
                                )
                            }
                        }
                        .padding(.horizontal, 2)
                    }
                }
            }
            .padding(20)
            .background(
                LinearGradient(
                    colors: [
                        Color.blue.opacity(0.03),
                        Color.blue.opacity(0.01)
                    ],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
            )
            .cornerRadius(16)
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                    .stroke(Color.blue.opacity(0.1), lineWidth: 1)
            )
        }
    }
    
    
    // MARK: - 方法
    
    // 生成示例号码
    private func generateSampleNumbers(for type: LotteryType) -> String {
        switch type {
        case .doubleColorBall:
            let redBalls = (1...33).shuffled().prefix(6).map { String(format: "%02d", $0) }
            let blueBall = String(format: "%02d", Int.random(in: 1...16))
            return "\(redBalls.joined(separator: " ")) + \(blueBall)"
            
        case .superLotto:
            let frontBalls = (1...35).shuffled().prefix(5).map { String(format: "%02d", $0) }
            let backBalls = (1...12).shuffled().prefix(2).map { String(format: "%02d", $0) }
            return "\(frontBalls.joined(separator: " ")) + \(backBalls.joined(separator: " "))"
            
        case .happy8:
            let numbers = (0...9).shuffled().prefix(3).map { String($0) }
            return numbers.joined(separator: " ")
            
        case .sevenStar:
            let numbers = (0...9).shuffled().prefix(7).map { String($0) }
            return numbers.joined(separator: " ")
            
        case .other:
            return "随机号码"
        }
    }
    
}
