import 'package:diplomaticquarterapp/analytics/google-analytics.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/ImagesInfo.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:diplomaticquarterapp/models/Appointments/DoctorListResponse.dart';
import 'package:diplomaticquarterapp/models/Appointments/OBGyneProcedureListResponse.dart';
import 'package:diplomaticquarterapp/models/Appointments/PatientShareResposne.dart';
import 'package:diplomaticquarterapp/models/Appointments/toDoCountProviderModel.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/QRCode.dart';
import 'package:diplomaticquarterapp/pages/BookAppointment/SearchResults.dart';
import 'package:diplomaticquarterapp/pages/MyAppointments/AppointmentDetails.dart';
import 'package:diplomaticquarterapp/pages/ToDoList/payment_method_select.dart';
import 'package:diplomaticquarterapp/pages/ToDoList/widgets/paymentDialog.dart';
import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/avatar/large_avatar.dart';
import 'package:diplomaticquarterapp/widgets/in_app_browser/InAppBrowser.dart';
import 'package:diplomaticquarterapp/widgets/my_rich_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:flutter_countdown_timer/countdown_timer_controller.dart';
import 'package:flutter_countdown_timer/current_remaining_time.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:rating_bar/rating_bar.dart';

class ToDo extends StatefulWidget {
  PatientShareResponse patientShareResponse;
  List<AppoitmentAllHistoryResultList> appoList = [];
  List<OBGyneProcedureListResponse> obGyneAppoList = [];
  var languageID;
  MyInAppBrowser browser;

  bool isShowAppBar = true;
  Function onBackClick;

  ToDo({@required this.isShowAppBar, this.onBackClick});

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

class _ToDoState extends State<ToDo> with SingleTickerProviderStateMixin {
  AppSharedPreferences sharedPref = AppSharedPreferences();

  List<ImagesInfo> imagesInfo = List();
  ToDoCountProviderModel toDoProvider;
  CountdownTimerController controller;

  ProjectViewModel projectViewModel;
  TabController _tabController;

