import 'package:doctor_app_flutter/core/enum/PatientType.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-referral-viewmodel.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/referral/refer_details/referred_patient_detail_in-paint.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient-referral-item-widget.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/errors/error_message.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class ReferredPatientScreen extends StatefulWidget {
  @override
  _ReferredPatientScreenState createState() => _ReferredPatientScreenState();
}

class _ReferredPatientScreenState extends State<ReferredPatientScreen> {
  PatientType patientType = PatientType.IN_PATIENT;

  @override
  Widget build(BuildContext context) {
    return BaseView<PatientReferralViewModel>(
      onModelReady: (model) => model.getMyReferredPatient(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        appBarTitle: TranslationBase.of(context).referredPatient,
        body: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 70),
              child: PatientTypeRadioWidget(
                (patientType) async {
                  setState(() {
                    this.patientType = patientType;
                  });
                  GifLoaderDialogUtils.showMyDialog(context);
                  if (patientType == PatientType.IN_PATIENT) {
                    await model.getMyReferredPatient(isFirstTime: false);
                  } else {
                    await model.getMyReferredOutPatient(isFirstTime: false);
                  }
                  GifLoaderDialogUtils.hideDialog(context);
                },
              ),
            ),
            if (model.listMyReferredPatientModel == null || model.listMyReferredPatientModel.length == 0)
              Center(
                  child: ErrorMessage(
                error: TranslationBase.of(context).referralEmptyMsg,
              ))
            else
              Expanded(
                child: SingleChildScrollView(
                  // DoctorApplication.svc/REST/GtMyReferredPatient
                  child: Container(
                    child: Column(
                      children: [
                        ListView.builder(
                          itemCount: model.listMyReferredPatientModel.length,
                          scrollDirection: Axis.vertical,
                          physics: ScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (context, index) {
                            return InkWell(
                              onTap: () {
                                Navigator.push(
                                  context,
                                  FadePage(
                                    page: ReferredPatientDetailScreen(model.getReferredPatientItem(index), this.patientType),
                                  ),
                                );
                              },
                              child: PatientReferralItemWidget(
                                referralStatus: model.getReferredPatientItem(index).referralStatusDesc!,
                                referralStatusCode: model.getReferredPatientItem(index).referralStatus!,
                                patientName: "${model.getReferredPatientItem(index).firstName} ${model.getReferredPatientItem(index).middleName} ${model.getReferredPatientItem(index).lastName}",
                                patientGender: model.getReferredPatientItem(index).gender!,
                                referredDate: model.state == ViewState.BusyLocal
                                    ? ""
                                    : AppDateUtils.getDayMonthYearDateFormatted(
                                        patientType == PatientType.IN_PATIENT
                                            ? AppDateUtils.getDateTimeFromServerFormat(
                                                model.getReferredPatientItem(index).referralDate!,
                                              )
                                            : AppDateUtils.getDateTimeFromString(
                                                model.getReferredPatientItem(index).referralDate!,
                                              ),
                                        isMonthShort: true),
                                referredTime: model.state == ViewState.BusyLocal
                                    ? ""
                                    : AppDateUtils.getHour(
                                        patientType == PatientType.IN_PATIENT
                                            ? AppDateUtils.getDateTimeFromServerFormat(
                                                model.getReferredPatientItem(index).referralDate!,
                                              )
                                            : AppDateUtils.getDateTimeFromString(
                                                model.getReferredPatientItem(index).referralDate!,
                                              ),
                                        // isMonthShort: true
                                      ),
                                patientID: "${model.getReferredPatientItem(index).patientID}",
                                isSameBranch: model.getReferredPatientItem(index).isReferralDoctorSameBranch,
                                isReferral: false,
                                remark: model.getReferredPatientItem(index).referringDoctorRemarks!,
                                nationality: model.getReferredPatientItem(index).nationalityName ?? "",
                                nationalityFlag: model.getReferredPatientItem(index).nationalityFlagURL!,
                                doctorAvatar: model.getReferredPatientItem(index).doctorImageURL!,
                                referralDoctorName: "${TranslationBase.of(context).dr} ${model.getReferredPatientItem(index).referralDoctorName}",
                                clinicDescription: model.getReferredPatientItem(index).referralClinicDescription!,
                                infoIcon: Icon(FontAwesomeIcons.arrowRight.data, size: 25, color: Colors.black),
                              ),
                            );
                          },
                        ),
                      ],
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}

class PatientTypeRadioWidget extends StatefulWidget {
  final Function(PatientType) radioOnChange;

  PatientTypeRadioWidget(this.radioOnChange);

  @override
  _PatientTypeRadioWidgetState createState() => _PatientTypeRadioWidgetState(this.radioOnChange);
}

class _PatientTypeRadioWidgetState extends State<PatientTypeRadioWidget> {
  final Function(PatientType) radioOnChange;

  _PatientTypeRadioWidgetState(this.radioOnChange);

  PatientType patientType = PatientType.IN_PATIENT;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: RadioListTile<PatientType>(
            title: AppText(TranslationBase.of(context).inPatient),
            value: PatientType.IN_PATIENT,
            groupValue: patientType,
            onChanged: (PatientType? value) {
              setState(() {
                patientType = value!;
                radioOnChange(value!);
              });
            },
          ),
        ),
        Expanded(
          child: RadioListTile<PatientType>(
            title: AppText(TranslationBase.of(context).outpatient),
            value: PatientType.OUT_PATIENT,
            groupValue: patientType,
            onChanged: (PatientType? value) {
              setState(() {
                patientType = value!;
                radioOnChange(value!);
              });
            },
          ),
        ),
      ],
    );
  }
}
