import SwiftUI

struct CreditsDisplay: View {
    @ObservedObject private var creditsManager = CreditsManager.shared
    @State private var displayCount: Int = 0
    @State private var isAnimating: Bool = false
    @State private var animationOffset: CGFloat = 0
    
    init() {
        self._displayCount = State(initialValue: 1000)
    }
    
    var body: some View {
        ZStack {
            // Main credits text
            Text("\(displayCount) credits")
                .font(Constants.Typography.xSmallRegular)
                .foregroundColor(textColor)
                .animation(.easeInOut(duration: 0.3), value: isAnimating)
                .animation(.easeOut(duration: 0.6), value: animationOffset)
        }
        .onChange(of: creditsManager.creditCount) { newValue in
            animateCreditsChange(to: newValue)
        }
        .onAppear {
            displayCount = creditsManager.creditCount
        }
    }
    
    private var textColor: Color {
        if isAnimating {
            return Constants.Colors.Accent.brand
        } else if displayCount <= 0 {
            return Constants.Colors.Accent.error
        } else {
            return Constants.Colors.Background.Fog.dense
        }
    }
    
    private func animateCreditsChange(to newValue: Int) {
        guard newValue != displayCount else { return }
        
        // Start animation
        isAnimating = true
        
        // Create dropping animation
        withAnimation(.easeOut(duration: 0.4)) {
            animationOffset = 10 // Drop down effect
        }
        
        // Animate the number counting down/up
        let startValue = displayCount
        let difference = newValue - startValue
        let steps = 20 // Number of animation steps
        let stepValue = Double(difference) / Double(steps)
        
        for i in 1...steps {
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(i) * 0.02) {
                displayCount = startValue + Int(stepValue * Double(i))
            }
        }
        
        // Reset animation state after completion
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
            withAnimation(.easeInOut(duration: 0.2)) {
                isAnimating = false
                animationOffset = 0
            }
        }
    }
    
    private func formatCredits(_ count: Int) -> String {
        if count >= 1000 {
            let thousands = Double(count) / 1000.0
            if thousands.truncatingRemainder(dividingBy: 1) == 0 {
                return String(format: "%.0f", thousands) + "K"
            } else {
                return String(format: "%.1f", thousands) + "K"
            }
        } else {
            return "\(count)"
        }
    }
}

#Preview {
    VStack(spacing: 16) {
        CreditsDisplay()
        
        Button("Deduct 50 Credits") {
            CreditsManager.shared.deductCredits(50)
        }
        
        Button("Add 100 Credits") {
            CreditsManager.shared.addCredits(100)
        }
        
        Button("Reset to 200") {
            CreditsManager.shared.creditCount = 200
        }
        
        Button("Set to 0 (Danger)") {
            CreditsManager.shared.creditCount = 0
        }
    }
    .padding()
    .background(Constants.Colors.Background.primary)
}
