import 'package:flutter/material.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/models/country_model.dart';
import 'package:tangheem/widgets/common_textfield_widget.dart';

class CountrySelectionBottomSheet extends StatefulWidget {
  final List<CountryModelData> countryList;
  final Function(CountryModelData) onSelectCountry;

  CountrySelectionBottomSheet({Key key, this.countryList, this.onSelectCountry}) : super(key: key);

  @override
  _CountrySelectionBottomSheetState createState() {
    return _CountrySelectionBottomSheetState();
  }
}

class _CountrySelectionBottomSheetState extends State<CountrySelectionBottomSheet> {
  TextEditingController _searchCountryController = TextEditingController();
  List<CountryModelData> _filteredCountryList = [];

  @override
  void initState() {
    super.initState();
    _searchCountryController.addListener(_onTextChange);
    _filterList("");
  }

  void _filterList(String _query) {
    _filteredCountryList = [];
    if (_query.isEmpty) {
      _filteredCountryList = widget.countryList;
    } else {
      _filteredCountryList = widget.countryList.where((element) => element.countryNameAr.contains(_query) || element.countryNameEn.toLowerCase().contains(_query.toLowerCase()))?.toList() ?? [];
    }
    setState(() {});
  }

  void _onTextChange() {
    var _searchText = _searchCountryController.text;
    _filterList(_searchText);
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Container(
        height: MediaQuery.of(context).size.height * 0.75,
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(16),
            topRight: Radius.circular(16),
          ),
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.all(8),
              height: 54,
              decoration: BoxDecoration(
                color: ColorConsts.primaryBlue,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(12),
                  topRight: Radius.circular(12),
                  bottomRight: Radius.circular(12),
                  bottomLeft: Radius.circular(12),
                ),
              ),
              child: CommonTextFieldWidget(hint: "البحث في البلد", controller: _searchCountryController),
            ),
            Expanded(
              child: ListView.separated(
                padding: EdgeInsets.only(left: 8, right: 8),
                itemCount: _filteredCountryList.length,
                physics: BouncingScrollPhysics(),
                separatorBuilder: (context, index) => Divider(
                  height: 1,
                  color: Colors.black87.withOpacity(0.3),
                ),
                itemBuilder: (context, index) => ListTile(
                  title: Text(_filteredCountryList[index].countryNameAr + " (" + _filteredCountryList[index].countryCode + ")"),
                  dense: true,
                  onTap: () {
                    Navigator.pop(context);
                    widget.onSelectCountry(_filteredCountryList[index]);
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
