import SwiftUI
import AgentPetCore

/// Renders an imported spritesheet pet: cycles its frames and applies the same
/// mood motion and overlays as the built-in pets. Frame rate varies by mood
/// (faster while working) since image packs carry no per-mood data.
struct ImageSpriteView: View {
    /// Frames of the clip bound to the current mood (resolved by the caller).
    let frames: [NSImage]
    let mood: PetMood
    var size: CGFloat = 110

    var body: some View {
        TimelineView(.animation(minimumInterval: 1.0 / 12.0)) { context in
            let t = context.date.timeIntervalSinceReferenceDate
            let m = PetMotion.resolve(mood, t)

            ZStack {
                MoodAccessories(mood: mood, t: t, size: size)

                frameImage(at: t)
                    .rotationEffect(.degrees(m.rotation), anchor: .bottom)
                    .scaleEffect(x: m.scaleX, y: m.scaleY, anchor: .bottom)
                    .offset(y: m.offsetY)
            }
            .frame(width: size, height: size)
        }
    }

    @ViewBuilder private func frameImage(at t: Double) -> some View {
        if frames.isEmpty {
            Image(systemName: "pawprint.fill").font(.system(size: 36))
        } else {
            let index = Int(t * fps) % frames.count
            Image(nsImage: frames[index])
                .resizable()
                .interpolation(.high)
                .scaledToFit()
                .frame(width: size, height: size)
        }
    }

    private var fps: Double {
        switch mood {
        case .working, .celebrate: return 8
        case .waiting: return 4
        default: return 3
        }
    }
}
