import 'package:cheminova/controller/cart_controller.dart';
import 'package:cheminova/models/oder_place_model.dart';
import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/screens/product/product_detail_screen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

import '../models/product_model1.dart';
import '../screens/product/cart_screen.dart';
import '../utils/show_snackbar.dart';

class ProductCard extends StatefulWidget {
  final Product? productModel;
  PlacedOrderModel? placedOrder;
  ProductModel? product;
  final bool isInCart;
  final bool isCheckout;
  final bool isConfirmation;
   int? quantity;
  ProductCard({
    super.key,
    this.product,
    this.quantity=1,
    this.productModel,
    this.placedOrder,
    this.isInCart = false,
    this.isCheckout = false,
    this.isConfirmation = false,
  });

  @override
  State<ProductCard> createState() => _ProductCardState();
}


class _ProductCardState extends State<ProductCard> {
  @override

  @override
  Widget build(BuildContext context) {
    final CartController _cartController = Get.put(CartController());
    bool showQuantity = widget.isInCart || widget.isCheckout || widget.isConfirmation;
    bool isProductInCart = _cartController.cartList.contains(widget.productModel);
    return GestureDetector(
      onTap: () => widget.isInCart || widget.isCheckout
          ? null
          : Get.to(() =>
          ProductDetailScreen(productModel: widget.productModel)),
      child: Card(
        child: Row(
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(15.0),
                child: Container(
                  height: Get.height * 0.15,
                  width: Get.width * 0.30,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/product.png"),
                      // Image.asset(productModel!['image']).image,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
            ),
            const SizedBox(
              width: 10,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  widget.productModel!.name,
                  style: GoogleFonts.roboto(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Text(
                  widget.productModel!.category!.categoryName,
                  style: GoogleFonts.roboto(
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                ),
                Text(
                  "₹ ${ widget.productModel!.price.toString()}",
                  style: GoogleFonts.roboto(
                    fontSize: 22,
                    fontWeight: FontWeight.w700,
                  ),
                ),
                showQuantity
                    ? Text(
                  "Quantity: ${widget.productModel!.quantity.toString()}",
                  style: GoogleFonts.roboto(
                    fontSize: 15,
                    fontWeight: FontWeight.w700,
                  ),
                )
                    : const SizedBox(),
                widget.isCheckout
                    ? const SizedBox()
                    : widget.isInCart
                    ? Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      height: 40,
                      width: 100,
                      decoration: BoxDecoration(
                        color: const Color(0xFF004791),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Row(
                        mainAxisAlignment:
                        MainAxisAlignment.spaceAround,
                        children: [
                          SizedBox(
                            height: 24,
                            width: 24,
                            child: ElevatedButton(
                              onPressed: () {
                                _cartController.increaseQuantity(widget.productModel!);
                                setState(() {
                                 widget.quantity = widget.productModel!.quantity;

                                });
                              },
                              style: ElevatedButton.styleFrom(
                                padding: EdgeInsets.zero,
                                shape: RoundedRectangleBorder(
                                  borderRadius:
                                  BorderRadius.circular(8.0),
                                ),
                              ),
                              child: Text(
                                '+',
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w800,
                                ),
                              ),
                            ),
                          ),
                          Text(
                           "${widget.quantity}",
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                            ),
                          ),
                          SizedBox(
                            height: 24,
                            width: 24,
                            child: ElevatedButton(
                              onPressed: () {
                                _cartController.decreaseQuantity(widget.productModel!);
                                setState(() {
                                  widget.quantity = widget.productModel!.quantity;
                                });

                              },
                              style: ElevatedButton.styleFrom(
                                padding: EdgeInsets.zero,
                                shape: RoundedRectangleBorder(
                                  borderRadius:
                                  BorderRadius.circular(8.0),
                                ),
                              ),
                              child: Text(
                                '-',
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w800,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(width: 20.0,),
                    IconButton(
                      onPressed: () {
                        _cartController.removeFromCart(widget.productModel!);
                      },
                      icon: const Icon(
                        Icons.delete_outline_rounded,
                        color: Colors.red,
                      ),
                    )
                  ],
                )
                    : ElevatedButton(
                  onPressed: () {
                    if (isProductInCart) {
                      showSnackbar("Product already added to cart");
                    } else {
                      _cartController.addToCart(widget.productModel!);
                      showSnackbar("Product added to cart successfully");
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    foregroundColor: Colors.white,
                    backgroundColor: const Color(0xFF00784C),
                    padding: const EdgeInsets.symmetric(
                        horizontal: 18, vertical: 8),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(30),
                    ),
                  ),
                  child: Text(
                    "Add To Cart",
                    style: GoogleFonts.roboto(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
