import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/api/hospitals_provider.dart';
import '../../../controllers/providers/settings/setting_provider.dart';
import '../../../models/hospital.dart';
import '../../../models/subtitle.dart';
import '../../../models/user.dart';
import '../app_text_form_field.dart';
import '../loaders/loading_manager.dart';
import '../loaders/no_item_found.dart';
import 'hospital_item.dart';

class SingleHospitalPicker extends StatefulWidget {
  static final String id = "/single-Hospital-Picker";
  final bool sandraChoice = true;
  @override
  _SingleHospitalPickerState createState() => _SingleHospitalPickerState();
}

class _SingleHospitalPickerState extends State<SingleHospitalPicker> {
  HospitalsProvider? _hospitalsProvider;
  SettingProvider? _settingProvider;
  List<Hospital> _searchableList = [];
  bool _firstTime = true;
  @override
  Widget build(BuildContext context) {
    _hospitalsProvider = Provider.of<HospitalsProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    if (_firstTime && _hospitalsProvider?.hospitals != null) {
      _searchableList.addAll(_hospitalsProvider?.hospitals ?? []);
      _firstTime = false;
    }
    Subtitle? _subtitle = AppLocalization.of(context)?.subtitle;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: LoadingManager(
        isLoading: _hospitalsProvider?.isLoading,
        stateCode: _hospitalsProvider?.stateCode,
        isFailedLoading: _hospitalsProvider?.hospitals == null,
        onRefresh: () async {
          _hospitalsProvider?.reset();
          await _hospitalsProvider?.getHospitals(host: _settingProvider?.host ?? "", user: User(), title: '');
        },
        child: Column(
          children: [
            SizedBox(
              height: 48,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
              child: ATextFormField(
                hintText: _subtitle?.searchByName ?? "",
                style: Theme.of(context).textTheme.headline6,
                suffixIcon: Icon(Icons.search_rounded),
                onChange: (value) {
                  _searchableList.clear();
                  _searchableList.addAll(_hospitalsProvider!.hospitals!.where((element) => element.name!.toLowerCase().contains(value.toLowerCase())).toList());
                  setState(() {});
                },
              ),
            ),
            Expanded(
              child: _searchableList.isEmpty
                  ? NoItemFound(
                      message: _subtitle?.noHospitalFound ?? "",
                    )
                  : ListView.builder(
                      padding: EdgeInsets.zero,
                      shrinkWrap: true,
                      itemCount: _searchableList.length,
                      itemBuilder: (listContext, itemIndex) {
                        return HospitalItem(
                          hospital: _searchableList[itemIndex],
                          onPressed: (hospital) {
                            Navigator.of(context).pop(hospital);
                          },
                        );
                      },
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
