import 'package:hmg_patient_app/core/service/AlHabibMedicalService/health-calculator/bariatrics-service.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/pages/BookAppointment/SearchResults.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/buttons/secondary_button.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class DeliveryDueResult extends StatelessWidget {
  var dateFrom;
  var dateTo;
  var conceivedDate;
  var deliveryDue;
  var firstTrimester;
  var secondTrimester;
  var thirdTrimester;

  DeliveryDueResult({this.dateFrom, this.dateTo, this.conceivedDate, this.deliveryDue, this.firstTrimester, this.secondTrimester, this.thirdTrimester});

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      isShowDecPage: false,
      appBarTitle: TranslationBase.of(context).pregnancyTitle,
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 21.0, vertical: 21.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Text(
                      TranslationBase.of(context).pregnancyTitle,
                      style: TextStyle(
                        fontSize: 19,
                        letterSpacing: -1.14,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    Text(
                      TranslationBase.of(context).ovulationPeriod,
                      style: TextStyle(
                        fontSize: 14,
                        letterSpacing: -0.56,
                        fontWeight: FontWeight.w600,
                        color: CustomColors.textColor,
                      ),
                    ),
                    mHeight(20),
                    showItem(TranslationBase.of(context).from, DateFormat.yMMMEd().format(dateFrom), titleColor: CustomColors.accentColor),
                    mHeight(12),
                    showItem(TranslationBase.of(context).to, DateFormat.yMMMEd().format(dateTo), titleColor: CustomColors.accentColor),
                    mHeight(12),
                    mDivider(CustomColors.devider),
                    mHeight(12),
                    showItem(TranslationBase.of(context).conceive, DateFormat.yMMMEd().format(conceivedDate)),
                    mHeight(12),
                    mDivider(CustomColors.devider),
                    mHeight(12),
                    showItem(TranslationBase.of(context).firstTri, DateFormat.yMMMEd().format(firstTrimester)),
                    mHeight(12),
                    mDivider(CustomColors.devider),
                    mHeight(12),
                    showItem(TranslationBase.of(context).secondTri, DateFormat.yMMMEd().format(secondTrimester)),
                    mHeight(12),
                    mDivider(CustomColors.devider),
                    mHeight(12),
                    showItem(TranslationBase.of(context).thirdTri, DateFormat.yMMMEd().format(thirdTrimester)),
                    mHeight(12),
                    mDivider(CustomColors.devider),
                    mHeight(12),
                  ],
                ).withBorderedContainer,
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
            color: Colors.white,
            child: SecondaryButton(
              label: TranslationBase.of(context).seeDoctorsList,
              color: CustomColors.accentColor,
              onTap: () {
                getDoctorsList(context);
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget showItem(String title, String value, {Color? titleColor}) {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 14,
              letterSpacing: -0.56,
              color: titleColor ?? Colors.black,
              fontWeight: FontWeight.w500,
              // fontWeight: FontWeight.w600,
            ),
          ),
          Text(
            value,
            style: TextStyle(
              fontSize: 18,
              letterSpacing: -1.08,
              fontWeight: FontWeight.w600,
            ),
          ),
        ],
      ),
    );
  }

  getDoctorsList(BuildContext context) {
    GifLoaderDialogUtils.showMyDialog(context);
    BariatricsService service = new BariatricsService();

    List<DoctorList> doctorsList = [];
    List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital =[];

    service.getCalculationDoctors(calculationID: 6).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      print(res['List_CalculationTable'].length);
      if (res['MessageStatus'] == 1) {
        if (res['List_CalculationTable'].length != 0) {
          res['List_CalculationTable'].forEach((item) {
            DoctorList docList = new DoctorList();
            docList.clinicID = item['ClinicID'];
            docList.clinicName = item['ClinicName'];
            docList.doctorID = item['DoctorID'];
            docList.doctorImageURL = item['DoctorImageURL'];
            docList.name = item['DoctorName'];
            docList.doctorTitle = item['DoctorTitle'];
            docList.gender = item['Gender'];
            docList.genderDescription = item['GenderDescription'];
            docList.nationalityName = item['Nationality'];
            docList.nationalityFlagURL = item['NationalityFlagURL'];
            docList.projectID = item['ProjectID'];
            docList.projectName = item['ProjectName'];
            docList.actualDoctorRate = 0;
            doctorsList.add(docList);
          });

          doctorsList.forEach((element) {
            List<PatientDoctorAppointmentList> doctorByHospital = _patientDoctorAppointmentListHospital
                .where(
                  (elementClinic) => elementClinic.filterName == element.projectName,
                )
                .toList();

            if (doctorByHospital.length != 0) {
              _patientDoctorAppointmentListHospital[_patientDoctorAppointmentListHospital.indexOf(doctorByHospital[0])].patientDoctorAppointmentList!.add(element);
            } else {
              _patientDoctorAppointmentListHospital.add(PatientDoctorAppointmentList(filterName: element.projectName, distanceInKMs: "0", patientDoctorAppointment: element));
            }
          });

          Navigator.push(context, FadePage(page: SearchResults(isLiveCareAppointment: false, doctorsList: doctorsList, patientDoctorAppointmentListHospital: _patientDoctorAppointmentListHospital, isDoctorSearchResult: false,)));
        }
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }
}

extension BorderedContainer on Widget {
  Widget get withBorderedContainer => Container(
        padding: EdgeInsets.only(left: 16, right: 16, bottom: 15, top: 15),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          color: Colors.white,
          border: Border.all(
            color: Color(0xffefefef),
            width: 1,
          ),
        ),
        child: this,
      );
}
