//
//  WalletCard.swift
//  AppClientBaseSwift
//
//  Wallet card with balance and quick actions - Pastel Yellow Theme
//  Card ví với số dư và quick actions - Theme Pastel Vàng
//

import SwiftUI

// MARK: - Wallet Card
// Card Ví

/// Wallet card displaying balance and quick actions
/// Card ví hiển thị số dư và quick actions
struct WalletCard: View {

    // MARK: - Properties

    /// Current balance
    /// Số dư hiện tại
    let balance: Double

    /// Whether balance is hidden
    /// Số dư có bị ẩn không
    @State private var isBalanceHidden = false

    /// Quick action callback
    /// Callback quick action
    var onQuickAction: ((QuickAction) -> Void)?

    // MARK: - Quick Actions Enum

    enum QuickAction: String, CaseIterable {
        case topUp = "Nạp tiền"
        case transfer = "Chuyển tiền"
        case payment = "Thanh toán"

        var icon: String {
            switch self {
            case .topUp: return "plus.circle.fill"
            case .transfer: return "arrow.left.arrow.right.circle.fill"
            case .payment: return "qrcode"
            }
        }
    }

    // MARK: - Body

    var body: some View {
        VStack(spacing: 20) {
            // Balance section
            // Phần số dư
            balanceSection

            // Quick actions
            // Quick actions
            quickActionsSection
        }
        .padding(20)
        .background(
            ZStack {
                // Main gradient
                AppTheme.warmGradient

                // Decorative circles
                Circle()
                    .fill(Color.white.opacity(0.1))
                    .frame(width: 150, height: 150)
                    .offset(x: -80, y: -60)

                Circle()
                    .fill(Color.white.opacity(0.08))
                    .frame(width: 100, height: 100)
                    .offset(x: 120, y: 40)
            }
        )
        .cornerRadius(20)
        .shadow(color: AppTheme.buttonShadow, radius: 20, x: 0, y: 12)
    }

    // MARK: - Subviews

    /// Balance display section
    /// Phần hiển thị số dư
    private var balanceSection: some View {
        HStack {
            VStack(alignment: .leading, spacing: 6) {
                Text("Số dư khả dụng")
                    .font(.subheadline)
                    .foregroundStyle(Color.textPrimary.opacity(0.7))

                HStack(spacing: 8) {
                    if isBalanceHidden {
                        Text("••••••••")
                            .font(.system(size: 28, weight: .bold, design: .rounded))
                            .foregroundStyle(Color.textPrimary)
                    } else {
                        Text(String.formatVND(balance))
                            .font(.system(size: 28, weight: .bold, design: .rounded))
                            .foregroundStyle(Color.textPrimary)
                    }

                    Button {
                        withAnimation(.easeInOut(duration: 0.2)) {
                            isBalanceHidden.toggle()
                        }
                    } label: {
                        Image(systemName: isBalanceHidden ? "eye.slash.fill" : "eye.fill")
                            .font(.subheadline)
                            .foregroundStyle(Color.textPrimary.opacity(0.6))
                    }
                }
            }

            Spacer()

            // Points badge
            // Badge điểm
            VStack(alignment: .trailing, spacing: 4) {
                HStack(spacing: 4) {
                    Image(systemName: "star.circle.fill")
                        .foregroundStyle(Color.brandAccent)
                    Text("1,250")
                        .fontWeight(.semibold)
                }
                .font(.subheadline)
                .foregroundStyle(Color.textPrimary)

                Text("điểm thưởng")
                    .font(.caption)
                    .foregroundStyle(Color.textPrimary.opacity(0.6))
            }
        }
    }

    /// Quick actions buttons section
    /// Phần buttons quick actions
    private var quickActionsSection: some View {
        HStack(spacing: 0) {
            ForEach(QuickAction.allCases, id: \.self) { action in
                Button {
                    onQuickAction?(action)
                } label: {
                    VStack(spacing: 8) {
                        ZStack {
                            Circle()
                                .fill(Color.white.opacity(0.4))
                                .frame(width: 48, height: 48)

                            Image(systemName: action.icon)
                                .font(.title2)
                                .foregroundStyle(Color.textPrimary)
                        }

                        Text(action.rawValue)
                            .font(.caption)
                            .fontWeight(.medium)
                            .foregroundStyle(Color.textPrimary)
                    }
                }
                .frame(maxWidth: .infinity)
            }
        }
    }
}

// MARK: - Preview

#Preview {
    ZStack {
        Color.appBackground.ignoresSafeArea()
        WalletCard(balance: 1_250_000)
            .padding()
    }
}
