import 'package:hmg_patient_app/core/service/AuthenticatedUserObject.dart';
import 'package:hmg_patient_app/core/viewModels/TermsConditionsViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/appointment_rate_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Appointments/toDoCountProviderModel.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_admission_info_response_model.dart';
import 'package:hmg_patient_app/models/InPatientServices/get_admission_request_info_response_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/landing/landing_page.dart';
import 'package:hmg_patient_app/pages/rateAppointment/rate_appointment_doctor.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:hmg_patient_app/services/clinic_services/get_clinic_service.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/Loader/gif_loader_container.dart';
import 'package:hmg_patient_app/widgets/buttons/custom_text_button.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';
import 'package:webview_flutter/webview_flutter.dart';

class UserLoginAgreementPage extends StatefulWidget {
  final String userAgreementText;
  final AuthenticatedUserObject authenticatedUserObject;
  final AppointmentRateViewModel appointmentRateViewModel;
  final int selectedOption;
  final isArabic;

  UserLoginAgreementPage({required this.userAgreementText, required this.authenticatedUserObject, required this.appointmentRateViewModel, required this.selectedOption, required this.isArabic});

  @override
  State<UserLoginAgreementPage> createState() => _UserLoginAgreementPageState();
}

class _UserLoginAgreementPageState extends State<UserLoginAgreementPage> {
  late ProjectViewModel projectViewModel;
  late ToDoCountProviderModel toDoProvider;
  final authService = AuthProvider();

  late final WebViewController _controller;
  bool isPageLoaded = true;

  bool isTermsAndConditionsPage = true;
  bool acceptTerms = false;

