import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/screens/patients/In_patient/filter_date_page.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app_text_field_custom_serach.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'NoData.dart';
import 'list_of_all_in_patient.dart';
import 'list_of_my_inpatient.dart';

class InPatientListPage extends StatefulWidget {
  final bool isMyInPatient;
  final PatientSearchViewModel? patientSearchViewModel;

  final bool isAllClinic;
  final bool showBottomSheet;
  final String? selectedClinicName;
  final Function? onChangeValue;
  final bool isVidaPlusProject;

  InPatientListPage({this.isMyInPatient = false, this.patientSearchViewModel, this.selectedClinicName, this.onChangeValue, this.isAllClinic = false, this.showBottomSheet = false, required this.isVidaPlusProject});

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

class _InPatientListPageState extends State<InPatientListPage> {
  TextEditingController _searchController = TextEditingController();

  bool isSortDes = false;
  bool hasQuery = false;

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

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      baseViewModel: widget.patientSearchViewModel!,
      isShowAppBar: false,
      backgroundColor: Color(0xFFF8F8F8),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (!widget.isMyInPatient && widget.patientSearchViewModel!.mainList.isNotEmpty)
            Container(
              margin: EdgeInsets.only(left: 10.0, right: 10, top: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    child: Row(
                      children: [
                        Radio(
                          value: 1,
                          groupValue: widget.isAllClinic ? 1 : 2,
                          onChanged: (value) {
                            setState(() {
                              widget.onChangeValue!(isAllClinic: true, showBottomSheet: false, selectedClinicName: null);

                              widget.patientSearchViewModel!.resetFilters();
                            });
                          },
                          activeColor: Colors.red,
                        ),
                        AppText(
                          "${TranslationBase.of(context).allClinic}",
                          fontSize: 15,
                        ),
                      ],
                    ),
                    onTap: () {
                      widget.onChangeValue!(isAllClinic: true, showBottomSheet: false, selectedClinicName: null);

                      widget.patientSearchViewModel!.resetFilters();
                    },
                  ),
                  if (widget.patientSearchViewModel!.InpatientClinicList.isNotEmpty)
                    InkWell(
                        onTap: () {
                          if (widget.patientSearchViewModel!.InpatientClinicList.length > 0) {
                            widget.onChangeValue!(isAllClinic: false, showBottomSheet: true, selectedClinicName: widget.selectedClinicName);
                          }
                        },
                        child: Row(
                          children: [
                            Radio(
                              value: 2,
                              groupValue: widget.isAllClinic ? 1 : 2,
                              onChanged: (value) {
                                if (widget.patientSearchViewModel!.InpatientClinicList.length > 0) {
                                  widget.onChangeValue!(isAllClinic: false, showBottomSheet: true, selectedClinicName: widget.selectedClinicName);
                                }
                              },
                              activeColor: Colors.red,
                            ),
                            AppText(
                              widget.selectedClinicName ?? TranslationBase.of(context).clinicSelect,
                              fontSize: 15,
                            ),
                            Container(margin: EdgeInsets.only(bottom: 10), child: Icon(FontAwesomeIcons.sortDown.data))
                          ],
                        )),
                ],
              ),
            ),
          AppTextFieldCustomSearch(
            searchController: _searchController,
            onChangeFun: (value) {
              setState(() {
                hasQuery = true;
              });
              widget.patientSearchViewModel!.filterSearchResults(value, isAllClinic: widget.isAllClinic, isMyInPatient: widget.isMyInPatient);
            },
            marginTop: widget.isMyInPatient ? 15 : 5,
            positionedChild: IconButton(
              icon: Icon(
                 isSortDes ? FontAwesomeIcons.sortAmountDown.data : FontAwesomeIcons.sortAmountUp.data,
                 color: Colors.black,
               ),
              iconSize: 20,
              // padding: EdgeInsets.only(bottom: 30),
              onPressed: () {
                GifLoaderDialogUtils.showMyDialog(context);
                widget.patientSearchViewModel!.sortInPatient(isDes: isSortDes, isAllClinic: widget.isAllClinic, isMyInPatient: widget.isMyInPatient);
                isSortDes = !isSortDes;
                GifLoaderDialogUtils.hideDialog(context);
              },
            ),
            suffixIcon: (widget.isVidaPlusProject)?IconButton(
              icon: Icon(
                DoctorApp.filter_1,
                color: Colors.black,
              ),
              iconSize: 20,
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (BuildContext context) => FilterDatePage(
                        patientSearchViewModel: widget.patientSearchViewModel!,
                      ),
                      settings: RouteSettings(name: 'FilterOutPatentDateScreen'),
                    ));
              },
            ):null,
            onFieldSubmitted: () {},
          ),
          widget.patientSearchViewModel!.state == ViewState.Idle
              ? (widget.isMyInPatient && widget.patientSearchViewModel!.filteredMyInPatientItems.length > 0)
                  ? ListOfMyInpatient(isAllClinic: widget.isAllClinic, hasQuery: hasQuery, patientSearchViewModel: widget.patientSearchViewModel!, isVidaPlusProject: widget.isVidaPlusProject,)
                  : widget.patientSearchViewModel!.filteredInPatientItems.length > 0
                      ? (widget.isMyInPatient && widget.patientSearchViewModel!.filteredMyInPatientItems.length == 0)
                          ? NoData()
                          : ListOfAllInPatient(isAllClinic: widget.isAllClinic, hasQuery: hasQuery, patientSearchViewModel: widget.patientSearchViewModel!, isVidaPlusProject: widget
              .isVidaPlusProject,query: _searchController.text, selectedClinicName: widget.selectedClinicName,isSortDes: isSortDes)
                      : NoData()
              : Center(
                  child: Container(
                    height: 300,
                    width: 300,
                  ),
                ),
          SizedBox(height: 16,),
          Visibility(
            visible: widget.patientSearchViewModel!.paginationLoading,
            child: Center(
              child: SizedBox(
                height: 25,
                width: 25,
                child: CircularProgressIndicator(
                  color: AppGlobal.appRedColor,
                ),
              ),
            ),
          )
        ],
      ),
      bottomSheet: !widget.showBottomSheet
          ? Container(
              height: 0,
            )
          : Container(
              decoration: BoxDecoration(
                border: Border.all(color: Colors.grey, width: 0.5),
                color: Colors.white,
              ),
              height: MediaQuery.of(context).size.height * 0.5,
              padding: EdgeInsets.only(top: 13, left: 10, right: 10),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      InkWell(
                          onTap: () {
                            widget.onChangeValue!(isAllClinic: widget.isAllClinic, showBottomSheet: false, selectedClinicName: widget.selectedClinicName);
                          },
                          child: Icon(DoctorApp.close_1, size: SizeConfig.getTextMultiplierBasedOnWidth() * 4.5, color: Color(0xFF2B353E)))
                    ],
                  ),
                  Row(
                    children: [
                      AppText(
                        "${TranslationBase.of(context).clinicSelect} :",
                        fontFamily: 'Poppins',
                        fontWeight: FontWeight.w700,
                        fontSize: 18,
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Expanded(
                    child: ListView.builder(
                        itemCount: widget.patientSearchViewModel!.InpatientClinicList.length,
                        scrollDirection: Axis.vertical,
                        physics: ScrollPhysics(),
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          return InkWell(
                              onTap: () {
                                widget.onChangeValue!(isAllClinic: false, showBottomSheet: false, selectedClinicName: widget.patientSearchViewModel!.InpatientClinicList[index]);

                                widget.patientSearchViewModel!.filterByClinic(clinicName: widget.patientSearchViewModel!.InpatientClinicList[index]);
                              },
                              child: Row(
                                children: [
                                  Radio(
                                    value: widget.patientSearchViewModel!.InpatientClinicList[index],
                                    groupValue: widget.selectedClinicName,
                                    onChanged: (value) {
                                      setState(() {
                                        widget.onChangeValue!(isAllClinic: false, showBottomSheet: false, selectedClinicName: widget.patientSearchViewModel!.InpatientClinicList[index]);
                                        widget.patientSearchViewModel!.filterByClinic(clinicName: value!);
                                      });
                                    },
                                    activeColor: Colors.red,
                                  ),
                                  AppText(
                                    widget.patientSearchViewModel!.InpatientClinicList[index],
                                    fontFamily: 'Poppins',
                                    fontWeight: FontWeight.w600,
                                    fontSize: 15,
                                  ),
                                ],
                              ));
                        }),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                ],
              ),
            ),
    );
  }
}
