import 'dart:io';

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/models/anicllary-orders/ancillary_order_list_model.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/ancillary-orders/ancillaryOrdersDetails.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/data_display/medical/doctor_card.dart';
import 'package:diplomaticquarterapp/widgets/in_app_browser/InAppBrowser.dart';
import 'package:diplomaticquarterapp/widgets/my_rich_text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_expandable_notifier.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_nfc_kit/flutter_nfc_kit.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<AncillaryOrdersListModel> ancillaryLists = [];
  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;

  String selectedPaymentMethod = "";
  String selectedInstallments = "";
  String tamaraPaymentStatus;
  String tamaraOrderID;

  bool dataLoaded = false;

  @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>[
                SingleChildScrollView(
                  child: Column(
                    children: [
                      Container(
                        margin: EdgeInsets.only(top: 12),
                        child: AppExpandableNotifier(
                          isExpand: true,
                          hasCounter: true,
                          counter: widget.appoList.length.toString(),
                          title: TranslationBase.of(context).appointments,
                          bodyWidget: 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: [
                                              Expanded(
                                                flex: 2,
                                                child: 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"),
                                                    ),
                                                    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} \n' +
                                                                          TranslationBase.of(context).upcomingTimeLeft,
                                                                      style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: CustomColors.accentColor, letterSpacing: -0.48))
                                                                  : Container();
                                                            },
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              Expanded(
                                                child: 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),
                                                        textAlign: TextAlign.center,
                                                        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.getDayMonthYearDateFormatted(DateUtil.convertStringToDate(widget.appoList[index].appointmentDate)) + " " + widget.appoList[index].startTime.substring(0, 5), projectViewModel.isArabic),

                                                    // Timezone changes
                                                    widget.appoList[index].isLiveCareAppointment
                                                        ? MyRichText(
                                                            TranslationBase.of(context).appointmentDate + ": ",
                                                            DateUtil.getDayMonthYearDateFormatted(DateUtil.convertStringToDate(widget.appoList[index].appointmentDate)) +
                                                                " " +
                                                                DateUtil.convertStringToDate(widget.appoList[index].appointmentDate).toString().split(" ")[1].substring(0, 5),
                                                            projectViewModel.isArabic)
                                                        : MyRichText(
                                                            TranslationBase.of(context).appointmentDate + ": ",
                                                            DateUtil.getDayMonthYearDateFormatted(DateUtil.convertStringToDate(widget.appoList[index].appointmentDate)) +
                                                                " " +
                                                                widget.appoList[index].startTime.substring(0, 5),
                                                            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(
                          margin: EdgeInsets.only(top: 12),
                          child: AppExpandableNotifier(
                            isExpand: true,
                            hasCounter: true,
                            counter: (widget.ancillaryLists.isNotEmpty) ? widget.ancillaryLists[0].ancillaryOrderList.length.toString() : "0",
                            title: TranslationBase.of(context).anicllaryOrders,
                            bodyWidget: widget.ancillaryLists.length != 0
                                ? Container(
                                    padding: EdgeInsets.all(12),
                                    child: ListView.separated(
                                      shrinkWrap: true,
                                      physics: NeverScrollableScrollPhysics(),
                                      reverse: true,
                                      itemBuilder: (context, index) {
                                        return DoctorCard(
                                          onTap: () => ancillaryOrdersDetails(widget.ancillaryLists[0].ancillaryOrderList[index], widget.ancillaryLists[0].projectID),
                                          isInOutPatient: true,
                                          name: TranslationBase.of(context).dr.toString() + " " + (widget.ancillaryLists[0].ancillaryOrderList[index].doctorName ?? ""),
                                          billNo: widget.ancillaryLists[0].ancillaryOrderList[index].orderNo.toString(),
                                          profileUrl: "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown.png",
                                          subName: widget.ancillaryLists[0].projectName,
                                          isLiveCareAppointment: false,
                                          date: DateUtil.convertStringToDate(widget.ancillaryLists[0].ancillaryOrderList[index].orderDate),
                                          isSortByClinic: true,
                                        );
                                      },
                                      itemCount: widget.ancillaryLists[0].ancillaryOrderList.length,
                                      separatorBuilder: (context, index) => SizedBox(height: 14),
                                    ),
                                  )
                                : 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: 20.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 {
    GAnalytics.APPOINTMENT_DETAIL_FLOW_TYPE = 'todo list';
    Navigator.push(context, FadePage(page: AppointmentDetails(appo: appo, parentIndex: appo.patientStatusType == 42 ? 1 : 0))).then((value) {
      getPatientAppointmentHistory();
    });
    projectViewModel.analytics.todoList.to_do_list_more_details(appo);
  }

  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));
            });
          }
          if (res['AncillaryOrderList'].length != 0) {
            res['AncillaryOrderList'].forEach((item) {
              widget.ancillaryLists.add(AncillaryOrdersListModel.fromJson(item));
            });
          }
          dataLoaded = true;
        });
      } else {
        AppToast.showErrorToast(message: res['ErrorEndUserMessage']);
      }
    }).catchError((err) {
      print(err);
      GifLoaderDialogUtils.hideDialog(context);
      err != null ?? AppToast.showErrorToast(message: err);
    });
    // getAncillaryOrders();
  }

  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;

    FlutterNfcKit.nfcAvailability.then((value) {
      if (value == NFCAvailability.available) {
        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;
        navigateToQR(context, "", patientShareResponse, appo);
        projectViewModel.analytics.todoList.to_do_list_check_in(appo);
      } else {
        Utils.showErrorToast(TranslationBase.of(context).NFCNotSupported);
      }
    });
    projectViewModel.analytics.todoList.to_do_list_check_in(appo);

    DoctorsListService service = new DoctorsListService();
    service.generateAppointmentQR(patientShareResponse, context).then((res) {}).catchError((err) {
      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) {
    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) {
        navigateToPaymentMethod(context, value, appo);
        projectViewModel.analytics.todoList.to_do_list_confirm_payment_details(appo);
      } else {
        projectViewModel.analytics.todoList.to_do_list_cancel_payment_details(appo);
      }
    });

    projectViewModel.analytics.todoList.to_do_list_pay_now(appo);
  }

  openPayment(List<String> paymentMethod, AuthenticatedUser authenticatedUser, num 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[0],
        authenticatedUser.patientType,
        authenticatedUser.firstName,
        authenticatedUser.patientID,
        authenticatedUser,
        widget.browser,
        appo.isLiveCareAppointment,
        "2",
        appo.isLiveCareAppointment ? widget.patientShareResponse.clinicID.toString() : "",
        context,
        appo.appointmentDate,
        appo.appointmentNo,
        appo.clinicID,
        appo.doctorID,
        paymentMethod[1]);
  }

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

    if (selectedPaymentMethod == "TAMARA") {
      if (Platform.isAndroid) {
        Uri uri = new Uri.dataFromString(url);
        tamaraPaymentStatus = uri.queryParameters['status'];
        tamaraOrderID = uri.queryParameters['AuthorizePaymentId'];
      } else {
        Uri uri = new Uri.dataFromString(url);
        tamaraPaymentStatus = uri.queryParameters['paymentStatus'];
        tamaraOrderID = uri.queryParameters['orderId'];
      }
    }

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

    // if(selectedPaymentMethod != "TAMARA") {
    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 (selectedPaymentMethod == "TAMARA") {
      checkTamaraPaymentStatus(Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), appo);
      // if (tamaraPaymentStatus != null && tamaraPaymentStatus.toLowerCase() == "approved") {
      //   updateTamaraRequestStatus("success", "14", Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), tamaraOrderID, num.parse(selectedInstallments), appo);
      // } else {
      //   updateTamaraRequestStatus("Failed", "00", Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), tamaraOrderID, num.parse(selectedInstallments), appo);
      // }
    } else {
      checkPaymentStatus(appo);
    }
  }

  checkTamaraPaymentStatus(String orderID, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service.getTamaraPaymentStatus(orderID).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (res["status"].toString().toLowerCase() == "success") {
        updateTamaraRequestStatus("success", "14", orderID, res["tamara_order_id"], num.parse(selectedInstallments), appo);
      } else {
        updateTamaraRequestStatus(
            "Failed", "00", Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), tamaraOrderID != null ? tamaraOrderID : "", num.parse(selectedInstallments), appo);
      }
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  addAdvancedNumberRequestTamara(String advanceNumber, String paymentReference, String appointmentID, AppoitmentAllHistoryResultList appo) {
    DoctorsListService service = new DoctorsListService();
    service.addAdvancedNumberRequest(advanceNumber, paymentReference, appointmentID, context).then((res) {}).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  updateTamaraRequestStatus(String responseMessage, String status, String clientRequestID, String tamaraOrderID, int selectedInstallments, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    try {
      DoctorsListService service = new DoctorsListService();
      service.updateTamaraRequestStatus(responseMessage, status, clientRequestID, tamaraOrderID, selectedInstallments).then((res) {
        GifLoaderDialogUtils.hideDialog(context);
        if (tamaraPaymentStatus.toLowerCase() == "approved") {
          markAppointmentForTamara(appo);
          addAdvancedNumberRequestTamara("Tamara-Advance-0000", tamaraOrderID, appo.appointmentNo.toString(), appo);
        }
      }).catchError((err) {
        print(err);
        AppToast.showErrorToast(message: err);
        GifLoaderDialogUtils.hideDialog(context);
      });
    } catch (err) {
      print(err);
    }
  }

  markAppointmentForTamara(AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    var apptData = {
      "AppointmentNo": appo.appointmentNo.toString(),
      "DoctorID": appo.doctorID.toString(),
      "ServiceID": appo.serviceID.toString(),
      "ProjectID": appo.projectID.toString(),
      "ClinicID": appo.clinicID.toString(),
      "AppointmentDate": appo.appointmentDate.toString(),
    };
    DoctorsListService service = new DoctorsListService();
    service.markAppointmentForTamara(appo.projectID, appo.appointmentNo.toString()).then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      if (appo.isLiveCareAppointment)
        addVIDARequestInsert("0", tamaraOrderID, appo);
      else
        getAppoQR(context, appo);
    }).catchError((err) {
      print(err);
      AppToast.showErrorToast(message: err);
      GifLoaderDialogUtils.hideDialog(context);
    });
  }

  checkPaymentStatus(AppoitmentAllHistoryResultList appo) {
    String txn_ref;
    num amount;
    String payment_method;
    final currency = projectViewModel.user.outSA == 0 ? "sar" : 'aed';
    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') {
        txn_ref = res['Merchant_Reference'];
        amount = res['Amount'];
        payment_method = res['PaymentMethod'];
        createAdvancePayment(res, appo);
        projectViewModel.analytics.appointment.payment_success(
            appointment_type: 'regular', payment_method: payment_method, clinic: appo.clinicName, hospital: appo.projectName, txn_amount: "$amount", txn_currency: currency, txn_number: txn_ref);
      } else {
        AppToast.showErrorToast(message: res['Response_Message']);
        projectViewModel.analytics.appointment.payment_fail(
            appointment_type: 'regular',
            payment_method: payment_method,
            clinic: appo.clinicName,
            hospital: appo.projectName,
            txn_amount: "$amount",
            txn_currency: currency,
            error_type: res['Response_Message']);
      }
    }).catchError((err) {
      if(mounted) 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, appo);
      else
        getAppoQR(context, appo);
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  addVIDARequestInsert(String advanceNumber, String paymentReference, AppoitmentAllHistoryResultList appo) {
    GifLoaderDialogUtils.showMyDialog(context);
    DoctorsListService service = new DoctorsListService();
    service
        .insertVIDARequest(appo.appointmentNo, appo.clinicID, appo.projectID, appo.serviceID, appo.doctorID, appo.appointmentDate,
            Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), context)
        .then((res) {
      GifLoaderDialogUtils.hideDialog(context);
      getPatientAppointmentHistory();
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      print(err);
    });
  }

  Future navigateToPaymentMethod(context, PatientShareResponse patientShareResponse, AppoitmentAllHistoryResultList appo) async {
    Navigator.push(
            context,
            FadePage(
                page: PaymentMethod(
                    onSelectedMethod: (String metohd, [String selectedInstallmentPlan]) {
                      setState(() {});
                    },
                    patientShare: widget.patientShareResponse.patientShareWithTax)))
        .then((value) {
      print(value);
      selectedPaymentMethod = value[0];
      selectedInstallments = value[1];
      getPatientAppointmentHistory();

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

  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) {
        AppToast.showSuccessToast(message: res['ErrorEndUserMessage']);
        if (appo.isLiveCareAppointment) {
          insertLiveCareVIDARequest(appo);
        } else {
          getPatientAppointmentHistory();
        }
        projectViewModel.analytics.todoList.to_do_list_confirm_appointment(appo);
      } 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, appo.appointmentDate,
            Utils.getAppointmentTransID(appo.projectID, appo.clinicID, appo.appointmentNo), 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);
    });
  }

  ancillaryOrdersDetails(item, projectId) {
    Navigator.push(
      context,
      FadePage(
        page: AnicllaryOrdersDetails(item.appointmentNo, item.orderNo, projectId),
      ),
    );
  }
}
