import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../app.dart';

class ProductCard extends StatefulWidget {
  final ProductModel model;
  final bool isSmallCard;
  final double elevation;

  ProductCard({
    required this.model,
    this.isSmallCard = false,
    this.elevation = .5,
  });

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

class _ProductCardState extends State<ProductCard> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext ctx) {
    return ConstrainedBox(
      constraints: BoxConstraints(maxHeight: 300.h, minHeight: 250.h),
      child: Material(
        color: Colors.transparent,
        child: Ink(
          decoration: new BoxDecoration(
            color: ThemeColor.white,
            border: Border.all(color: ThemeColor.colorEFF0F4),
            borderRadius: BorderRadius.circular(8),
          ),
          child: InkWell(
            splashColor: Theme.of(context).splashColor,
            onTap: () => Get.to(() => ProductDetailsPage(model: widget.model)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Image part
                Container(
                  padding: const EdgeInsets.all(8),
                  decoration: new BoxDecoration(
                    color: ThemeColor.searchBoxFillColor,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Stack(
                    children: [
                      cachedImageNetwork(
                        widget.model.images.isNotEmpty ? widget.model.images.first.originalImageUrl : '',
                        BoxFit.scaleDown,
                        const BorderRadius.all(Radius.circular(4.0)),
                        1.sw,
                        widget.isSmallCard ? 140.h : 160.h,
                      ),
                      FavoriteWidget(model: widget.model),
                    ],
                  ),
                ),

                SizedBox(height: 12.h),

                Padding(
                  padding: const EdgeInsets.only(left: 6),
                  child: widget.model.reviews.total != 0
                      ? Row(
                          children: [
                            Icon(
                              Icons.star,
                              color: ThemeColor.mainColor,
                              size: widget.isSmallCard ? 14.sp : 16.sp,
                            ),
                            SizedBox(width: 5.w),
                            Text(
                              widget.model.reviews.averageRating,
                              style: new TextStyle(
                                fontSize: widget.isSmallCard ? 12.sp : 14.sp,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ],
                        )
                      : Text(
                          '',
                          style: new TextStyle(
                            fontSize: widget.isSmallCard ? 12.sp : 14.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                ),

                SizedBox(height: 8.h),

                // Product name part
                Padding(
                  padding: const EdgeInsets.only(left: 6, right: 6),
                  child: Text(
                    widget.model.name,
                    maxLines: 2,
                    style: TextStyle(
                      fontWeight: FontWeight.normal,
                      fontSize: 12.sp,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),

                Spacer(),

                // SizedBox(height: 8.h),

                widget.model.hasDiscount
                    // Discount part text
                    ? Row(
                        children: [
                          // Discounted price
                          _priceWidget(widget.model.formattedSpecialPrice ?? '', true),

                          // Original price
                          Flexible(
                            child: Text(
                              widget.model.formattedRegularPrice ?? '',
                              style: new TextStyle(
                                fontSize: widget.isSmallCard ? 8.sp : 10.sp,
                                fontWeight: FontWeight.w400,
                                decoration: TextDecoration.lineThrough,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          )
                        ],
                      )
                    : _priceWidget(widget.model.formattedPrice, false),
              ],
            ),
            /* */
          ),
        ),
      ),
    );
  }

  Widget _priceWidget(String text, bool hasDiscount) {
    return Container(
      padding: const EdgeInsets.all(4),
      decoration: new BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        border: Border.all(color: ThemeColor.searchBoxFillColor),
      ),
      child: Text(
        text,
        style: new TextStyle(
          fontSize: hasDiscount ? 12.sp : 14.sp,
          fontWeight: FontWeight.w500,
          color: hasDiscount ? ThemeColor.mainColor: ThemeColor.blackColorsTxt
        ),
        overflow: TextOverflow.ellipsis,
      ),
    );
  }
}
