import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/product_detail_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/compare-list.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/product-name-and-price.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/recommended_products.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/reviews_info.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/shared/custom-divider.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.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/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';

import 'availability_info.dart';
import 'details_info.dart';
import 'discount_description.dart';
import 'footor/footer-widget.dart';
import 'shared/product_details_app_bar.dart';

String itemID;
PharmacyProduct specificationData;

class ProductDetailPage extends StatefulWidget {
  final PharmacyProduct product;

  ProductDetailPage(this.product);

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

class __ProductDetailPageState extends State<ProductDetailPage> {
  AppSharedPreferences sharedPref = AppSharedPreferences();

  bool isTrue = true;
  bool isDetails = true;
  bool isReviews = false;
  bool isAvailability = false;
  String customerId;
  bool isSelected = true;
  bool isOverQuantity = false;
  bool isInWishList = false;
  int quantity = 0;

  checkWishlist() async {
    GifLoaderDialogUtils.showMyDialog(context);
    ProductDetailViewModel model = new ProductDetailViewModel();
    await model.checkWishlistData();

    for (int i = 0; i < model.wishListItems.length; i++) {
      if (itemID == model.wishListItems[i].product.id) {
        isInWishList = true;
        break;
      } else {
        isInWishList = false;
      }
    }
    GifLoaderDialogUtils.hideDialog(context);
    setState(() {});
  }

  void initState() {
    quantity = 1;
    specificationData = widget.product;

    userInfo();
    super.initState();
  }

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

  void userInfo() async {
    print(specificationData);
    customerId = await sharedPref.getString(PHARMACY_CUSTOMER_ID);
    if (customerId != null) {
      itemID = widget.product.id;
      checkWishlist();
    }
    setState(() {});
  }

