//
//  HomeView.swift
//  AppClientBaseSwift
//
//  Super App style home screen - Pastel Yellow Theme
//  Màn hình home phong cách Super App - Theme Pastel Vàng
//

import SwiftUI

// MARK: - Home View
// View Home

/// Super App style home view with wallet, services, promos
/// View home phong cách Super App với ví, services, promos
struct HomeView: View {

    // MARK: - Properties

    /// Auth manager for user info
    /// Auth manager cho thông tin user
    @StateObject private var authManager = AuthManager.shared

    /// Mock balance
    /// Số dư mock
    private let balance: Double = 1_250_000

    // MARK: - Body

    var body: some View {
        NavigationStack {
            ScrollView(showsIndicators: false) {
                VStack(spacing: 24) {
                    // Header
                    headerSection
                        .padding(.horizontal, 20)

                    // Wallet Card
                    // Card Ví
                    WalletCard(balance: balance) { action in
                        handleQuickAction(action)
                    }
                    .padding(.horizontal, 20)

                    // Services Grid
                    // Grid Services
                    VStack(alignment: .leading, spacing: 16) {
                        Text("Dịch vụ")
                            .font(.headline)
                            .fontWeight(.semibold)
                            .foregroundStyle(Color.textPrimary)
                            .padding(.horizontal, 20)

                        ServiceGrid { service in
                            handleServiceTap(service)
                        }
                        .padding(.horizontal, 20)
                    }

                    // Promo Carousel
                    // Carousel Promo
                    VStack(alignment: .leading, spacing: 16) {
                        HStack {
                            Text("Ưu đãi hấp dẫn")
                                .font(.headline)
                                .fontWeight(.semibold)
                                .foregroundStyle(Color.textPrimary)

                            Spacer()

                            Button("Xem tất cả") {
                                // Show all promos
                            }
                            .font(.subheadline)
                            .foregroundStyle(Color.brandAccent)
                        }
                        .padding(.horizontal, 20)

                        PromoCarousel()
                            .padding(.horizontal, 16)
                    }

                    // Activity Feed
                    // Feed Hoạt động
                    ActivityFeed { activity in
                        handleActivityTap(activity)
                    }
                    .padding(.horizontal, 20)

                    // Bottom spacing
                    Spacer(minLength: 20)
                }
                .padding(.top, 10)
            }
            .background(Color.appBackground)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    HStack(spacing: 16) {
                        // QR Code
                        Button {
                            // Show QR scanner
                        } label: {
                            Image(systemName: "qrcode.viewfinder")
                                .font(.title3)
                                .foregroundStyle(Color.textPrimary)
                        }

                        // Notifications
                        Button {
                            // Show notifications
                        } label: {
                            Image(systemName: "bell.badge")
                                .font(.title3)
                                .foregroundStyle(Color.brandAccent)
                        }
                    }
                }
            }
        }
    }

    // MARK: - Subviews

    /// Header with user info
    /// Header với thông tin user
    private var headerSection: some View {
        HStack(spacing: 14) {
            // Avatar
            avatarView

            // Greeting
            VStack(alignment: .leading, spacing: 4) {
                Text(greetingText)
                    .font(.subheadline)
                    .foregroundStyle(Color.textSecondary)

                Text(authManager.currentUser?.name ?? "Người dùng")
                    .font(.title3)
                    .fontWeight(.bold)
                    .foregroundStyle(Color.textPrimary)
            }

            Spacer()
        }
    }

    /// User avatar view
    /// View avatar user
    private var avatarView: some View {
        Group {
            if let avatarUrl = authManager.currentUser?.avatarUrl,
               let url = URL(string: avatarUrl) {
                AsyncImage(url: url) { image in
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                } placeholder: {
                    avatarPlaceholder
                }
            } else {
                avatarPlaceholder
            }
        }
        .frame(width: 50, height: 50)
        .clipShape(Circle())
        .shadow(color: AppTheme.cardShadow, radius: 5, x: 0, y: 2)
    }

    /// Avatar placeholder
    /// Placeholder avatar
    private var avatarPlaceholder: some View {
        ZStack {
            Circle()
                .fill(AppTheme.primaryGradient)

            Text(authManager.currentUser?.initials ?? "U")
                .font(.headline)
                .fontWeight(.bold)
                .foregroundStyle(Color.textPrimary)
        }
    }

    /// Greeting text based on time of day
    /// Text lời chào dựa trên thời gian trong ngày
    private var greetingText: String {
        let hour = Calendar.current.component(.hour, from: Date())
        switch hour {
        case 5..<12:
            return "Chào buổi sáng"
        case 12..<18:
            return "Chào buổi chiều"
        default:
            return "Chào buổi tối"
        }
    }

    // MARK: - Methods

    /// Handle quick action tap
    /// Xử lý tap quick action
    private func handleQuickAction(_ action: WalletCard.QuickAction) {
        print("Quick action: \(action.rawValue)")
    }

    /// Handle service tap
    /// Xử lý tap service
    private func handleServiceTap(_ service: ServiceItem) {
        print("Service: \(service.title)")
    }

    /// Handle activity tap
    /// Xử lý tap activity
    private func handleActivityTap(_ activity: ActivityItem) {
        print("Activity: \(activity.title)")
    }
}

// MARK: - Preview

#Preview {
    HomeView()
}
