//
//  PlayerProfileSheet.swift
//  lichunWebsocket
//
//  Character profile popup showing detailed player info
//

import SwiftUI

struct PlayerProfileSheet: View {
    @EnvironmentObject var webSocketService: WebSocketService
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: AppSpacing.lg) {
                    // Avatar header
                    avatarHeader

                    // Info grid
                    infoSection

                    // Stats section
                    statsSection

                    // Education section
                    educationSection
                }
                .padding(AppSpacing.md)
                .padding(.bottom, AppSpacing.xl)
            }
            .background(
                LinearGradient(
                    colors: [AppColors.background, AppColors.surfaceSubtle],
                    startPoint: .top,
                    endPoint: .bottom
                )
            )
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    Text("Profile")
                        .font(.appHeadline)
                        .foregroundColor(AppColors.primaryText)
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: { dismiss() }) {
                        Image(systemName: "xmark.circle.fill")
                            .font(.system(size: 24))
                            .foregroundColor(AppColors.secondaryText.opacity(0.6))
                    }
                }
            }
        }
    }

    // MARK: - Avatar Header

    private var avatarHeader: some View {
        VStack(spacing: AppSpacing.md) {
            CharacterAvatar(
                imageURL: webSocketService.person.image,
                name: webSocketService.person.firstname,
                size: 120,
                showBorder: true,
                animateBorder: true
            )

            Text("\(webSocketService.person.firstname) \(webSocketService.person.lastname)")
                .font(.appLargeTitle)
                .foregroundColor(AppColors.primaryText)

            if !webSocketService.person.mood.isEmpty {
                Text(webSocketService.person.mood)
                    .font(.appBody)
                    .foregroundColor(AppColors.secondaryText)
                    .padding(.horizontal, 16)
                    .padding(.vertical, 6)
                    .background(AppColors.surfaceSubtle)
                    .cornerRadius(AppSpacing.pillCornerRadius)
            }
        }
        .padding(.top, AppSpacing.md)
    }

    // MARK: - Info Section

    private var infoSection: some View {
        BaseCard {
            VStack(spacing: 0) {
                infoRow(icon: "calendar", label: "Age", value: "\(webSocketService.person.ageYears) years old")
                Divider().padding(.horizontal, AppSpacing.md)

                if !webSocketService.person.birthday.isEmpty {
                    infoRow(icon: "gift.fill", label: "Birthday", value: webSocketService.person.birthday)
                    Divider().padding(.horizontal, AppSpacing.md)
                }

                infoRow(icon: "person.fill", label: "Sex", value: webSocketService.person.sex.isEmpty ? "Unknown" : webSocketService.person.sex)
                Divider().padding(.horizontal, AppSpacing.md)

                if !webSocketService.person.occupation.isEmpty {
                    infoRow(icon: "briefcase.fill", label: "Occupation", value: webSocketService.person.occupation.capitalized)
                    Divider().padding(.horizontal, AppSpacing.md)
                }

                if !webSocketService.person.education.isEmpty {
                    infoRow(icon: "graduationcap.fill", label: "Education", value: webSocketService.person.education.capitalized)
                    Divider().padding(.horizontal, AppSpacing.md)
                }

                if !webSocketService.person.location.isEmpty {
                    infoRow(icon: "mappin.circle.fill", label: "Location", value: webSocketService.person.location)
                }
            }
        }
    }

    // MARK: - Stats Section

    private var statsSection: some View {
        BaseCard {
            VStack(spacing: AppSpacing.md) {
                HStack {
                    Image(systemName: "chart.bar.fill")
                        .foregroundColor(AppColors.accent)
                    Text("Stats")
                        .font(.appHeadline)
                        .foregroundColor(AppColors.primaryText)
                    Spacer()
                }

                // Resources row
                HStack(spacing: AppSpacing.lg) {
                    resourcePill(icon: "bolt.fill", value: "\(webSocketService.person.calcEnergy)", color: AppColors.energy)
                    resourcePill(icon: "dollarsign.circle.fill", value: "\(webSocketService.person.money)", color: AppColors.money)
                    resourcePill(icon: "diamond.fill", value: "\(webSocketService.person.diamonds)", color: AppColors.diamond)
                }

                CozyStatBar(label: "Health", value: Int(webSocketService.person.health * 100), color: AppColors.health, height: 12)
                CozyStatBar(label: "Happiness", value: webSocketService.person.happiness, color: AppColors.happiness, height: 12)
                CozyStatBar(label: "Intelligence", value: webSocketService.person.intelligence, color: AppColors.intelligence, height: 12)
                CozyStatBar(label: "Prestige", value: webSocketService.person.prestige, color: AppColors.prestige, height: 12)
            }
        }
    }

    // MARK: - Education Section

    private var educationSection: some View {
        Group {
            if let currentEd = webSocketService.person.currentEducation {
                BaseCard {
                    VStack(spacing: AppSpacing.md) {
                        HStack {
                            Image(systemName: "graduationcap.fill")
                                .foregroundColor(AppColors.intelligence)
                            Text("Current Education")
                                .font(.appHeadline)
                                .foregroundColor(AppColors.primaryText)
                            Spacer()
                        }

                        HStack {
                            Text("Level")
                                .font(.appBody)
                                .foregroundColor(AppColors.secondaryText)
                            Spacer()
                            Text(currentEd.educationLevel.capitalized)
                                .font(.appBodyBold)
                                .foregroundColor(AppColors.primaryText)
                        }

                        HStack {
                            Text("GPA")
                                .font(.appBody)
                                .foregroundColor(AppColors.secondaryText)
                            Spacer()
                            let gpa = GPpercentToGPA(GPPercent: Int(currentEd.GPA))
                            Text(String(format: "%.1f", gpa))
                                .font(.appBodyBold)
                                .foregroundColor(AppColors.primaryText)
                        }

                        HStack {
                            Text("Focus")
                                .font(.appBody)
                                .foregroundColor(AppColors.secondaryText)
                            Spacer()
                            Text(currentEd.focus)
                                .font(.appBodyBold)
                                .foregroundColor(AppColors.primaryText)
                        }

                        if let major = currentEd.major {
                            HStack {
                                Text("Major")
                                    .font(.appBody)
                                    .foregroundColor(AppColors.secondaryText)
                                Spacer()
                                Text(major)
                                    .font(.appBodyBold)
                                    .foregroundColor(AppColors.primaryText)
                            }
                        }
                    }
                }
            }
        }
    }

    // MARK: - Helpers

    private func infoRow(icon: String, label: String, value: String) -> some View {
        HStack(spacing: AppSpacing.md) {
            Image(systemName: icon)
                .font(.system(size: 16))
                .foregroundColor(AppColors.accent)
                .frame(width: 24)

            Text(label)
                .font(.appBody)
                .foregroundColor(AppColors.secondaryText)

            Spacer()

            Text(value)
                .font(.appBodyBold)
                .foregroundColor(AppColors.primaryText)
                .lineLimit(1)
        }
        .padding(.horizontal, AppSpacing.md)
        .padding(.vertical, AppSpacing.sm)
    }

    private func resourcePill(icon: String, value: String, color: Color) -> some View {
        HStack(spacing: 4) {
            Image(systemName: icon)
                .font(.system(size: 12))
                .foregroundColor(color)
            Text(value)
                .font(.appCaptionBold)
                .foregroundColor(AppColors.primaryText)
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 6)
        .background(color.opacity(0.1))
        .cornerRadius(AppSpacing.pillCornerRadius)
    }
}
