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/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.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';

class IdealBodyResult extends StatelessWidget {
  final double idealBodyWeight;
  final double minRange;
  final double mixRange;
  final double overWeightBy;
  final String textResult;

  IdealBodyResult({this.idealBodyWeight, this.minRange, this.mixRange, this.overWeightBy, this.textResult});

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      isShowDecPage: false,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: TranslationBase.of(context).idealBody,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: const EdgeInsets.all(20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  TranslationBase.of(context).idealWeightRange,
                  style: TextStyle(
                    fontSize: 19,
                    letterSpacing: -1.34,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                mHeight(12),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Row(
                      children: [
                        Texts(
                          minRange.toStringAsFixed(1),
                          fontSize: 30.0,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 8.0, left: 4.0),
                          child: Text(
                            " " + TranslationBase.of(context).kg + " ",
                            style: TextStyle(color: Colors.red),
                          ),
                        ),
                      ],
                    ),
                    Icon(
                      Icons.arrow_forward,
                      color: Colors.red,
                      size: 55.0,
                    ),
                    Row(
                      children: [
                        Texts(
                          (mixRange / 2.2).toStringAsFixed(1),
                          fontSize: 30.0,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 8.0, left: 4.0),
                          child: Text(
                            " " + TranslationBase.of(context).kg + " ",
                            style: TextStyle(color: Colors.red),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
                mHeight(12),
                overWeightBy >= 0 && overWeightBy <= 10
                    ? Column(
                        children: [
                          Texts(
                            TranslationBase.of(context).currentWeightPerfect,
                            fontSize: 20.0,
                          ),
                        ],
                      )
                    : overWeightBy > 10 && overWeightBy < 17
                        ? Column(
                            children: [
                              Texts(TranslationBase.of(context).littleBitWeightMore),
                              Texts(overWeightBy.toStringAsFixed(1)),
                              Texts(TranslationBase.of(context).consultWithDoctor),
                            ],
                          )
                        : overWeightBy >= 18
                            ? Container(
                                child: Column(
                                  children: [
                                    Texts(
                                      TranslationBase.of(context).excessiveObesity,
                                    ),
                                    SizedBox(
                                      height: 12.0,
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        Texts(
                                          (overWeightBy / 2.2).toStringAsFixed(1),
                                          fontSize: 30.0,
                                        ),
                                        Padding(
                                          padding: EdgeInsets.only(top: 8.0, left: 4.0),
                                          child: Text(
                                            " " + TranslationBase.of(context).kg + " ",
                                            style: TextStyle(color: Colors.red),
                                          ),
                                        ),
                                      ],
                                    ),
                                    SizedBox(
                                      height: 12.0,
                                    ),
                                    Texts(TranslationBase.of(context).mayWish),
                                  ],
                                ),
                              )
                            : overWeightBy < -18
                                ? Column(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.all(8.0),
                                        child: Texts(
                                          TranslationBase.of(context).underWeight,
                                          fontSize: 18.0,
                                        ),
                                      ),
                                      SizedBox(
                                        height: 12.0,
                                      ),
                                      Text(
                                        overWeightBy.toStringAsFixed(1),
                                        style: TextStyle(
                                          fontSize: 17,
                                          fontWeight: FontWeight.bold,
                                          letterSpacing: -1.34,
                                        ),
                                      ),
                                    ],
                                  )
                                : Container(
                                    child: Column(
                                      children: [
                                        Text(
                                          TranslationBase.of(context).underWeight,
                                          style: TextStyle(
                                            fontSize: 17,
                                            fontWeight: FontWeight.bold,
                                            letterSpacing: -1.34,
                                          ),
                                        ),
                                        SizedBox(
                                          height: 12.0,
                                        ),
                                        Text(
                                          overWeightBy.toStringAsFixed(1),
                                          style: TextStyle(
                                            fontSize: 17,
                                            fontWeight: FontWeight.bold,
                                            letterSpacing: -1.34,
                                          ),
                                        ),
                                        SizedBox(
                                          height: 12.0,
                                        ),
                                        Texts(TranslationBase.of(context).mayWish),
                                      ],
                                    ),
                                  ),
              ],
            ).withBorderedContainer,
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
            color: Colors.white,
            child: SecondaryButton(
              label: TranslationBase.of(context).viewDocList,
              color: CustomColors.accentColor,
              onTap: () {
                getDoctorsList(context);
              },
            ),
          ),
        ],
      ),
    );
  }

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

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

    service.getCalculationDoctors(calculationID: 4).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);
    });
  }
}

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