  Widget build(BuildContext context) {
    return BaseView<ProductDetailViewModel>(
        allowAny: true,
        builder: (_, model, wi) => AppScaffold(
              appBarTitle: TranslationBase.of(context).productDetails,
              isShowAppBar: true,
              isPharmacy: true,
              isShowDecPage: false,
              customAppBar: ProductAppBar(
                product: widget.product,
                quantity: quantity,
                model: model,
                addToWishlistFunction: () async {
                  await addToWishlistFunction(itemID: itemID, model: model);
                },
                deleteFromWishlistFunction: () async {
                  await deleteFromWishlistFunction(itemID: itemID, model: model);
                },
                  isInWishList:isInWishList,
                addToCartFunction:addToCartFunction ,
              ),
              body: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      width: double.infinity,
                      color: Colors.white,
                      child: Column(
                        children: [
                          if (widget.product.images.isNotEmpty)
                            Container(
                              height: MediaQuery.of(context).size.height * .40,
                              child: Image.network(
                                widget.product.images[0].src.trim(),
                                fit: BoxFit.contain,
                              ),
                            ),
                          if (widget.product.discountDescription != null)
                            DiscountDescription(product: widget.product)
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 4,
                    ),
                    Container(
                      color: Colors.white,
                      child: ProductNameAndPrice(
                        context,
                        widget.product,
                        customerId: customerId,
                        addToWishlistFunction: (item) {
                          addToWishlistFunction(itemID: item, model: model);
                          setState(() {});
                        },
                        deleteFromWishlistFunction: (item) {
                          deleteFromWishlistFunction(
                              itemID: item, model: model);
                          setState(() {});
                        },
                        notifyMeWhenAvailable: (context, itemId) {
                          notifyMeWhenAvailable(
                              itemId: itemId,
                              customerId: customerId,
                              model: model);
                        },
                        isInWishList: isInWishList,
                      ),
                    ),
                    SizedBox(
                      height: 6,
                    ),
                    Container(
                      color: Colors.white,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 15, horizontal: 10),
                            child: Texts(
                              TranslationBase.of(context).specification,
                              fontSize: 15,
                              fontWeight: FontWeight.bold,
                            ),
                            width: double.infinity,
                          ),
                          // Divider(color: Colors.grey),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 6,
                    ),
                    Container(
                      // width: 500,
                      margin: EdgeInsets.only(bottom: 6),
                      color: Colors.white,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Column(
                                children: [
                                  FlatButton(
                                    onPressed: () {
                                      setState(() {
                                        isDetails = true;
                                        isReviews = false;
                                        isAvailability = false;
                                      });
                                    },
                                    child: Text(
                                      TranslationBase.of(context).details,
                                      style: TextStyle(
                                          fontSize: 16,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    color: Colors.white,
                                  ),
                                  CustomDivider(
                                    color: isDetails
                                        ? Colors.green
                                        : Colors.transparent,
                                  )
                                ],
                              ),
                              SizedBox(
                                width: 20,
                              ),
                              Column(
                                children: [
                                  FlatButton(
                                    onPressed: () async {
                                      if (widget.product.approvedTotalReviews >
                                          0) {
                                        GifLoaderDialogUtils.showMyDialog(
                                            context);
                                        await model.getProductReviewsData(
                                            widget.product.id);
                                        GifLoaderDialogUtils.hideDialog(
                                            context);
                                      } else {
                                        model.clearReview();
                                      }
                                      setState(() {
                                        isDetails = false;
                                        isReviews = true;
                                        isAvailability = false;
                                      });
                                    },
                                    child: Text(
                                      TranslationBase.of(context).reviews,
                                      style: TextStyle(
                                          fontSize: 16,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    color: Colors.white,
                                  ),
                                  CustomDivider(
                                    color: isReviews
                                        ? Colors.green
                                        : Colors.transparent,
                                  ),
                                ],
                              ),
                              SizedBox(
                                width: 20,
                              ),
                              Column(
                                children: [
                                  FlatButton(
                                    onPressed: () async {
                                      GifLoaderDialogUtils.showMyDialog(
                                          context);
                                      await model.getProductLocationData();
                                      GifLoaderDialogUtils.hideDialog(context);

                                      setState(() {
                                        isDetails = false;
                                        isReviews = false;
                                        isAvailability = true;
                                      });
                                    },
                                    child: Text(
                                      TranslationBase.of(context).availability,
                                      style: TextStyle(
                                          fontSize: 16,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    color: Colors.white,
                                  ),
                                  CustomDivider(
                                    color: isAvailability
                                        ? Colors.green
                                        : Colors.transparent,
                                  ),
                                ],
                              ),
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          isDetails
                              ? DetailsInfo(
                                  product: widget.product,
                                )
                              : isReviews
                                  ? ReviewsInfo(
                                      product: widget.product,
                                      previousModel: model,
                                    )
                                  : isAvailability
                                      ? AvailabilityInfo(
                                          previousModel: model,
                                        )
                                      : Container(),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    RecommendedProducts(product: widget.product,productDetailViewModel: model,
                      addToWishlistFunction: (itemID) async {
                      await addToWishlistFunction(itemID: itemID, model: model);
                    }, deleteFromWishlistFunction: (itemID) async {
                        await deleteFromWishlistFunction(itemID: itemID, model: model);
                      },)
                  ],
                ),
              ),
              bottomSheet: FooterWidget(
                widget.product.stockAvailability != 'Out of stock',
                widget.product.orderMaximumQuantity,
                widget.product.orderMinimumQuantity,
                widget.product.stockQuantity,
                widget.product,
                quantity: quantity,
                isOverQuantity: isOverQuantity,
                addToCartFunction: addToCartFunction,
                model: model,
              ),
            ));
  }

  addToWishlistFunction({itemID, ProductDetailViewModel model}) async {
    isInWishList = true;
    await model.addToWishlistData(itemID);
    setState(() {});
  }

  deleteFromWishlistFunction({itemID, ProductDetailViewModel model}) async {
    isInWishList = false;
    await model.deleteWishlistData(itemID);
    setState(() {});
  }
  addToCartFunction(
      {quantity,
        itemID,
        ProductDetailViewModel model}) async {
    GifLoaderDialogUtils.showMyDialog(context);
    await model.addToCartData(quantity, itemID);
    GifLoaderDialogUtils.hideDialog(context);
  }
}



notifyMeWhenAvailable(
    {itemId, customerId, ProductDetailViewModel model}) async {
  await model.notifyMe(customerId, itemId);
}
