import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/departments_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/department.dart';
import 'package:test_sa/views/widgets/departments/department_item.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

import '../app_text_form_field.dart';

class SingleDepartmentPicker extends StatefulWidget {
  static const String id = "/single-Department-Picker";

  const SingleDepartmentPicker({Key? key}) : super(key: key);

  @override
  _SingleDepartmentPickerState createState() => _SingleDepartmentPickerState();
}

class _SingleDepartmentPickerState extends State<SingleDepartmentPicker> {
  late DepartmentsProvider _departmentsProvider;
  late SettingProvider _settingProvider;
  final List<Department> _searchableList = [];
  bool _firstTime = true;

  @override
  Widget build(BuildContext context) {
    _departmentsProvider = Provider.of<DepartmentsProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    if (_firstTime && _departmentsProvider.departments != null) {
      _searchableList.addAll(_departmentsProvider.departments!);
      _firstTime = false;
    }

    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: LoadingManager(
        isLoading: _departmentsProvider.isLoading,
        stateCode: _departmentsProvider.stateCode,
        isFailedLoading: _departmentsProvider.departments == null,
        onRefresh: () async {
          _departmentsProvider.reset();
          await _departmentsProvider.getDepartment(_settingProvider.host!);
        },
        child: Column(
          children: [
            const SizedBox(height: 48),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
              child: ATextFormField(
                hintText: context.translation.searchByName,
                style: Theme.of(context).textTheme.titleLarge,
                suffixIcon: const Icon(Icons.search_rounded),
                onChange: (value) {
                  _searchableList.clear();
                  _searchableList.addAll(_departmentsProvider.departments!.where((element) => element.name!.toLowerCase().contains(value.toLowerCase())).toList());
                  setState(() {});
                },
              ),
            ),
            Expanded(
              child: _searchableList.isEmpty
                  ? NoDataFound(
                      message: context.translation.noUniteFound,
                    )
                  : ListView.builder(
                      padding: EdgeInsets.zero,
                      shrinkWrap: true,
                      itemCount: _searchableList.length,
                      itemBuilder: (listContext, itemIndex) {
                        return DepartmentItem(
                          department: _searchableList[itemIndex],
                          onPressed: (hospital) {
                            Navigator.of(context).pop(hospital);
                          },
                        );
                      },
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
