import 'dart:collection';

import 'package:auto_size_text/auto_size_text.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/health_calculator/bmi_calculator/bariatrics-screen.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/SearchResults.dart';
import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.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/defaultButton.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:flutter_svg/svg.dart';

class ResultPage extends StatefulWidget {
  final double finalResult;
  final String textResult;
  final String msg;

  ResultPage({this.finalResult, this.textResult, this.msg});

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

class _ResultPageState extends State<ResultPage> {
  Color inductorColor;

  double percent;

  Color colorInductor() {
    if (widget.finalResult >= 30) {
      inductorColor = Color(0xffC70D00);
    } else if (widget.finalResult < 30 && widget.finalResult >= 25) {
      inductorColor = Color(0xffC25400);
    } else if (widget.finalResult < 25 && widget.finalResult >= 18.5) {
      inductorColor = Color(0xff36D600);
    } else if (widget.finalResult < 18.5) {
      inductorColor = Color(0xff1BE0EE);
    }
    return inductorColor;
  }

  double percentInductor() {
    if (widget.finalResult >= 30) {
      percent = 1.0;
    } else if (widget.finalResult < 30 && widget.finalResult >= 25) {
      percent = 0.73;
    } else if (widget.finalResult < 25 && widget.finalResult >= 18.5) {
      percent = 0.5;
    } else if (widget.finalResult < 18.5) {
      percent = 0.25;
    }
    return percent;
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowDecPage: false,
      isShowAppBar: true,
      showNewAppBarTitle: true,
      showNewAppBar: true,
      appBarTitle: "${TranslationBase.of(context).bmi} ${TranslationBase.of(context).calcHealth}",
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 6),
            decoration: cardRadius(12),
            child: Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    TranslationBase.of(context).bodyMassIndex + widget.finalResult.toStringAsFixed(2),
                    style: TextStyle(
                      fontSize: 16,
                      letterSpacing: -0.64,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                  mHeight(20),
                  Container(
                    height: MediaQuery.of(context).size.width / 2.8,
                    child: Row(
                      children: [
                        showMass(context, TranslationBase.of(context).underWeight, "< 18.5", widget.finalResult <= 18.5 ? Colors.red : Colors.black, 8),
                        mWidth(12),
                        showMass(context, TranslationBase.of(context).healthy, "18.5 - 24.9", (widget.finalResult > 18.5 && widget.finalResult < 25) ? Colors.red : Colors.black, 6),
                        mWidth(12),
                        showMass(context, TranslationBase.of(context).overWeight, "25 - 29.9", (widget.finalResult >= 25 && widget.finalResult < 30) ? Colors.red : Colors.black, 4),
                        mWidth(12),
                        showMass(context, TranslationBase.of(context).obese, "30 - 34.9", (widget.finalResult >= 30 && widget.finalResult < 35) ? Colors.red : Colors.black, 2),
                        mWidth(12),
                        showMass(context, TranslationBase.of(context).extremeObese, "> 35", (widget.finalResult >= 35) ? Colors.red : Colors.black, 0),
                      ],
                    ),
                  ),
                  mHeight(20),
                  Text(
                    widget.textResult,
                    style: TextStyle(
                      fontSize: 16.0,
                      fontWeight: FontWeight.w600,
                      letterSpacing: -0.56,
                    ),
                  ),
                  mHeight(4),
                  Text(
                    widget.msg,
                    style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w600, letterSpacing: -0.56, color: CustomColors.textColor),
                  ),
                ],
              ),
            ),
          ),
          mFlex(1),
          Container(
            color: Colors.white,
            padding: EdgeInsets.all(16),
            child: DefaultButton(
              TranslationBase.of(context).seeListOfDoctor,
              () {
                callDoctorsSearchAPI();
              },
            ),
          ),
        ],
      ),
    );
  }

  callDoctorsSearchAPI() {
    GifLoaderDialogUtils.showMyDialog(context);
    List<DoctorList> doctorsList = [];
    List<String> arr = [];
    List<String> arrDistance = [];
    List<String> result;
    int numAll;
    List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital = List();

    DoctorsListService service = new DoctorsListService();
    service.getDoctorsList(108, 0, false, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          if (res['DoctorList'].length != 0) {
            doctorsList.clear();
            res['DoctorList'].forEach((v) {
              doctorsList.add(new DoctorList.fromJson(v));
            });
            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: element.projectDistanceInKiloMeters.toString(), patientDoctorAppointment: element));
              }
            });
          } else {}
        });

        result = LinkedHashSet<String>.from(arr).toList();
        numAll = result.length;
        navigateToSearchResults(context, doctorsList, _patientDoctorAppointmentListHospital);
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
      AppToast.showErrorToast(message: err);
    });
  }

  Future navigateToSearchResults(context, List<DoctorList> docList, List<PatientDoctorAppointmentList> patientDoctorAppointmentListHospital) async {
    Navigator.push(context, FadePage(page: SearchResults(isLiveCareAppointment: false, doctorsList: docList, patientDoctorAppointmentListHospital: patientDoctorAppointmentListHospital)))
        .then((value) {
      setState(() {
        // dropdownValue = null;
      });
      // getProjectsList();
    });
  }

  Widget showMass(BuildContext context, String title, String weight, Color color, int f) {
    return Expanded(
      flex: 1,
      child: Container(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            Flexible(
              child: Row(
                children: [
                  mFlex(f),
                  Flexible(
                    flex: 10,
                    child: SvgPicture.asset(
                      "assets/images/new/mass/health_BMI.svg",
                      height: double.infinity,
                      width: double.infinity,
                      fit: BoxFit.fill,
                      color: color,
                    ),
                  ),
                  mFlex(f),
                ],
              ),
            ),
            mHeight(20),
            AutoSizeText(
              title,
              maxLines: 1,
              minFontSize: 6,
              style: TextStyle(
                color: color,
                fontSize: 10,
                letterSpacing: -0.6,
              ),
            ),
            AutoSizeText(
              weight,
              maxLines: 1,
              minFontSize: 6,
              style: TextStyle(
                color: color,
                fontSize: 10,
                letterSpacing: -0.6,
              ),
            )
          ],
        ),
      ),
    );
  }
}
