import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/service/AuthenticatedUserObject.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/product_detail_view_model.dart';
import 'package:diplomaticquarterapp/pages/landing/landing_page_pharmcy.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/product-detail.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/navigation_service.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

import '../../../../../locator.dart';
import '../../../compare-list.dart';
import 'icon_with_bg.dart';

class ProductAppBar extends StatelessWidget with PreferredSizeWidget {
  final PharmacyProduct product;

  final ProductDetailViewModel model;
  final Function addToWishlistFunction;
  final Function deleteFromWishlistFunction;
  final int quantity;
  final bool isInWishList;
  final Function addToCartFunction;

  ProductAppBar(
      {Key key,
      this.product,
      this.model,
      this.addToWishlistFunction,
      this.quantity,
      this.deleteFromWishlistFunction,
      this.isInWishList,
      this.addToCartFunction})
      : super(key: key);

  AuthenticatedUserObject authenticatedUserObject =
      locator<AuthenticatedUserObject>();

  @override
  Widget build(BuildContext context) {
    OrderPreviewViewModel orderPreviewViewModel = Provider.of(context);
    return Container(
      color: Colors.white,
      child: FractionallySizedBox(
        widthFactor: 0.95,
        child: Column(
          children: [
            SizedBox(
              height: 30,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    IconWithBg(
                      icon: Icons.arrow_back_ios,
                      hasPadding: true,
                      color: Color(0xFF2B353E),
                      onPress: () {
                        Navigator.pop(context);
                      },
                    ),
                  ],
                ),
                if (authenticatedUserObject.isLogin)
                  Row(
                    children: [
                      Stack(
                        children: [
                          IconWithBg(
                              icon: Icons.shopping_cart,
                              color: Color(0xFF2B353E),
                              onPress: () {
                                Navigator.pushAndRemoveUntil(
                                    locator<NavigationService>()
                                        .navigatorKey
                                        .currentContext,
                                    MaterialPageRoute(
                                        builder: (context) =>
                                            LandingPagePharmacy(currentTab: 3)),
                                    (Route<dynamic> r) => false);
                                // Navigator.push(
                                //   context,
                                //   MaterialPageRoute(builder: (context) => CartOrderPage()),
                                // );
                              }),
                          if (Provider.of<OrderPreviewViewModel>(context,
                                      listen: false)
                                  .cartResponse
                                  .quantityCount !=
                              0)
                            Positioned(
                              top: 0,
                              right: -1.0,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.red[800],
                                  borderRadius: BorderRadius.circular(15),
                                ),
                                padding: EdgeInsets.only(left: 5, right: 4.5),
                                height: 18,
                                child: Center(
                                    child: Texts(
                                  Provider.of<OrderPreviewViewModel>(context,
                                          listen: false)
                                      .cartResponse
                                      .quantityCount
                                      .toString(),
                                  style: "caption",
                                  // medium: true,
                                  color: Colors.white,
                                )),
                              ),
                            )
                        ],
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      IconWithBg(
                        icon: FontAwesomeIcons.ellipsisV,
                        color: Color(0xFF2B353E),
                        onPress: () {
                          settingModalBottomSheet(context);
                        },
                      ),
                    ],
                  )
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size(double.maxFinite, 50);

  settingModalBottomSheet(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return Container(
            child: new Wrap(
              children: <Widget>[
                if (product.stockAvailability != 'Out of stock' && product.isRx != true)
                  new ListTile(
                      leading: Icon(Icons.shopping_cart),
                      title: Text(
                        TranslationBase.of(context).addToCart,
                      ),
                      onTap: () async {
                        if (quantity > 0) {
                          {
                            await addToCartFunction(
                                quantity: quantity,
                                itemID: itemID,
                                model: model);

                            Navigator.of(context).pop();
                          }
                        } else {
                          AppToast.showErrorToast(
                              message: TranslationBase.of(context).addQuantity
                              //  "you should add quantity"
                              );
                        }
                      }),
                ListTile(
                    leading: Icon(
                      !isInWishList ? Icons.favorite_border : Icons.favorite,
                      color: !isInWishList ? Colors.white : Colors.red[800],
                    ),
                    title: Text(
                      isInWishList
                          ? TranslationBase.of(context).removeFromWishlist
                          : TranslationBase.of(context).addToWishlist,
                    ),
                    onTap: () async {
                      if (isInWishList)
                        await deleteFromWishlistFunction();
                      else
                        await addToWishlistFunction();
                      Navigator.of(context).pop();
                    }),
                ListTile(
                  leading: Icon(Icons.compare),
                  title: Text(
                    TranslationBase.of(context).compare,
                  ),
                  onTap: () {
                    Provider.of<CompareList>(context, listen: false)
                        .addItem(specificationData, context);
                    Navigator.of(context).pop();
                  },
                ),
              ],
            ),
          );
        });
  }
}
