import 'package:flutter/material.dart';
import 'package:kmobile/api/services/cheque_service.dart';
import 'package:kmobile/data/models/user.dart';
import 'package:kmobile/di/injection.dart';
import 'package:kmobile/l10n/app_localizations.dart';

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

  @override
  State<ChequeEnquiryScreen> createState() => _ChequeEnquiryScreenState();
}

class _ChequeEnquiryScreenState extends State<ChequeEnquiryScreen> {
  User? _selectedAccount;
  final TextEditingController _searchController = TextEditingController();
  var service = getIt<ChequeService>();
  bool _isLoading = true;
  List<Cheque> _allCheques = [];
  Map<String, List<Cheque>> _groupedCheques = {};
  List<User> _filteredUsers = [];

  @override
  void initState() {
    super.initState();
    _filteredUsers = widget.users
        .where((user) => ['SA', 'SB', 'CA', 'CC'].contains(user.accountType))
        .toList();

    if (widget.users.isNotEmpty && widget.selectedIndex < widget.users.length) {
      if (_filteredUsers.isNotEmpty) {
        if (_filteredUsers.contains(widget.users[widget.selectedIndex])) {
          _selectedAccount = widget.users[widget.selectedIndex];
        } else {
          _selectedAccount = _filteredUsers.first;
        }
      } else {
        _selectedAccount = widget.users[widget.selectedIndex];
      }
    } else {
      if (_filteredUsers.isNotEmpty) {
        _selectedAccount = _filteredUsers.first;
      }
    }

    _loadCheques();
    _searchController.addListener(() {
      _filterCheques(_searchController.text);
    });
  }

