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

class OvulationResult extends StatelessWidget {
  var dateFrom;
  var dateTo;
  var conceivedDate;
  var deliveryDue;
  var babyAge;
  var babyAgeDays;

  OvulationResult({
    this.dateFrom,
    this.dateTo,
    this.deliveryDue,
    this.conceivedDate,
    this.babyAge,
    this.babyAgeDays,
  });
  //var newFormat = DateFormat("yy-MM-dd");

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      showNewAppBar: true,
      showNewAppBarTitle: true,
      isShowDecPage: false,
      appBarTitle: TranslationBase.of(context).ovulation,
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 30.0, vertical: 15.0),
        child: SingleChildScrollView(
          child: Container(
            height: 750.0,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Texts(
                  'The next ovulation period is estimated to be:',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  'From:',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  DateFormat.yMMMEd().format(dateFrom),
                  fontWeight: FontWeight.w800,
                  fontSize: 21.0,
                ),
                Texts(
                  'To:',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  DateFormat.yMMMEd().format(dateTo),
                  fontWeight: FontWeight.w800,
                  fontSize: 21.0,
                ),
                Texts(
                  'Useful Information:',
                  color: Color(0xffC5272D),
                ),
                Texts(
                  'You have conceived on:',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  DateFormat.yMMMEd().format(conceivedDate),
                  fontWeight: FontWeight.w800,
                  fontSize: 21.0,
                ),
                Texts(
                  'The baby\'s age right now:',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  babyAge <= 0 ? "baby age is not available" : babyAge.toString() + " Weeks",
                  fontWeight: FontWeight.w800,
                  fontSize: 21.0,
                ),
                Texts(
                  'The delivery due date is estimated to be on the: ',
                  fontWeight: FontWeight.w400,
                ),
                Texts(
                  DateFormat.yMMMEd().format(deliveryDue),
                  fontWeight: FontWeight.w800,
                  fontSize: 21.0,
                ),
                Container(
                  width: 350,
                  child: DefaultButton(
                    TranslationBase.of(context).seeDoctorsList,
                    () {
                      getDoctorsList(context);
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

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

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

    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)));

        }
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

}
