import 'package:flutter/material.dart';
import 'package:kmobile/data/models/user.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';

import '../../../l10n/app_localizations.dart';

class AllAccountsScreen extends StatefulWidget {
  final List<User> users;
  const AllAccountsScreen({super.key, required this.users});

  @override
  State<AllAccountsScreen> createState() => _AllAccountsScreenState();
}

class _AllAccountsScreenState extends State<AllAccountsScreen> {
  final Map<String, bool> _visibilityMap = {};

  String getFullAccountType(BuildContext context, String? accountType) {
    // This is duplicated from dashboard_screen.dart.
    // In a real app, this should be moved to a utility/helper class.
    if (accountType == null || accountType.isEmpty) return 'N/A';
    switch (accountType.toLowerCase()) {
      case 'sa':
        return AppLocalizations.of(context).savingsAccount;
      case 'sb':
        return AppLocalizations.of(context).savingsAccount;
      case 'ln':
        return AppLocalizations.of(context).loanAccount;
      case 'td':
        return AppLocalizations.of(context).termDeposit;
      case 'rd':
        return AppLocalizations.of(context).recurringDeposit;
      case 'ca':
        return "Current Account";
      case 'cc':
        return "Cash Credit Account";
      case 'od':
        return "Overdraft Account";
      default:
        return AppLocalizations.of(context).unknownAccount;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).viewall),
      ),
      body: Column(
        children: [
          const SizedBox(height: 16.0), // Added space below the app bar
          Expanded(
            child: ListView.builder(
              itemCount: widget.users.length,
              itemBuilder: (context, index) {
                final user = widget.users[index];
                return Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 16.0, vertical: 8.0),
                  child: _buildAccountCard(user),
                );
              },
            ),
          ), // Closing Expanded
        ], // Closing Column
      ),
    );
  }

  Widget _buildAccountCard(User user) {
    final theme = Theme.of(context);
    final accountNo = user.accountNo ?? '';
    final isVisible = _visibilityMap[accountNo] ?? false;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
      decoration: BoxDecoration(
        color: const Color(0xFF01A04C),
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Top section: Account Type and Number
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                getFullAccountType(context, user.accountType),
                style: TextStyle(
                  color: theme.colorScheme.onPrimary,
                  fontSize: 16,
                  fontWeight: FontWeight.w700,
                ),
              ),
              Text(
                user.accountNo ?? 'N/A',
                style: TextStyle(
                  color: theme.colorScheme.onPrimary,
                  fontSize: 14,
                  fontWeight: FontWeight.w700,
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          // Bottom section: Balance and Toggle
          Row(
            children: [
              Text(
                "₹ ",
                style: TextStyle(
                  color: theme.colorScheme.onPrimary,
                  fontSize: 32,
                  fontWeight: FontWeight.w700,
                ),
              ),
              Expanded(
                child: FittedBox(
                  fit: BoxFit.scaleDown,
                  alignment: Alignment.centerLeft,
                  child: Text(
                    isVisible ? user.currentBalance ?? '0.00' : '*****',
                    style: TextStyle(
                      color: theme.colorScheme.onPrimary,
                      fontSize: 32,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                ),
              ),
              const Spacer(),
              InkWell(
                onTap: () {
                  setState(() {
                    _visibilityMap[accountNo] = !isVisible;
                  });
                },
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Icon(
                    isVisible ? Symbols.visibility_lock : Symbols.visibility,
                    color: theme.scaffoldBackgroundColor,
                    weight: 800,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
