import '../../../l10n/app_localizations.dart';
import 'package:flutter/material.dart';

class Branch {
  final String name;
  final String code;
  final String ifsc;
  final String address;

  Branch({
    required this.name,
    required this.code,
    required this.ifsc,
    required this.address,
  });
}

class BranchLocatorScreen extends StatefulWidget {
  const BranchLocatorScreen({super.key});

  @override
  State<BranchLocatorScreen> createState() => _BranchLocatorScreenState();
}

class _BranchLocatorScreenState extends State<BranchLocatorScreen> {
  final TextEditingController _searchController = TextEditingController();

// Static list of 2 branches
  final List<Branch> _branches = [
    Branch(
        name: "Dharamsala - Head Office",
        code: "002",
        ifsc: "KACE0000002",
        address: "Civil Lines Dharmashala, Kangra, HP - 176215"),
    Branch(
        name: "Kangra",
        code: "033",
        ifsc: "KACE0000033",
        address: "Rajput Bhawankangrapo, Kangra, HP "),
  ];

  List<Branch> _filteredBranches = [];

  @override
  void initState() {
    super.initState();
    _filteredBranches = _branches; // Initially show all branches
  }

  void _filterBranches(String query) {
    setState(() {
      if (query.isEmpty) {
        _filteredBranches = _branches;
      } else {
        _filteredBranches = _branches.where((branch) {
          final lowerQuery = query.toLowerCase();
          return branch.name.toLowerCase().contains(lowerQuery) ||
              branch.code.toLowerCase().contains(lowerQuery) ||
              branch.ifsc.toLowerCase().contains(lowerQuery) ||
              branch.address.toLowerCase().contains(lowerQuery);
        }).toList();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context).branchLocator),
      ),
      body: Column(
        children: [
// Search bar
          Padding(
            padding: const EdgeInsets.all(12.0),
            child: TextField(
              controller: _searchController,
              onChanged: _filterBranches,
              decoration: InputDecoration(
                hintText: AppLocalizations.of(context).searchbranchby,
                prefixIcon: const Icon(Icons.search),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
            ),
          ),

// List of branches
          Expanded(
            child: _filteredBranches.isEmpty
                ? const Center(child: Text("No matching branches found"))
                : ListView.builder(
                    itemCount: _filteredBranches.length,
                    itemBuilder: (context, index) {
                      final branch = _filteredBranches[index];
                      return Card(
                        margin: const EdgeInsets.symmetric(
                            horizontal: 12, vertical: 6),
                        child: ListTile(
                          leading: Icon(Icons.location_city,
                              color: Theme.of(context).colorScheme.primary),
                          title: Text(branch.name,
                              style:
                                  const TextStyle(fontWeight: FontWeight.bold)),
                          subtitle: Text(
                              "Code: ${branch.code} | IFSC: ${branch.ifsc} \nBranch Address: ${branch.address}"),
                          onTap: () {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                  content: Text("Selected ${branch.name}")),
                            );
                          },
                        ),
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }
}
