import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_card/PatientCard.dart';
import 'package:doctor_app_flutter/widgets/shared/app_loader_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_container.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import '../../routes.dart';

class InPatientPage extends StatefulWidget {
  final bool isMyInPatient;
  final PatientSearchViewModel  patientSearchViewModel;

  InPatientPage(this.isMyInPatient, this.patientSearchViewModel);

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

class _InPatientPageState extends State<InPatientPage> {
   
  TextEditingController _searchController = TextEditingController();

  bool isSortDes = false;

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

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      baseViewModel: widget.patientSearchViewModel,
      isShowAppBar: false,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: MediaQuery.of(context).size.height * 0.070,
          ),
          Container(
            margin: EdgeInsets.all(16.0),
            child: Stack(
              children: [
                AppTextFieldCustom(
                    hintText: TranslationBase.of(context).searchPatientName,
                    isTextFieldHasSuffix: true,
                    suffixIcon: IconButton(
                      icon: Icon(
                        Icons.search,
                        color: Colors.black,
                      ),
                      onPressed: () {},
                    ),
                    controller: _searchController,
                    onChanged: (value) {
                      widget.patientSearchViewModel.filterSearchResults(value);
                    }),

                Positioned(
                  right: 35,
                  top:5,
                  child: IconButton(
                    icon: Icon(
                      isSortDes
                          ? FontAwesomeIcons.sortAmountDown
                          : FontAwesomeIcons.sortAmountUp,
                      color: Colors.black,
                    ),
                    iconSize: 20,
                    // padding: EdgeInsets.only(bottom: 30),
                    onPressed:  () {
                      GifLoaderDialogUtils.showMyDialog(context);
                      widget.patientSearchViewModel.sortInPatient(isDes: isSortDes);
                      isSortDes = !isSortDes;
                      GifLoaderDialogUtils.hideDialog(context);
                    },
                  ),
                )
              ],
            ),
          ),
          widget.patientSearchViewModel.state == ViewState.Idle?widget.patientSearchViewModel.filteredInPatientItems.length > 0
              ? (widget.isMyInPatient && widget.patientSearchViewModel.myIinPatientList.length ==0 )?NoData():Expanded(
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 16.0),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ...List.generate(
                        widget.patientSearchViewModel.filteredInPatientItems.length, (index) {
                      if (!widget.isMyInPatient)
                        return PatientCard(
                          patientInfo:
                          widget.patientSearchViewModel.filteredInPatientItems[index],
                          patientType: "1",
                          arrivalType: "1",
                          isInpatient: true,
                          isMyPatient: widget.patientSearchViewModel.filteredInPatientItems[index]
                              .doctorId ==
                              widget.patientSearchViewModel.doctorProfile.doctorID,
                          onTap: () {
                            FocusScopeNode currentFocus =
                            FocusScope.of(context);
                            if (!currentFocus.hasPrimaryFocus) {
                              currentFocus.unfocus();
                            }

                            Navigator.of(context).pushNamed(
                                PATIENTS_PROFILE,
                                arguments: {
                                  "patient": widget.patientSearchViewModel.
                                  filteredInPatientItems[index],
                                  "patientType": "1",
                                  "from": "0",
                                  "to": "0",
                                  "isSearch": false,
                                  "isInpatient": true,
                                  "arrivalType": "1",
                                });
                          },
                        );
                      else if (widget.patientSearchViewModel.filteredInPatientItems[index]
                          .doctorId ==
                          widget.patientSearchViewModel.doctorProfile.doctorID &&
                          widget.isMyInPatient)
                        return PatientCard(
                          patientInfo:
                          widget.patientSearchViewModel.filteredInPatientItems[index],
                          patientType: "1",
                          arrivalType: "1",
                          isInpatient: true,
                          isMyPatient: widget.patientSearchViewModel
                              .filteredInPatientItems[index]
                              .doctorId ==
                              widget.patientSearchViewModel.doctorProfile.doctorID,
                          onTap: () {
                            FocusScopeNode currentFocus =
                            FocusScope.of(context);
                            if (!currentFocus.hasPrimaryFocus) {
                              currentFocus.unfocus();
                            }

                            Navigator.of(context).pushNamed(
                                PATIENTS_PROFILE,
                                arguments: {
                                  "patient": widget.patientSearchViewModel.filteredInPatientItems[index],
                                  "patientType": "1",
                                  "from": "0",
                                  "to": "0",
                                  "isSearch": false,
                                  "isInpatient": true,
                                  "arrivalType": "1",
                                  "isMyPatient":widget.patientSearchViewModel.filteredInPatientItems[index]
                                      .doctorId ==
                                      widget.patientSearchViewModel.doctorProfile.doctorID,
                                });
                          },
                        );
                      else
                        return SizedBox();
                    }),
                    SizedBox(
                      height: 15,
                    )
                  ],
                ),
              ),
            ),
          )
              : NoData(): Center(
            child: Container(
              height: 300,
              width: 300,

            ),
          ),
        ],
      ),
    );
  }
}

class NoData extends StatelessWidget {
  const NoData({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
            child: SingleChildScrollView(
    child: Container(
        child: ErrorMessage(
            error:
            TranslationBase.of(context).noDataAvailable)),
            ),
          );
  }
}
