import 'package:barcode_scan_fix/barcode_scan.dart';
import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/product_detail.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/cart-order-page.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/pharmacy_module_page.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/wishlist.dart';
import 'package:diplomaticquarterapp/pages/pharmacy/profile/profile.dart';
import 'package:diplomaticquarterapp/pages/pharmacy_categorise.dart';
import 'package:diplomaticquarterapp/pages/search_products_page.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/pharmacy/bottom_nav_pharmacy_bar.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

class LandingPagePharmacy extends StatefulWidget {
  @override
  _LandingPagePharmacyState createState() => _LandingPagePharmacyState();
}

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

  _changeCurrentTab(int tab) {
    setState(() {
      currentTab = tab;
      pageController.jumpToPage(tab);
    });
  }

  void initState() {
    super.initState();
    pageController = PageController(keepPage: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: currentTab != 4 && currentTab != 3
          ? AppBar(
              backgroundColor: Color(0xff5AB145),
              elevation: 0,
              title: Container(
                height: MediaQuery.of(context).size.height * 0.056,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5.0),
                  color: Colors.white,
                ),
                child: InkWell(
                  child: Padding(
                    padding: EdgeInsets.all(5.0),
                    child: Row(
                      //crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Icon(Icons.search, size: 25.0),
                        SizedBox(
                          width: 15.0,
                        ),
                        Texts(
                          TranslationBase.of(context).searchProductHere,
                          fontSize: 13,
                        )
                      ],
                    ),
                  ),
                  onTap: () {
                    Navigator.push(
                      context,
                      FadePage(page: SearchProductsPage()),
                    );
                  },
                ),
              ),
              leading: Builder(
                builder: (BuildContext context) {
                  return InkWell(
                    onTap: () {
                      setState(() {
                        currentTab = 0;
                        pageController.jumpToPage(0);
                      });
                    },
                    child: Container(
                      height: 2.0,
                      width: 10.0,
                      child: Image.asset(
                        'assets/images/pharmacy_logo.png',
                      ),
                    ),
                  );
                },
              ),
              actions: [
                IconButton(
                    //  iconSize: 70,
                    icon: Image.asset(
                      'assets/images/new-design/qr-code.png',
                    ),
                    onPressed: _scanQrAndGetProduct //do something,
                    )
              ],
              centerTitle: true,
            )
          : null,
      extendBody: false,
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        controller: pageController,
        children: [
          PharmacyPage(),
          PharmacyCategorisePage(),
          PharmacyProfilePage(),
          CartOrderPage(),
        ],
      ),
      bottomNavigationBar: BottomNavPharmacyBar(
        changeIndex: _changeCurrentTab,
        index: currentTab,
      ),
    );
  }

  void _scanQrAndGetProduct() async {
    try {
      String result = await BarcodeScanner.scan();
      try {
        String barcode = result;
        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';
    }
  }
}
