import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/wishlist.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/widgets/pharmacy/product_tile.dart';
import 'package:flutter/material.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/product_detail_view_model.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/wishlist_view_model.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';

AppSharedPreferences sharedPref = AppSharedPreferences();
var languageID;

class ProductCheckTypeWidget extends StatefulWidget {
  final List<dynamic> wishlist;
  final bool isTrue;

  ProductCheckTypeWidget(this.isTrue, this.wishlist);

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

class _ProductCheckTypeWidgetState extends State<ProductCheckTypeWidget> {
  void initState() {
    getLanguageID();
  }

  @override
  Widget build(BuildContext context) {
    return widget.isTrue
        ? ListView.builder(
            itemCount: widget.wishlist.length,
            itemBuilder: (BuildContext context, int index) {
              return Column(
                children: [
                  Container(
                    child: widget.isTrue
                        ? productTile(
                            productName: languageID == 'ar'
                                ? widget.wishlist[index].product.namen
                                : widget.wishlist[index].product.name,
                            productPrice: widget.wishlist[index].subtotal,
                            productRate: double.parse(
                                widget.wishlist[index].subtotalVatRate),
                            productImage:
                                widget.wishlist[index].product.images[0].src,
                            showLine: widget.isTrue,
                            productID: widget.wishlist[index].product.id,
                            onDelete: deleteWishListItem,

                          )
                        : productTile(
                            productName: languageID == 'ar'
                                ? widget.wishlist[index].product.namen
                                : widget.wishlist[index].product.name,
                            productPrice: widget.wishlist[index].subtotal,
                            productRate: double.parse(
                                widget.wishlist[index].subtotalVatRate),
                            productImage:
                                widget.wishlist[index].product.images[0].src,
                            showLine: widget.isTrue,
                            onDelete: deleteWishListItem,

                          ),
                  ),
                  Divider(height: 1, color: Colors.grey)
                ],
              );
            })
        : GridView.count(
            crossAxisCount: 2,
            children: List.generate(
                widget.wishlist.length,
                (index) => productTile(
                      productName: widget.wishlist[index].product.name,
                      productPrice: widget.wishlist[index].subtotal,
                      productRate:
                          double.parse(widget.wishlist[index].subtotalVatRate),
                      productImage:
                          widget.wishlist[index].product.images[0].src,
                      showLine: widget.isTrue,
                    )),
          );
  }

  deleteWishListItem(itemID) async {
    ProductDetailViewModel x = new ProductDetailViewModel();
    GifLoaderDialogUtils.showMyDialog(context);
    await x.deletWishlistData(itemID);
    setState(()
    async{
     await
      //getWishlistData();
      Navigator.push(context,
        FadePage(page: WishlistPage(false)),
      );
    });
    GifLoaderDialogUtils.hideDialog(context);
    print("Delete");
  }


  getWishlistData() async {
    WishListViewModel x = new WishListViewModel();
    await x.getWishlistData();
  }

}

getLanguageID() async {
  languageID = await sharedPref.getString(APP_LANGUAGE);
}
