import 'package:flutter/material.dart';
import 'package:kmobile/data/models/beneficiary.dart';
import 'package:kmobile/di/injection.dart';
import 'package:kmobile/widgets/bank_logos.dart';
import 'package:kmobile/api/services/beneficiary_service.dart';

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

class BeneficiaryDetailsScreen extends StatelessWidget {
  final Beneficiary beneficiary;

  BeneficiaryDetailsScreen({super.key, required this.beneficiary});

  final service = getIt<BeneficiaryService>();

  void _deleteBeneficiary(BuildContext context) async {
    try {
      await service.deleteBeneficiary(beneficiary.accountNo);
      if (!context.mounted) {
        return;
      }
      _showSuccessDialog(context);
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Failed to delete beneficiary: $e')),
      );
    }
  }

  void _showSuccessDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Success'),
          content: const Text('Beneficiary deleted successfully.'),
          actions: <Widget>[
            TextButton(
              child: const Text('OK'),
              onPressed: () {
                Navigator.of(context).popUntil((route) => route.isFirst);
              },
            ),
          ],
        );
      },
    );
  }

  void _showDeleteConfirmationDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Delete Beneficiary'),
          content:
              const Text('Are you sure you want to delete this beneficiary?'),
          actions: <Widget>[
            TextButton(
              child: const Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('Delete'),
              onPressed: () {
                _deleteBeneficiary(context);
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).beneficiarydetails),
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  CircleAvatar(
                    radius: 24,
                    backgroundColor: Colors.transparent,
                    child: getBankLogo(beneficiary.bankName, context),
                  ),
                  const SizedBox(width: 16),
                  Text(
                    beneficiary.name,
                    style: const TextStyle(
                        fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
              const SizedBox(height: 24),
              _buildDetailRow('${AppLocalizations.of(context).bankName} ',
                  beneficiary.bankName ?? 'N/A'),
              _buildDetailRow('${AppLocalizations.of(context).accountNumber} ',
                  beneficiary.accountNo),
              _buildDetailRow('${AppLocalizations.of(context).accountType} ',
                  beneficiary.accountType),
              _buildDetailRow('${AppLocalizations.of(context).ifscCode} ',
                  beneficiary.ifscCode),
              _buildDetailRow('${AppLocalizations.of(context).branchName} ',
                  beneficiary.branchName ?? 'N/A'),
              const Spacer(),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  // ElevatedButton.icon(
                  //   onPressed: () {
                  //     // Set Transaction Limit for this beneficiary
                  //   },
                  //   icon: const Icon(Icons.currency_rupee),
                  //   label: const Text('Set Limit'),
                  // ),
                  ElevatedButton.icon(
                    onPressed: () {
                      // Delete beneficiary option
                      _showDeleteConfirmationDialog(context);
                    },
                    icon: const Icon(Icons.delete),
                    label: Text(AppLocalizations.of(context).delete),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildDetailRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
          const SizedBox(width: 16),
          Flexible(
            child: Text(
              value,
              textAlign: TextAlign.end,
            ),
          ),
        ],
      ),
    );
  }
}
