import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/viewModels/dashboard_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/pharmacy_module_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/locator.dart';
import 'package:hmg_patient_app/pages/Covid-DriveThru/covid-drivethru-location.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/conference/zoom/call_screen.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';

import 'fragments/home_page_fragment2.dart';
import 'landing_page_pharmcy.dart';

class HomePage2 extends StatefulWidget {
  final Function? goToMyProfile;
  VoidCallback? onLoginClick, onMedicalFileClick;

  HomePage2({Key? key, this.goToMyProfile, this.onLoginClick, this.onMedicalFileClick});

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

class _HomePageState2 extends State<HomePage2> {
  PharmacyModuleViewModel pharmacyModuleViewModel = locator<PharmacyModuleViewModel>();
  late ProjectViewModel projectViewModel;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);

    return BaseView<DashboardViewModel>(
      onModelReady: (model) => model.getPatientRadOrders(),
      builder: (_, model, wi) => Container(
        color: CustomColors.backgroudGreyColor,
        // color: Colors.yellow,
        child: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            children: [
              HomePageFragment2(
                model,
                onLoginClick: () {
                  widget.onLoginClick!();
                  // FirebaseCrashlytics.instance.crash();
                  // Navigator.pushNamed(
                  //   context,
                  //   "zoom_call_page",
                  //   arguments: CallArguments("h9qkpf02", "123", "Patient", "40", "1", false),
                  // );
                },
                onPharmacyClick: () {
                  // getPharmacyToken(model);
                  Uri uri = Uri.parse(PHARMACY_REDIRECT_URL);
                  launchUrl(uri, mode: LaunchMode.externalApplication);
                },
                onMedicalFileClick: () {
                  widget.onMedicalFileClick!();
                },
              )
            ],
          ),
        ),
      ),
    );
  }

  getPharmacyToken(DashboardViewModel model) async {
    if (!model.isLogin) {
      Navigator.push(context, FadePage(page: LandingPagePharmacy()));
    } else {
      GifLoaderDialogUtils.showMyDialog(context);
      await pharmacyModuleViewModel.generatePharmacyToken().then((value) async {
        if (!pharmacyModuleViewModel.error.isNotEmpty) {
          await pharmacyModuleViewModel.createUser().then((value) {
            GifLoaderDialogUtils.hideDialog(context);
            Navigator.push(context, FadePage(page: LandingPagePharmacy()));
          });
        } else {
          GifLoaderDialogUtils.hideDialog(context);
          Navigator.push(context, FadePage(page: LandingPagePharmacy()));
        }
      });
    }
  }

  navigateToCovidDriveThru() {
    Navigator.push(context, FadePage(page: CovidDrivethruLocation()));
  }
}