  @override
  void initState() {
    widget.patientShareResponse = new PatientShareResponse();
    _tabController = TabController(length: 2, vsync: this);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getPatientAppointmentHistory();
    });
    super.initState();
    imagesInfo
        .add(ImagesInfo(imageEn: 'https://hmgwebservices.com/Images/MobileApp/images-info-home/todo/en/0.png', imageAr: 'https://hmgwebservices.com/Images/MobileApp/images-info-home/todo/ar/0.png'));
    int endTime = DateTime.now().millisecondsSinceEpoch + 1000 * 30;
    controller = CountdownTimerController(endTime: endTime);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    toDoProvider = Provider.of<ToDoCountProviderModel>(context);
    projectViewModel = Provider.of(context);
    return AppScaffold(
      appBarTitle: TranslationBase.of(context).todoList,
      imagesInfo: imagesInfo,
      isShowAppBar: widget.isShowAppBar,
      isShowDecPage: true,
      showNewAppBar: true,
      showNewAppBarTitle: true,
      icon: "assets/images/new/bottom_nav/todo.svg",
      description: TranslationBase.of(context).infoTodo,
      onTap: widget.onBackClick,
      backgroundColor: CustomColors.appBackgroudGrey2Color,
      body: Column(
        children: <Widget>[
          TabBar(
            controller: _tabController,
            indicatorWeight: 3.0,
            indicatorSize: TabBarIndicatorSize.tab,
            labelColor: Color(0xff2B353E),
            unselectedLabelColor: Color(0xff575757),
            labelPadding: EdgeInsets.only(top: 15, bottom: 13, left: 20, right: 20),
            labelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 16,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            unselectedLabelStyle: TextStyle(
              fontFamily: projectViewModel.isArabic ? 'Cairo' : 'Poppins',
              fontSize: 16,
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
            tabs: [Text(TranslationBase.of(context).appointments), Text(TranslationBase.of(context).orders)],
            onTap: (index) {
              if (index == 1) {
                getOBGyneOrdersList();
              }
            },
          ),
          Expanded(
            child: TabBarView(
              physics: BouncingScrollPhysics(),
              controller: _tabController,
              children: <Widget>[
                Container(
                  child: widget.appoList.length != 0 ? ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    physics: ScrollPhysics(),
                    padding: EdgeInsets.all(0.0),
                    itemCount: widget.appoList.length,
                    itemBuilder: (context, index) {
                      return Container(
                        width: double.infinity,
                        margin: EdgeInsets.only(left: 12.0, right: 12.0, top: 12.0),
                        decoration: cardRadius(12),
                        padding: EdgeInsets.all(16),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.only(top: 4.0),
                                      child: widget.appoList[index].clinicID == 265
                                          ? Container(
                                              margin: EdgeInsets.only(left: 5.0, right: 5.0),
                                              child: SvgPicture.asset("assets/images/new/CoronaIcon.svg", width: 35.0, height: 35.0),
                                            )
                                          : widget.appoList[index].isLiveCareAppointment
                                              ? SvgPicture.asset("assets/images/new/virtual.svg")
                                              : SvgPicture.asset("assets/images/new/hospital-visit.svg"),

                                      // SvgPicture.asset("assets/images/new/virtual.svg"),
                                    ),
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.only(left: 8.0, right: 8.0),
                                          child: Text(
                                              widget.appoList[index].clinicID == 265
                                                  ? TranslationBase.of(context).covidTestTodo
                                                  : widget.appoList[index].isLiveCareAppointment
                                                      ? TranslationBase.of(context).liveCareAppo
                                                      : TranslationBase.of(context).walkinAppo,
                                              style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48)),
                                        ),
                                        Padding(
                                          padding: const EdgeInsets.only(left: 8.0, right: 8.0),
                                          child: CountdownTimer(
                                            controller: new CountdownTimerController(endTime: DateTime.now().millisecondsSinceEpoch + (widget.appoList[index].remaniningHoursTocanPay * 1000) * 60),
                                            widgetBuilder: (_, CurrentRemainingTime time) {
                                              return time != null
                                                  ? Text(
                                                      '${time.days != null ? time.days : "0"}:${time.hours != null ? time.hours.toString().length == 1 ? "0" + time.hours.toString() : time.hours : "00"}:${time.min}:${time.sec} ' +
                                                          TranslationBase.of(context).upcomingTimeLeft,
                                                      style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: CustomColors.accentColor, letterSpacing: -0.48))
                                                  : Container();
                                            },
                                          ),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                                Container(
                                  child: InkWell(
                                    onTap: () {
                                      performNextAction(widget.appoList[index]);
                                    },
                                    child: Container(
                                      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 14),
                                      decoration: BoxDecoration(
                                        color: getNextActionButtonColor(widget.appoList[index].nextAction),
                                        border: Border.all(color: Colors.white, width: 1),
                                        borderRadius: BorderRadius.circular(6),
                                      ),
                                      child: Text(
                                        getNextActionText(widget.appoList[index].nextAction),
                                        style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Colors.white, letterSpacing: -0.4),
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            Padding(
                              padding: const EdgeInsets.only(top: 8.0),
                              child: Text(
                                widget.appoList[index].doctorTitle + " " + widget.appoList[index].doctorNameObj,
                                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.64, height: 25 / 16),
                              ),
                            ),
                            Row(
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                LargeAvatar(
                                  name: widget.appoList[index].doctorTitle + " " + widget.appoList[index].doctorNameObj,
                                  url: widget.appoList[index].doctorImageURL,
                                  width: 52,
                                  height: 52,
                                ),
                                SizedBox(width: 11),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisSize: MainAxisSize.min,
                                    children: <Widget>[
                                      MyRichText(TranslationBase.of(context).clinic + ": ", widget.appoList[index].clinicName, projectViewModel.isArabic),
                                      MyRichText(TranslationBase.of(context).appointmentDate + ": ",
                                          DateUtil.getDayMonthYearHourMinuteDateFormatted(DateUtil.convertStringToDate(widget.appoList[index].appointmentDate)), projectViewModel.isArabic),
                                      MyRichText(TranslationBase.of(context).branch, widget.appoList[index].projectName, projectViewModel.isArabic),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        mainAxisSize: MainAxisSize.max,
                                        children: <Widget>[
                                          RatingBar.readOnly(
                                            initialRating: widget.appoList[index].actualDoctorRate.toDouble(),
                                            size: 16.0,
                                            filledColor: Color(0XFFD02127),
                                            emptyColor: Color(0XFFD02127),
                                            isHalfAllowed: true,
                                            halfFilledIcon: Icons.star_half,
                                            filledIcon: Icons.star,
                                            emptyIcon: Icons.star_border,
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                            Padding(
                              padding: const EdgeInsets.only(top: 12.0),
                              child: Text(
                                getNextActionDescription(widget.appoList[index].nextAction),
                                style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.48, height: 25 / 16),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                navigateToAppointmentDetails(context, widget.appoList[index]);
                              },
                              child: Padding(
                                padding: const EdgeInsets.only(top: 0.0),
                                child: Text(
                                  TranslationBase.of(context).moreDetails,
                                  style: TextStyle(
                                      fontSize: 10, fontWeight: FontWeight.w600, color: CustomColors.accentColor, letterSpacing: -0.48, height: 25 / 16, decoration: TextDecoration.underline),
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  ) : getNoDataWidget(context),
                ),
                Container(
                  child: widget.obGyneAppoList.length != 0 ? ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    physics: ScrollPhysics(),
                    padding: EdgeInsets.all(0.0),
                    itemCount: widget.obGyneAppoList.length,
                    itemBuilder: (context, index) {
                      return Container(
                        width: double.infinity,
                        margin: EdgeInsets.only(left: 12.0, right: 12.0, top: 12.0),
                        decoration: cardRadius(12),
                        padding: EdgeInsets.all(16),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.only(top: 0.0),
                                      child: Container(
                                        margin: EdgeInsets.only(left: 5.0, right: 5.0),
                                        child: SvgPicture.asset("assets/images/new/ultrasound.svg", width: 20.0, height: 20.0),
                                      ),
                                    ),
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.only(left: 8.0, right: 8.0),
                                          child: Text(TranslationBase.of(context).walkinAppo,
                                              style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Color(0xff2B353E), letterSpacing: -0.48)),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                                Container(
                                  child: InkWell(
                                    onTap: () {
                                      getOBGyneDoctorsList(widget.obGyneAppoList[index].projectID, widget.obGyneAppoList[index].setupID, widget.obGyneAppoList[index]);
                                    },
                                    child: Container(
                                      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 14),
                                      decoration: BoxDecoration(
                                        color: CustomColors.green,
                                        border: Border.all(color: Colors.white, width: 1),
                                        borderRadius: BorderRadius.circular(6),
                                      ),
                                      child: Text(
                                        TranslationBase.of(context).bookNow,
                                        style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Colors.white, letterSpacing: -0.4),
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            Padding(
                              padding: const EdgeInsets.only(top: 8.0),
                              child: Text(
                                TranslationBase.of(context).dr + " " + widget.obGyneAppoList[index].doctorName,
                                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Color(0xff2E303A), letterSpacing: -0.64, height: 25 / 16),
                              ),
                            ),
                            Row(
                              mainAxisSize: MainAxisSize.min,
                              children: <Widget>[
                                LargeAvatar(
                                  name: TranslationBase.of(context).dr + " " + widget.obGyneAppoList[index].doctorName,
                                  url: "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown.png",
                                  width: 52,
                                  height: 52,
                                ),
                                SizedBox(width: 11),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisSize: MainAxisSize.min,
                                    children: <Widget>[
                                      MyRichText(TranslationBase.of(context).clinic + ": ", widget.obGyneAppoList[index].clinicDescription, projectViewModel.isArabic),
                                      MyRichText(TranslationBase.of(context).orderDate + ": ",
                                          DateUtil.getDayMonthYearHourMinuteDateFormatted(DateUtil.convertStringToDate(widget.obGyneAppoList[index].orderDate)).split(" ")[0], projectViewModel.isArabic),
                                      MyRichText(TranslationBase.of(context).branch, widget.obGyneAppoList[index].projectDescription, projectViewModel.isArabic),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      );
                    },
                  ) : getNoDataWidget(context),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 120.0,
          ),
        ],
      ),
    );
  }

  String getNextActionImage(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return "assets/images/new-design/confirm_button.png";
        break;

      case 15:
        return projectViewModel.isArabic ? "assets/images/new-design/pay_online_button_arabic_disabled.png" : "assets/images/new-design/pay_online_button_disabled.png";
        break;

      case 20:
        return projectViewModel.isArabic ? "assets/images/new-design/pay_online_button_arabic.png" : "assets/images/new-design/pay_online_button.png";
        break;

      case 30:
        return "assets/images/new-design/NFCIcon_option2.png";
        break;

      case 40:
        return "assets/images/new-design/video_call_instruction.png";
        break;

      case 50:
        return "assets/images/new-design/confirm_button.png";
        break;

      case 60:
        return "assets/images/new-design/waiting_for_doctor.png";
        break;

      case 90:
        return "assets/images/new-design/NFCIcon_option2.png";
        break;

      default:
        return "";
    }
  }

  performNextAction(AppoitmentAllHistoryResultList appo) {
    switch (appo.nextAction) {
      case 10:
        confirmAppointment(appo);
        break;
      case 20:
        getPatientShare(context, appo);
        break;
      case 30:
        getAppoQR(context, appo);
        break;
      case 50:
        confirmAppointment(appo);
        break;
      case 60:
        break;
      case 90:
        getAppoQR(context, appo);
        break;
    }
  }

  Color getNextActionButtonColor(nextAction) {
    switch (nextAction) {
      case 0:
        return CustomColors.accentColor;
        break;
      case 10:
        return CustomColors.green;
        break;

      case 15:
        return CustomColors.grey2;
        break;

      case 20:
        return CustomColors.green;
        break;

      case 30:
        return CustomColors.accentColor;
        break;

      case 40:
        return CustomColors.green;
        break;

      case 50:
        return CustomColors.green;
        break;

      case 60:
        return CustomColors.orange;
        break;

      case 90:
        return CustomColors.accentColor;
        break;

      default:
        return CustomColors.green;
    }
  }

  String getNextActionText(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return TranslationBase.of(context).confirm;
        break;

      case 15:
        return TranslationBase.of(context).pendingPayment;
        break;

      case 20:
        return TranslationBase.of(context).payNow;
        break;

      case 30:
        return TranslationBase.of(context).viewQR;
        break;

      case 40:
        return TranslationBase.of(context).instruction;
        break;

      case 50:
        return TranslationBase.of(context).confirmLiveCare;
        break;

      case 60:
        return TranslationBase.of(context).waitingForDoctor;
        break;

      case 90:
        return TranslationBase.of(context).checkinOptions;
        break;

      default:
        return "";
    }
  }

  String getNextActionDescription(nextAction) {
    switch (nextAction) {
      case 0:
        return "No Action";
        break;
      case 10:
        return TranslationBase.of(context).upcomingConfirm;
        break;

      case 15:
        return TranslationBase.of(context).upcomingPaymentPending;
        break;

      case 20:
        return TranslationBase.of(context).upcomingPaymentNow;
        break;

      case 30:
        return TranslationBase.of(context).upcomingQRNFC;
        break;

      case 90:
        return TranslationBase.of(context).upcomingQRNFC;
        break;

      case 40:
        return TranslationBase.of(context).upcomingVirtual;
        break;

      case 50:
        return TranslationBase.of(context).upcomingLivecare;
        break;

      case 60:
        return TranslationBase.of(context).waitingForDoctor;
        break;

      default:
        return "";
    }
  }

  getLanguageID() async {
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    setState(() {
      widget.languageID = languageID;
    });
  }

  String getDate(String date) {
    DateTime dateObj = DateUtil.convertStringToDate(date);
    return DateUtil.getWeekDay(dateObj.weekday) +
        ", " +
        dateObj.day.toString() +
        " " +
        DateUtil.getMonth(dateObj.month) +
        " " +
        dateObj.year.toString() +
        " " +
        dateObj.hour.toString() +
        ":" +
        getMinute(dateObj);
  }

  String getMinute(DateTime dateObj) {
    if (dateObj.minute == 0) {
      return dateObj.minute.toString() + "0";
    } else {
      return dateObj.minute.toString();
    }
  }

  String getDoctorSpeciality(List<String> docSpecial) {
    String docSpeciality = "";
    docSpecial.forEach((v) {
      docSpeciality = docSpeciality + v + "\n";
    });
    return docSpeciality;
  }

  Future navigateToAppointmentDetails(context, AppoitmentAllHistoryResultList appo) async {
    projectViewModel.analytics.todoList.to_do_list_more_details(appo);
    GAnalytics.APPOINTMENT_DETAIL_FLOW_TYPE = 'todo list';
    Navigator.push(context, FadePage(page: AppointmentDetails(appo: appo, parentIndex: appo.patientStatusType == 42 ? 1 : 0))).then((value) {
      getPatientAppointmentHistory();
    });
  }

  getOBGyneOrdersList() {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.getOBGyneOrdersList(context).then((res) {
      widget.obGyneAppoList.clear();
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          if (res['HIS_OBGYNEProcedureGet_List'].length != 0) {
            widget.obGyneAppoList.clear();
            res['HIS_OBGYNEProcedureGet_List'].forEach((v) {
              widget.obGyneAppoList.add(new OBGyneProcedureListResponse.fromJson(v));
            });
          } else {
            // Navigator.of(context).popAndPushNamed(HOME);
          }
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  getOBGyneDoctorsList(int projectID, String setupID, OBGyneProcedureListResponse obGyneProcedureListResponse) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    List<DoctorList> doctorsList = [];
    List<PatientDoctorAppointmentList> _patientDoctorAppointmentListHospital = [];
    service.getOBGyneDoctorsList(projectID, setupID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      print(res['HIS_ObgyneUltrasoundDoctorsList'][0]);
      if (res['MessageStatus'] == 1) {
        setState(() {
          if (res['HIS_ObgyneUltrasoundDoctorsList'].length != 0) {
            res['HIS_ObgyneUltrasoundDoctorsList'].forEach((v) {
              doctorsList.add(new DoctorList.fromJson(v));
            });
            print(doctorsList.length);
            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));
              }
            });
            navigateToSearchResults(context, doctorsList, _patientDoctorAppointmentListHospital, obGyneProcedureListResponse);
          } else {
            // Navigator.of(context).popAndPushNamed(HOME);
          }
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  navigateToSearchResults(context, List<DoctorList> docList, List<PatientDoctorAppointmentList> patientDoctorAppointmentListHospital, OBGyneProcedureListResponse obGyneProcedureListResponse) {
    Navigator.push(context, FadePage(page: SearchResults(isLiveCareAppointment: false, isObGyneAppointment: true, doctorsList: docList, patientDoctorAppointmentListHospital: patientDoctorAppointmentListHospital, obGyneProcedureListResponse: obGyneProcedureListResponse)));
  }

  getPatientAppointmentHistory() {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.getPatientAppointmentHistory(true, context).then((res) {
      widget.appoList.clear();
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        setState(() {
          if (res['AppoimentAllHistoryResultList'].length != 0) {
            widget.appoList.clear();
            res['AppoimentAllHistoryResultList'].forEach((v) {
              widget.appoList.add(new AppoitmentAllHistoryResultList.fromJson(v));
            });
          } else {
            // Navigator.of(context).popAndPushNamed(HOME);
          }
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
      err != null ?? AppToast.showErrorToast(message: err);
    });
  }

  getPatientShare(context, AppoitmentAllHistoryResultList appo) {
    DoctorsListService service = new DoctorsListService();
    if (appo.isLiveCareAppointment) {
      getLiveCareAppointmentPatientShare(context, service, appo);
    } else {
      GifLoaderDialogUtils.showMyDialog(context);
      service.getPatientShare(appo.appointmentNo.toString(), appo.clinicID, appo.projectID, context).then((res) {
        GifLoaderDialogUtils.hideDialog(context);
        widget.patientShareResponse = new PatientShareResponse.fromJson(res);
        openPaymentDialog(appo, widget.patientShareResponse);
      }).catchError((err) {
        GifLoaderDialogUtils.hideDialog(context);
        AppToast.showErrorToast(message: err);
        print(err);
      });
    }
  }

  getLiveCareAppointmentPatientShare(context, DoctorsListService service, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    service.getLiveCareAppointmentPatientShare(appo.appointmentNo.toString(), appo.clinicID, appo.projectID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      widget.patientShareResponse = new PatientShareResponse.fromJson(res);
      openPaymentDialog(appo, widget.patientShareResponse);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  getAppoQR(context, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    PatientShareResponse patientShareResponse = new PatientShareResponse();

    patientShareResponse.doctorNameObj = appo.doctorNameObj;
    patientShareResponse.doctorSpeciality = appo.doctorSpeciality;
    patientShareResponse.projectName = appo.projectName;
    patientShareResponse.appointmentDate = appo.appointmentDate;
    patientShareResponse.appointmentNo = appo.appointmentNo;
    patientShareResponse.clinicID = appo.clinicID;
    patientShareResponse.projectID = appo.projectID;
    patientShareResponse.isFollowup = appo.isFollowup;

    DoctorsListService service = new DoctorsListService();
    service.generateAppointmentQR(patientShareResponse, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      projectViewModel.analytics.todoList.to_do_list_check_in(appo);
      navigateToQR(context, res['AppointmentQR'], patientShareResponse, appo);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  Future navigateToQR(context, String appoQR, PatientShareResponse patientShareResponse, AppoitmentAllHistoryResultList appintment) async {
    Navigator.push(context, FadePage(page: QRCode(patientShareResponse: patientShareResponse, appoQR: appoQR, appointment: appintment))).then((value) {
      getPatientAppointmentHistory();
    });
  }

  openPaymentDialog(AppoitmentAllHistoryResultList appo, PatientShareResponse patientShareResponse) {
    projectViewModel.analytics.todoList.to_do_list_pay_now(appo);
    showGeneralDialog(
            barrierColor: Colors.black.withOpacity(0.5),
            transitionBuilder: (context, a1, a2, widget) {
              final curvedValue = Curves.easeInOutBack.transform(a1.value) - 1.0;
              return Transform(
                transform: Matrix4.translationValues(0.0, curvedValue * 200, 0.0),
                child: Opacity(
                  opacity: a1.value,
                  child: PaymentDialog(appo: appo, patientShareResponse: patientShareResponse),
                ),
              );
            },
            transitionDuration: Duration(milliseconds: 500),
            barrierDismissible: false,
            barrierLabel: '',
            context: context,
            pageBuilder: (context, animation1, animation2) {})
        .then((value) {
      if (value != null) {
        projectViewModel.analytics.todoList.to_do_list_confirm_payment_details(appo);
        navigateToPaymentMethod(context, value, appo);
      } else {
        projectViewModel.analytics.todoList.to_do_list_cancel_payment_details(appo);
      }
    });
  }

  openPayment(String paymentMethod, AuthenticatedUser authenticatedUser, double amount, PatientShareResponse patientShareResponse, AppoitmentAllHistoryResultList appo) {
    widget.browser = new MyInAppBrowser(onExitCallback: onBrowserExit, appo: appo, onLoadStartCallback: onBrowserLoadStart, context: context);

    widget.browser.openPaymentBrowser(
        amount,
        "Appointment check in",
        Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo),
        appo.projectID.toString(),
        authenticatedUser.emailAddress,
        paymentMethod,
        authenticatedUser.patientType,
        authenticatedUser.firstName,
        authenticatedUser.patientID,
        authenticatedUser,
        widget.browser,
        appo.isLiveCareAppointment,
        "2",
        "",
        appo.appointmentDate,
        appo.appointmentNo,
        appo.clinicID,
        appo.doctorID);
  }

  onBrowserLoadStart(String url) {
    print("onBrowserLoadStart");
    print(url);

    MyInAppBrowser.successURLS.forEach((element) {
      if (url.contains(element)) {
        if (widget.browser.isOpened()) widget.browser.close();
        MyInAppBrowser.isPaymentDone = true;
        return;
      }
    });

    MyInAppBrowser.errorURLS.forEach((element) {
      if (url.contains(element)) {
        if (widget.browser.isOpened()) widget.browser.close();
        MyInAppBrowser.isPaymentDone = false;
        return;
      }
    });
  }

  onBrowserExit(AppoitmentAllHistoryResultList appo, bool isPaymentMade) {
    print("onBrowserExit Called!!!!");
    if (isPaymentMade) checkPaymentStatus(appo);
  }

  checkPaymentStatus(AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.checkPaymentStatus(Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      String paymentInfo = res['Response_Message'];
      if (paymentInfo == 'Success') {
        createAdvancePayment(res, appo);
      } else {
        AppToast.showErrorToast(message: res['Response_Message']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  createAdvancePayment(res, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    String paymentReference = res['Fort_id'].toString();
    service.createAdvancePayment(appo, appo.projectID.toString(), res['Amount'], res['Fort_id'], res['PaymentMethod'], context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      addAdvancedNumberRequest(res['OnlineCheckInAppointments'][0]['AdvanceNumber'].toString(), paymentReference, appo.appointmentNo.toString(), appo, res['OnlineCheckInAppointments'][0]);
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
    });
  }

  addAdvancedNumberRequest(String advanceNumber, String paymentReference, String appointmentID, AppoitmentAllHistoryResultList appo, dynamic apptData) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.addAdvancedNumberRequest(advanceNumber, paymentReference, appointmentID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (appo.isLiveCareAppointment)
        addVIDARequestInsert(advanceNumber, paymentReference, apptData);
      else
        getAppoQR(context, appo);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  addVIDARequestInsert(String advanceNumber, String paymentReference, dynamic apptData) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.addVIDARequest(advanceNumber, paymentReference, apptData, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      print(res);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  Future navigateToPaymentMethod(context, PatientShareResponse patientShareResponse, AppoitmentAllHistoryResultList appo) async {
    Navigator.push(context, FadePage(page: PaymentMethod(onSelectedMethod: (String metohd) {
      setState(() {});
    }))).then((value) {
      print(value);
      getPatientAppointmentHistory();

      if (value != null){
        final appType = appo.isLiveCareAppointment ? 'livecare' : 'regular';
        projectViewModel.analytics.appointment.payment_method(appointment_type: appType, clinic: appo.clinicName, payment_method: value, payment_type: 'appointment');
        openPayment(value, projectViewModel.user, double.parse(patientShareResponse.patientShareWithTax.toString()), patientShareResponse, appo);
      }
    });
  }

  confirmAppointment(AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.confirmAppointment(appo.appointmentNo, appo.clinicID, appo.projectID, appo.isLiveCareAppointment, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        projectViewModel.analytics.todoList.to_do_list_confirm_appointment(appo);
        AppToast.showSuccessToast(message: res['ErrorEndUserMessage']);
        if (appo.isLiveCareAppointment) {
          insertLiveCareVIDARequest(appo);
        } else {
          getPatientAppointmentHistory();
        }
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  insertLiveCareVIDARequest(AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.insertVIDARequest(appo.appointmentNo, appo.clinicID, appo.projectID, appo.serviceID, appo.doctorID, context).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res['MessageStatus'] == 1) {
        AppToast.showSuccessToast(message: res['ErrorEndUserMessage']);
        getPatientAppointmentHistory();
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
    });
  }
}
