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

class AccountInfoScreen extends StatefulWidget {
  final List<User> users;
  final int selectedIndex;

  const AccountInfoScreen({
    super.key,
    required this.users,
    required this.selectedIndex,
  });

  @override
  State<AccountInfoScreen> createState() => _AccountInfoScreen();
}

class _AccountInfoScreen extends State<AccountInfoScreen> {
  late User selectedUser;

  @override
  void initState() {
    super.initState();

    selectedUser = widget.users[widget.selectedIndex];
  }

  String getFullAccountType(String? accountType) {
    if (accountType == null || accountType.isEmpty) return 'N/A';
    // Convert to title case
    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) {
    final users = widget.users;

    int selectedIndex = widget.selectedIndex;

    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context)
            .accountInfo
            .replaceFirst(RegExp('\n'), '')),
      ),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                Card(
                  elevation: 4,
                  margin: const EdgeInsets.symmetric(vertical: 8.0),
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          AppLocalizations.of(context).accountNumber,
                          style: const TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 18),
                        ),
                        DropdownButton<User>(
                          value: selectedUser,
                          onChanged: (User? newUser) {
                            if (newUser != null) {
                              setState(() {
                                selectedUser = newUser;
                              });
                            }
                          },
                          items: widget.users.map((user) {
                            return DropdownMenuItem<User>(
                              value: user,
                              child: Text(
                                user.accountNo.toString(),
                                style: const TextStyle(
                                    fontSize: 20, fontWeight: FontWeight.bold),
                              ),
                            );
                          }).toList(),
                          isExpanded: true,
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: Card(
                    elevation: 4,
                    margin: const EdgeInsets.symmetric(vertical: 8.0),
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: ListView(
                        children: [
                          InfoRow(
                            title: AppLocalizations.of(context).customerNumber,
                            value: selectedUser.cifNumber ?? 'N/A',
                          ),
                          InfoRow(
                            title: AppLocalizations.of(context).accountType,
                            value: getFullAccountType(selectedUser.accountType),
                          ),
                          InfoRow(
                            title: AppLocalizations.of(context).productName,
                            value: selectedUser.productType ?? 'N/A',
                          ),
                          InfoRow(
                            title: AppLocalizations.of(context).accountStatus,
                            value: 'OPEN',
                          ),
                          InfoRow(
                            title:
                                AppLocalizations.of(context).availableBalance,
                            value: selectedUser.availableBalance ?? 'N/A',
                          ),
                          InfoRow(
                            title: AppLocalizations.of(context).currentBalance,
                            value: selectedUser.currentBalance ?? 'N/A',
                          ),
                          if (users[selectedIndex].approvedAmount != null)
                            InfoRow(
                              title:
                                  AppLocalizations.of(context).approvedAmount,
                              value: selectedUser.approvedAmount ?? 'N/A',
                            ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          IgnorePointer(
            child: Center(
              child: Opacity(
                opacity: 0.07, // Reduced opacity

                child: ClipOval(
                  child: Image.asset(
                    'assets/images/logo.png',

                    width: 200, // Adjust size as needed

                    height: 200, // Adjust size as needed
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class InfoRow extends StatelessWidget {
  final String title;
  final String value;

  const InfoRow({required this.title, required this.value, super.key});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.symmetric(vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: theme.colorScheme.onSurfaceVariant,
            ),
          ),
          const SizedBox(height: 3),
          Text(
            value,
            style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
                color: theme.colorScheme.onSurface),
          ),
        ],
      ),
    );
  }
}
