//
//  AvatarCard.swift
//  lichunWebsocket
//
//  Large avatar display with mood - tappable to show player profile
//

import SwiftUI

struct AvatarCard: View {
    @EnvironmentObject var webSocketService: WebSocketService

    @State private var isFloating = false
    @State private var isBreathing = false
    @State private var showProfile = false

    var body: some View {
        BaseCard {
            VStack(spacing: AppSpacing.sm) {
                // Avatar
                CharacterAvatar(
                    imageURL: webSocketService.person.image,
                    name: webSocketService.person.firstname,
                    size: AppSpacing.avatarSizeLarge,
                    showBorder: true,
                    animateBorder: true
                )
                .scaleEffect(isBreathing ? 1.05 : 1.0)
                .offset(y: isFloating ? -5 : 0)

                // Character name
                Text("\(webSocketService.person.firstname) \(webSocketService.person.lastname)")
                    .font(.appBodyBold)
                    .foregroundColor(AppColors.primaryText)
                    .lineLimit(1)

                // Mood
                if !webSocketService.person.mood.isEmpty {
                    Text(webSocketService.person.mood)
                        .font(.appCaption)
                        .foregroundColor(AppColors.secondaryText)
                        .multilineTextAlignment(.center)
                        .lineLimit(2)
                }
            }
            .frame(maxWidth: .infinity)
        }
        .onTapGesture {
            hapticFeedback(style: .light)
            showProfile = true
        }
        .sheet(isPresented: $showProfile) {
            PlayerProfileSheet()
                .environmentObject(webSocketService)
        }
        .onAppear {
            // Floating animation
            withAnimation(
                .easeInOut(duration: 2.5)
                .repeatForever(autoreverses: true)
            ) {
                isFloating = true
            }

            // Breathing animation (scale + glow)
            withAnimation(
                .easeInOut(duration: 3.0)
                .repeatForever(autoreverses: true)
            ) {
                isBreathing = true
            }
        }
    }
}

// MARK: - Home Character Card (T014)

/// One glanceable identity block for the Home screen: avatar + name + mood on
/// the left, the four core stat bars on the right. Replaces the previous trio
/// of competing cards (AvatarCard + QuickStatsCard + the header mood) so the
/// first screen reads as a single character at a glance. Tappable to open the
/// full player profile.
struct HomeCharacterCard: View {
    @EnvironmentObject var webSocketService: WebSocketService

    @State private var isFloating = false
    @State private var isBreathing = false
    @State private var showProfile = false

    private var person: Person { webSocketService.person }

    var body: some View {
        BaseCard {
            HStack(alignment: .center, spacing: AppSpacing.md) {
                // Left: avatar + name + mood
                VStack(spacing: AppSpacing.xs) {
                    CharacterAvatar(
                        imageURL: person.image,
                        name: person.firstname,
                        size: AppSpacing.avatarSizeMedium,
                        showBorder: true,
                        animateBorder: true
                    )
                    .scaleEffect(isBreathing ? 1.05 : 1.0)
                    .offset(y: isFloating ? -4 : 0)

                    Text("\(person.firstname) \(person.lastname)")
                        .font(.appBodyBold)
                        .foregroundColor(AppColors.primaryText)
                        .lineLimit(1)
                        .minimumScaleFactor(0.7)

                    if !person.mood.isEmpty {
                        Text(person.mood)
                            .font(.appCaption)
                            .foregroundColor(AppColors.secondaryText)
                            .multilineTextAlignment(.center)
                            .lineLimit(2)
                    }
                }
                .frame(width: 116)

                // Right: core stats
                VStack(spacing: AppSpacing.sm) {
                    CozyStatBar(
                        label: "Health",
                        value: person.health,
                        color: AppColors.health,
                        height: 10
                    )
                    CozyStatBar(
                        label: "Happiness",
                        value: person.happiness,
                        color: AppColors.happiness,
                        height: 10
                    )
                    CozyStatBar(
                        label: "Intelligence",
                        value: person.intelligence,
                        color: AppColors.intelligence,
                        height: 10
                    )
                    CozyStatBar(
                        label: "Prestige",
                        value: person.prestige,
                        color: AppColors.prestige,
                        height: 10
                    )
                }
                .frame(maxWidth: .infinity)
            }
        }
        .contentShape(Rectangle())
        .onTapGesture {
            hapticFeedback(style: .light)
            showProfile = true
        }
        .sheet(isPresented: $showProfile) {
            PlayerProfileSheet()
                .environmentObject(webSocketService)
        }
        .onAppear {
            withAnimation(.easeInOut(duration: 2.5).repeatForever(autoreverses: true)) {
                isFloating = true
            }
            withAnimation(.easeInOut(duration: 3.0).repeatForever(autoreverses: true)) {
                isBreathing = true
            }
        }
    }
}

// MARK: - Preview
#Preview {
    let service = WebSocketService(urlSession: URLSession.shared, delegateQueue: OperationQueue())
    AvatarCard()
        .environmentObject(service)
        .padding()
        .background(AppColors.background)
}
