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

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

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

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

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

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

  @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();
                },
                onPharmacyClick: () {
                  getPharmacyToken(model);
                },
                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()));
  }
}