  @override
  void initState() {
    super.initState();
    _controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
            print("Progress: $progress");
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            setState(() {
              isPageLoaded = true;
            });
          },
          onHttpError: (HttpResponseError error) {},
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            if (request.url.startsWith('https://www.youtube.com/')) {
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.isArabic ? "https://hmg.com/ar/Pages/MBTerms.aspx" : "https://hmg.com/en/Pages/MBTerms.aspx"));
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    toDoProvider = Provider.of<ToDoCountProviderModel>(context);
    return BaseView<TermsConditionsViewModel>(
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        showNewAppBarTitle: true,
        showNewAppBar: true,
        isShowDecPage: false,
        appBarTitle: TranslationBase.of(context).userAgreement,
        body:
            // isTermsAndConditionsPage
            // ?
            // getTermsAndConditionsContent(),
            // :
            isPageLoaded
                ? WebViewWidget(controller: _controller)
                : Container(
                    child: Center(
                      child: GifLoaderContainer(
                        barrierDismissible: false,
                      ),
                    ),
                  ),
        // Padding(
        //   padding: const EdgeInsets.all(8.0),
        //   child: SingleChildScrollView(child: WebViewWidget(controller: _controller)
        //       // Html(
        //       //   data: widget.userAgreementText,
        //       // ),
        //       ),
        // ),
        bottomSheet: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10), bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.5),
                spreadRadius: 5,
                blurRadius: 7,
                offset: Offset(0, 3), // changes position of shadow
              ),
            ],
          ),
          padding: EdgeInsets.only(left: 21, right: 21, top: 15, bottom: 15),
          width: double.infinity,
          // color: Colors.white,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(height: 12),
              Text(
                TranslationBase.of(context).continueAgreeTerms,
                style: TextStyle(
                  fontSize: 16.0,
                  fontWeight: FontWeight.w600,
                  color: Color(0xff2B353E),
                  letterSpacing: -0.64,
                ),
              ),
              mHeight(24.0),
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Checkbox(
                      value: acceptTerms,
                      onChanged: (v) {
                        setState(() => acceptTerms = v!);
                      }),
                  Expanded(
                    child: Text(
                      TranslationBase.of(context).termsConditionsRead,
                      style: TextStyle(
                          fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 12),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: ButtonTheme(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      height: 45.0,
                      child: CustomTextButton(
                        backgroundColor: CustomColors.green,
                        elevation: 0,
                        onPressed: isPageLoaded
                            ? () {
                                // if (isTermsAndConditionsPage) {
                                if (acceptTerms) {
                                  addUsageAgreement();
                                  // setState(() {
                                  //   isTermsAndConditionsPage = false;
                                  // });
                                } else {
                                  AppToast.showErrorToast(message: TranslationBase.of(context).pleaseAcceptTerms);
                                }
                                // } else {
                                //   addUsageAgreement();
                                // }
                              }
                            : null,
                        child: Text(TranslationBase.of(context).acceptLbl.toUpperCase(),
                            style: TextStyle(
                              fontSize: 18.0,
                              color: Colors.white,
                            )),
                      ),
                    ),
                  ),
                  mWidth(7),
                  Expanded(
                    flex: 1,
                    child: ButtonTheme(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      height: 45.0,
                      child: CustomTextButton(
                        backgroundColor: Color(0xffc5272d),
                        elevation: 0,
                        onPressed: isPageLoaded
                            ? () {
                                Navigator.pushAndRemoveUntil(
                                    context,
                                    FadePage(
                                      page: LandingPage(),
                                    ),
                                    (r) => false);
                              }
                            : null,
                        child: Text(TranslationBase.of(context).declineLbl.toUpperCase(), style: TextStyle(fontSize: 18.0, color: Colors.white)),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget getTermsAndConditionsContent() {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              TranslationBase.of(context).termsHeading,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions1,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions2,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions3,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            // InkWell(
            //   onTap: () async {
            //     await launchUrl(uri);
            //     Uri.parse(widget.isArabic ? "https://hmg.com/ar/Pages/Privacy.aspx" : "https://hmg.com/en/Pages/Privacy.aspx");
            //   },
            //   child: Text(
            //     TranslationBase.of(context).clickPrivacyPolicy,
            //     style: TextStyle(
            //         fontSize: 16,
            //         fontWeight: FontWeight.bold,
            //         fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'),
            //         color: Colors.blue,
            //         letterSpacing: -1.44,
            //         height: 35 / 24,
            //         decoration: TextDecoration.underline),
            //   ),
            // ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions4,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions5,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading2,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions6,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading3,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions7,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions8,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions9,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions10,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions11,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions12,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions12a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions12b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions12c,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions13,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions14,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions14a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions14b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading4,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions15,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions16,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions16a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions16b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions17,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions18,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions19,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions19a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions19b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading5,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions20,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions21,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading6,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions21,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions22,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions23,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions24,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading7,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions25,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions25a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions25b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions25c,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions26,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions27,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions28,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions29,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions30,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions31,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions32,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading8,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions33,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions33a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions33b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34c,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34d,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions34e,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35c,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35d,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions35e,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions36,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions37,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions38,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions39,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions39a,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions39b,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions39c,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions39d,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading9,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions40,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions41,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading10,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions42,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions43,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading11,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions44,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsHeading12,
              style:
                  TextStyle(fontSize: 20, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(12.0),
            Text(
              TranslationBase.of(context).termsConditions45,
              style: TextStyle(fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
            ),
            mHeight(24.0),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Checkbox(
                    value: acceptTerms,
                    onChanged: (v) {
                      setState(() => acceptTerms = v!);
                    }),
                Expanded(
                  child: Text(
                    TranslationBase.of(context).termsConditionsRead,
                    style: TextStyle(
                        fontSize: 16, fontFamily: (projectViewModel.isArabic ? 'Cairo' : 'Poppins'), fontWeight: FontWeight.w700, color: Color(0xff2B353E), letterSpacing: -1.44, height: 35 / 24),
                  ),
                ),
              ],
            ),
            mHeight(150.0),
          ],
        ),
      ),
    );
  }

  addUsageAgreement() {
    GifLoaderDialogUtils.showMyDialog(context);
    this.authService.addUserAgreementContent().then((result) {
      goToHome();
    }).catchError((err) {
      GifLoaderDialogUtils.hideDialog(context);
      AppToast.showErrorToast(message: err);
      print(err);
    });
  }

  checkIfIsInPatient() {
    bool isAdmitted = false;
    bool hasAdmissionRequest = false;
    GetAdmissionInfoResponseModel getAdmissionInfoResponseModel;
    GetAdmissionRequestInfoResponseModel getAdmissionRequestInfoResponseModel;
    ClinicListService service = new ClinicListService();
    service.checkIfInPatientAPI(context).then((res) {
      if (res['MessageStatus'] == 1) {
        isAdmitted = res['isAdmitted'];
        hasAdmissionRequest = res['hasAdmissionRequests'];
        print("IS ADMITTED: $isAdmitted");
        print("Has Admission Request: $hasAdmissionRequest");
        if (isAdmitted) {
          if (res['PatientAdmittedInformation'].length != 0) {
            getAdmissionInfoResponseModel = GetAdmissionInfoResponseModel.fromJson(res['PatientAdmittedInformation'][0]);
            projectViewModel.setInPatientProjectID(res['PatientAdmittedInformation'][0]['ProjectID']);
            projectViewModel.setInPatientAdmissionInfo(getAdmissionInfoResponseModel);
            projectViewModel.setIsPatientAdmitted(true);
          }
        }
        if (hasAdmissionRequest) {
          if (res['MedicalInstruction'].length != 0) {
            getAdmissionRequestInfoResponseModel = GetAdmissionRequestInfoResponseModel.fromJson(res['MedicalInstruction'][0]);
            // projectViewModel.setInPatientProjectID(res['MedicalInstruction'][0]['ProjectID']);
            projectViewModel.setInPatientProjectID(res['MedicalInstruction'][0]['projectId']);
            projectViewModel.setInPatientAdmissionRequest(getAdmissionRequestInfoResponseModel);
            projectViewModel.setPatientHasAdmissionRequest(true);
          }
        }
      } else {}
    });
  }

  goToHome() async {
    widget.authenticatedUserObject.isLogin = true;
    widget.appointmentRateViewModel.isLogin = true;
    projectViewModel.isLogin = true;
    projectViewModel.user = widget.authenticatedUserObject.user;
    await widget.authenticatedUserObject.getUser(getUser: true);

    // GifLoaderDialogUtils.hideDialog(context);
    getToDoCount();
    checkIfIsInPatient();
    widget.appointmentRateViewModel
        .getIsLastAppointmentRatedList(projectViewModel.isArabic ? 1 : 2)
        .then((value) => {
              GifLoaderDialogUtils.hideDialog(context),
              if (widget.appointmentRateViewModel.isHaveAppointmentNotRate)
                {
                  Navigator.pushAndRemoveUntil(
                      context,
                      FadePage(
                        page: RateAppointmentDoctor(),
                      ),
                      (r) => false)
                }
              else
                {
                  GifLoaderDialogUtils.hideDialog(context),
                  Navigator.pushAndRemoveUntil(
                      context,
                      FadePage(
                        page: LandingPage(),
                      ),
                      (r) => false)
                },
              insertIMEI()
            })
        .catchError((err) {
      print(err);
    });
  }

  insertIMEI() {
    authService.insertDeviceImei(widget.selectedOption).then((value) => {}).catchError((err) {
      print(err);
    });
  }

  getToDoCount() {
    toDoProvider.setState(0, 0, true, "0");
    ClinicListService service = new ClinicListService();
    service.getActiveAppointmentNo(context).then((res) {
      if (res['MessageStatus'] == 1) {
        toDoProvider.setState(res['AppointmentActiveNumber'], res['AncillaryOrderListCount'], true, "0");
        // toDoProvider.setState(res['AppointmentActiveNumber'], true, "0");
      } else {}
    }).catchError((err) {
      print(err);
    });
  }
}
