import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/models/product_model1.dart';
import 'package:cheminova/utils/show_snackbar.dart';
import 'package:cheminova/widgets/my_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

import '../../controller/cart_controller.dart';

// ProductDetailScreen displays detailed information about a selected product.
class ProductDetailScreen extends StatefulWidget {
  Product? productModel; // Holds the product model data for display.
  ProductModel? product; // Another product model (not used in this code).

  ProductDetailScreen({super.key, this.product, this.productModel});

  @override
  State<ProductDetailScreen> createState() => _ProductDetailScreenState();
}

class _ProductDetailScreenState extends State<ProductDetailScreen> {
  final CartController _cartController = Get.put(CartController()); // Controller for managing cart state.

  // Capitalizes the first letter of the given text.
  String capitalizeFirstLetter(String text) {
    if (text.isEmpty) return text; // Return empty if the text is empty.
    return text[0].toUpperCase() + text.substring(1).toLowerCase(); // Capitalize the first letter.
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true, // Extend the body behind the AppBar.
      appBar: AppBar(
        centerTitle: true, // Center the title.
        backgroundColor: Colors.transparent, // Make AppBar transparent.
        elevation: 0, // Remove shadow from AppBar.
        leading: Builder(
          builder: (context) {
            return GestureDetector(
              onTap: () => Scaffold.of(context).openDrawer(), // Open drawer on tap.
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: SvgPicture.asset(
                  'assets/svg/menu.svg', // Menu icon.
                ),
              ),
            );
          },
        ),
        actions: [
          GestureDetector(
            onTap: () => Get.back(), // Navigate back on tap.
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: SvgPicture.asset(
                'assets/svg/back_arrow.svg', // Back arrow icon.
              ),
            ),
          ),
        ],
        title: const Text(
          "Product Detail", // Title of the screen.
        ),
      ),
      drawer: const MyDrawer(), // Navigation drawer.
      body: Stack(
        fit: StackFit.expand, // Expand the stack to fit the screen.
        children: [
          Image.asset(
            'assets/images/image_1.png', // Background image.
            fit: BoxFit.cover,
          ),
          SafeArea(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start, // Align items to start.
              children: [
                SizedBox(height: Get.height * 0.02), // Spacing above the card.

                // Card to display product details.
                Card(
                  margin: const EdgeInsets.symmetric(horizontal: 16), // Horizontal margin.
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(19), // Rounded corners.
                    side: const BorderSide(color: Color(0xFFFDFDFD)), // Border color.
                  ),
                  color: const Color(0xFFB4D1E5).withOpacity(0.9), // Background color of the card.
                  child: Padding(
                    padding: const EdgeInsets.all(12.0), // Padding inside the card.
                    child: Column(
                      mainAxisSize: MainAxisSize.min, // Minimize the size of the column.
                      crossAxisAlignment: CrossAxisAlignment.start, // Align items to start.
                      children: [
                        // Product image section.
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Container(
                            height: Get.height * 0.4, // Height of the image container.
                            width: Get.width * 0.8, // Width of the image container.
                            decoration: BoxDecoration(
                              border: Border.all(
                                width: 4,
                                color: Colors.white, // Border color.
                              ),
                              borderRadius: BorderRadius.circular(15), // Rounded corners for the container.
                            ),
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(10), // Clip the corners of the image.
                              child: Image.asset(
                                "assets/images/product.png", // Product image.
                                fit: BoxFit.cover, // Cover the container.
                              ),
                            ),
                          ),
                        ),

                        // Product name row.
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text("Product Name ", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), // Label for product name.
                              Text(
                                capitalizeFirstLetter(widget.productModel!.name), // Display product name.
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  color: Colors.black,
                                ),
                              ),
                            ],
                          ),
                        ),

                        // Product price row.
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text("Price ", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), // Label for product price.
                              Text(
                                "₹ ${widget.productModel!.price.toString()}", // Display product price.
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  color: Colors.black,
                                ),
                              ),
                            ],
                          ),
                        ),

                        // Product category row.
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text("Category ", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), // Label for category.
                              Text(
                                capitalizeFirstLetter(widget.productModel!.category.categoryName), // Display category name.
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w400,
                                  color: Colors.black,
                                ),
                              ),
                            ],
                          ),
                        ),

                        const SizedBox(height: 8), // Spacing between rows.

                        // Product description row.
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                          child: Row(
                            children: [
                              const Text("Description", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)), // Label for description.
                              Text(
                                capitalizeFirstLetter(widget.productModel!.description), // Display product description.
                                style: GoogleFonts.roboto(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w400,
                                  color: Colors.black,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),

                SizedBox(height: Get.height * 0.04), // Spacing below the card.

                // Add to cart button.
                SizedBox(
                  width: Get.width * 0.9,
                  height: Get.height * 0.06,
                  child: ElevatedButton(
                    onPressed: () {
                      // Add the product to the cart and show a snackbar notification.
                      _cartController.addToCart(widget.productModel!);
                      showSnackbar("Product successfully added to your cart"); // Display success message.
                    },
                    style: ElevatedButton.styleFrom(
                      foregroundColor: Colors.white, // Text color.
                      backgroundColor: const Color(0xFF00784C), // Button background color.
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10), // Rounded corners for the button.
                      ),
                    ),
                    child: Text(
                      "Add To Cart", // Button text.
                      style: GoogleFonts.roboto(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
