import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/pharmacies/PharmacyProduct.dart';
import 'package:hmg_patient_app/core/service/client/base_app_client.dart';
import 'package:hmg_patient_app/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:hmg_patient_app/core/viewModels/PharmacyPagesViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/cart-page/cart-order-page.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/pharmacy_module_page.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/product-details/product-detail.dart';
import 'package:hmg_patient_app/pages/pharmacy/profile/profile.dart';
import 'package:hmg_patient_app/pages/pharmacy_categorise.dart';
import 'package:hmg_patient_app/pages/search_products_page.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/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/pharmacy/bottom_nav_pharmacy_bar.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../locator.dart';

class LandingPagePharmacy extends StatefulWidget {
  final int currentTab;

  const LandingPagePharmacy({Key? key, this.currentTab = 0}) : super(key: key);
  @override
  _LandingPagePharmacyState createState() => _LandingPagePharmacyState();
}

class _LandingPagePharmacyState extends State<LandingPagePharmacy> {
  late ProjectViewModel projectProvider;
  int currentTab = 0;
  late PageController pageController;

  void initState() {
    super.initState();
    locator<PharmacyModuleService>().manufacturerList = [];
    locator<PharmacyModuleService>().bestSellerProducts = [];
    locator<PharmacyModuleService>().lastVisitedProducts = [];
    pageController =
        PageController(keepPage: true, initialPage: widget.currentTab);
    setState(() {
      currentTab = widget.currentTab;
    });
  }

  void changeCurrentTab(int tab) {
    if (pageController.hasClients) {
      setState(() {
        currentTab = tab;
        pageController.jumpToPage(tab);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: AppScaffold(
        isBottomBar: true,
        extendBody: false,
        isShowDecPage: false,
        isMainPharmacyPages: true,
        currentTab: currentTab,
        changeCurrentTab: changeCurrentTab,
        body: PageView(
          physics: NeverScrollableScrollPhysics(),
          controller: pageController,
          children: [
            PharmacyPage(),
            PharmacyCategorisePage(),
            PharmacyProfilePage(moveToOrder: false, changeTab: (){changeCurrentTab(2);}),
            CartOrderPage(changeTab:  changeCurrentTab),
          ],
        ),
      ),
    );
  }

  void _scanQrAndGetProduct() async {
    try {
      ScanResult result = await BarcodeScanner.scan();
      try {
        String barcode = result.rawContent;
        GifLoaderDialogUtils.showMyDialog(context);
        await BaseAppClient()
            .getPharmacy("$GET_PHARMACY_PRODUCTs_BY_SKU$barcode",
                onSuccess: (dynamic response, int statusCode) {
          print(response);
          var product = PharmacyProduct.fromJson(response["products"][0]);
          GifLoaderDialogUtils.hideDialog(context);
          Navigator.push(context, FadePage(page: ProductDetailPage(product)));
        }, onFailure: (String error, int statusCode) {
          GifLoaderDialogUtils.hideDialog(context);
          AppToast.showErrorToast(message: "Product not found");
        });
      } catch (apiEx) {
        AppToast.showErrorToast(
            message: "Something went wrong, please try again");
      }
    } catch (barcodeEx) {}
  }

  getText(currentTab) {
    switch (currentTab) {
      case 2:
        return 'Wishlist';
      case 3:
        return 'My Account';
      case 4:
        return 'Shopping Cart';
    }
  }
}
