import 'package:cheminova/controller/cart_controller.dart';
import 'package:cheminova/models/oder_place_model.dart';
import 'package:cheminova/models/order_item_model.dart';
import 'package:cheminova/models/place_order_list_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 '../utils/show_snackbar.dart';

class ProductCard extends StatefulWidget {
  final Product? productModel;
  PlacedOrderModel? placedOrder;
  PlacedOrderList? placedOrderList;
  PlaceOrderItem1? placeorderItem;
  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.placedOrderList,
    this.placeorderItem,
    this.isInCart = false,
    this.isCheckout = false,
    this.isConfirmation = false,
  });

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

class _ProductCardState extends State<ProductCard> {
  String capitalizeFirstLetter(String text) {
    if (text.isEmpty) return text;
    return text[0].toUpperCase() + text.substring(1).toLowerCase();
  }

  @override
  Widget build(BuildContext context) {
    final CartController _cartController = Get.put(CartController());
    bool showQuantity = widget.isInCart || widget.isCheckout || widget.isConfirmation;
    bool isProductInCart = _cartController.cartList.any((p) => p.id == widget.productModel!.id);

    int currentQuantity = isProductInCart
        ? _cartController.cartList.firstWhere((p) => p.id == widget.productModel!.id).quantity
        : widget.productModel!.quantity;

    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.31,
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/product.png"),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 3.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      capitalizeFirstLetter(widget.productModel!.name),
                      style: GoogleFonts.roboto(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    Text(
                      capitalizeFirstLetter(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,
                      ),
                    ),
                    if (showQuantity)
                      Text(
                        "Quantity: ${currentQuantity}",
                        style: GoogleFonts.roboto(
                          fontSize: 15,
                          fontWeight: FontWeight.w700,
                        ),
                      ),
                    if (!widget.isCheckout)
                      widget.isInCart
                          ? Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            height: Get.height * 0.04,
                            width: Get.width * 0.21,
                            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.decreaseQuantity(widget.productModel!);
                                      setState(() {
                                        currentQuantity = _cartController
                                            .cartList
                                            .firstWhere((p) => p.id == widget.productModel!.id)
                                            .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(
                                  "${currentQuantity}",
                                  style: const TextStyle(
                                    color: Colors.white,
                                    fontSize: 16,
                                  ),
                                ),
                                SizedBox(
                                  height: 22,
                                  width: 22,
                                  child: ElevatedButton(
                                    onPressed: () {
                                      _cartController.increaseQuantity(widget.productModel!);
                                      setState(() {
                                        currentQuantity = _cartController
                                            .cartList
                                            .firstWhere((p) => p.id == widget.productModel!.id)
                                            .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: 2.0,
                          ),
                          IconButton(
                            onPressed: () {
                              _cartController.removeFromCart(widget.productModel!);
                              showSnackbar("Product has been removed successfully!");
                              setState(() {
                                currentQuantity = 1;
                              });
                            },
                            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 successfully added to your cart");
                          }
                        },
                        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,
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