  Future<void> _loadCheques() async {
    if (_selectedAccount == null) {
      setState(() {
        _isLoading = false;
        _groupedCheques = {};
      });
      return;
    }
    setState(() {
      _isLoading = true;
    });

    String instrType;
    switch (_selectedAccount!.accountType) {
      case 'SA':
      case 'SB':
        instrType = '10';
        break;
      case 'CA':
        instrType = '11';
        break;
      case 'CC':
        instrType = '13';
        break;
      default:
        instrType = '10';
    }

    try {
      final data = await service.ChequeEnquiry(
          accountNumber: _selectedAccount!.accountNo!, instrType: instrType);
      _allCheques = data;
      _groupedCheques.clear();
      for (var cheque in _allCheques) {
        if (cheque.type != null) {
          if (!_groupedCheques.containsKey(cheque.type)) {
            _groupedCheques[cheque.type!] = [];
          }
          _groupedCheques[cheque.type!]!.add(cheque);
        }
      }
      setState(() {
        _isLoading = false;
      });
    } catch (e) {
      setState(() {
        _isLoading = false;
        _groupedCheques = {};
      });
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('Failed to fetch cheque status: ${e.toString()}'),
        ),
      );
    }
  }

  void _filterCheques(String query) {
    _groupedCheques.clear();
    List<Cheque> filteredCheques;
    if (query.isEmpty) {
      filteredCheques = _allCheques;
    } else {
      filteredCheques = _allCheques.where((cheque) {
        final lowerQuery = query.toLowerCase();
        return (cheque.ChequeNumber?.toLowerCase().contains(lowerQuery) ??
                false) ||
            (cheque.status?.toLowerCase().contains(lowerQuery) ?? false) ||
            (cheque.fromCheque?.toLowerCase().contains(lowerQuery) ?? false) ||
            (cheque.toCheque?.toLowerCase().contains(lowerQuery) ?? false);
      }).toList();
    }

    for (var cheque in filteredCheques) {
      if (cheque.type != null) {
        if (!_groupedCheques.containsKey(cheque.type)) {
          _groupedCheques[cheque.type!] = [];
        }
        _groupedCheques[cheque.type!]!.add(cheque);
      }
    }

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).chequeEnquiryTitle),
        centerTitle: false,
      ),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                Card(
                  elevation: 4,
                  margin: const EdgeInsets.symmetric(vertical: 8.0),
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          AppLocalizations.of(context).accountNumber,
                          style: const TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 18),
                        ),
                        const SizedBox(width: 16),
                        if (_selectedAccount != null)
                          Expanded(
                            child: DropdownButton<User>(
                              value: _selectedAccount,
                              onChanged: (User? newUser) {
                                if (newUser != null) {
                                  setState(() {
                                    _selectedAccount = newUser;
                                    _loadCheques();
                                  });
                                }
                              },
                              items: _filteredUsers.map((user) {
                                return DropdownMenuItem<User>(
                                  value: user,
                                  child: Text(user.accountNo.toString()),
                                );
                              }).toList(),
                            ),
                          )
                        else
                          const Text('No accounts found'),
                      ],
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                Card(
                  elevation: 4,
                  margin: const EdgeInsets.symmetric(vertical: 8.0),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TextField(
                      controller: _searchController,
                      decoration: InputDecoration(
                        labelText: AppLocalizations.of(context)
                            .searchByChequeDetailsHint,
                        prefixIcon: const Icon(Icons.search),
                        border: InputBorder
                            .none, // Remove border to make it look like it's inside the card
                      ),
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                Expanded(
                  child: _isLoading
                      ? const Center(child: CircularProgressIndicator())
                      : _groupedCheques.isEmpty
                          ? Center(
                              child: Text(AppLocalizations.of(context)
                                  .noChequeStatusFound))
                          : ListView(
                              children: _groupedCheques.entries.map((entry) {
                                return Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    ...entry.value.map((cheque) =>
                                        ChequeStatusTile(cheque: cheque)),
                                  ],
                                );
                              }).toList(),
                            ),
                ),
              ],
            ),
          ),
          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 ChequeStatusTile extends StatelessWidget {
  final Cheque cheque;

  const ChequeStatusTile({
    super.key,
    required this.cheque,
  });

  @override
  Widget build(BuildContext context) {
    switch (cheque.type) {
      case 'CI':
        return _buildCiTile(context);
      case 'PR':
        return _buildPrTile(context);
      case 'ST':
        return _buildStTile(context);
      default:
        return const SizedBox.shrink();
    }
  }

  Widget _buildCiTile(BuildContext context) {
    return Card(
      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).chequebookIssuedLabel,
                style: Theme.of(context).textTheme.titleLarge),
            const SizedBox(height: 8),
            _buildInfoRow('Branch Code:', cheque.branchCode),
            _buildInfoRow('From Cheque:', cheque.fromCheque),
            _buildInfoRow('To Cheque:', cheque.toCheque),
            _buildInfoRow('Date:', cheque.Date),
            _buildInfoRow('Cheques Count:', cheque.Chequescount),
          ],
        ),
      ),
    );
  }

  Widget _buildPrTile(BuildContext context) {
    return Card(
      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).presentedChequeLabel,
                style: Theme.of(context).textTheme.titleLarge),
            const SizedBox(height: 8),
            _buildInfoRow('Branch Code:', cheque.branchCode),
            _buildInfoRow('Cheque Number:', cheque.ChequeNumber),
            _buildInfoRow('Date:', cheque.Date),
            _buildInfoRow('Transaction Code:', cheque.transactionCode),
            _buildInfoRow('Amount:', '₹ ${cheque.amount.toString()}'),
            _buildInfoRow('Status:', cheque.status),
          ],
        ),
      ),
    );
  }

  Widget _buildStTile(BuildContext context) {
    return Card(
      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).stopChequeLabel,
                style: Theme.of(context).textTheme.titleLarge),
            const SizedBox(height: 8),
            _buildInfoRow('Branch Code:', cheque.branchCode),
            _buildInfoRow('From Cheque:', cheque.fromCheque),
            _buildInfoRow('To Cheque:', cheque.toCheque),
            _buildInfoRow('Stop Issue Date:', cheque.stopIssueDate),
            _buildInfoRow('Stop Expiry Date:', cheque.StopExpiryDate),
            _buildInfoRow('Cheques Count:', cheque.Chequescount),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String? value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
          Text(value ?? ''),
        ],
      ),
    );
  }
}
