import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:shimmer/shimmer.dart';
import '../../../../../theme.dart';
import '../../../../../size_config.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';

class ProductCardCoupon extends StatelessWidget {
  const ProductCardCoupon({
    Key? key,
    this.width = 120,
    this.pad = 16,
    this.padRight = 0,
    required this.thumbnail,
    required this.id,
    required this.isTopCourse,
    required this.title,
    required this.instructorName,
    required this.rating,
    required this.specificRating,
    required this.numberOfRatings,
    required this.price,
    required this.realPrice,
    required this.press,
    required this.students,
    required this.totalDiscount,
  }) : super(key: key);

  final double width;
  final double pad, padRight;
  final String thumbnail,
      title,
      instructorName,
      price,
      realPrice,
      rating,
      specificRating,
      numberOfRatings,
      id,
      isTopCourse;
  final String students;
  final VoidCallback press;
  final int totalDiscount;
  // final VoidCallback? whislistPress;
  // final Widget? iconWishlist;

  @override
  Widget build(BuildContext context) {
    //CourseProvider courseProvider = Provider.of<CourseProvider>(context);
    // final formatCurrency =
    //     new NumberFormat.simpleCurrency(decimalDigits: 0, locale: 'id_ID');
    bool isRatingValid = int.tryParse(rating) != null;
    bool isStudentsValid = int.tryParse(students) != null;

    bool isPopular = false;
    if (isRatingValid && isStudentsValid) {
      int ratingValue = int.parse(rating);
      int studentsValue = int.parse(students);

      bool isSpecificRatingValid = int.tryParse(specificRating) != null;
      bool isNumberOfRatingsValid = int.tryParse(numberOfRatings) != null;

      if (isSpecificRatingValid && isNumberOfRatingsValid) {
        int specificRatingValue = int.parse(specificRating);
        int numberOfRatingsValue = int.parse(numberOfRatings);

        isPopular = ratingValue > 4 &&
            studentsValue > 19 &&
            specificRatingValue > 4 &&
            numberOfRatingsValue > 4;
      }
    }
    return Stack(
      children: [
        Container(
          width: getProportionateScreenWidth(165),
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                spreadRadius: 1,
                blurRadius: 1,
                offset: Offset(7, 17),
              ),
            ],
          ),
          padding: EdgeInsets.only(
            left: getProportionateScreenWidth(pad),
            right: getProportionateScreenWidth(padRight),
          ),
          child: InkWell(
            onTap: press,
            child: Container(
              width: getProportionateScreenWidth(40),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Theme.of(context).colorScheme.primaryContainer,
                    spreadRadius: 11,
                    blurRadius: 0,
                    offset: Offset(0, 3),
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  AspectRatio(
                    aspectRatio: 1.7,
                    child: CachedNetworkImage(
                      imageUrl: thumbnail,
                      imageBuilder: (context, imageProvider) => Container(
                        decoration: BoxDecoration(
                          borderRadius:
                              BorderRadius.vertical(top: Radius.circular(5)),
                          image: DecorationImage(
                            image: imageProvider,
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      placeholder: (context, url) => Shimmer(
                        child: Container(
                          decoration: BoxDecoration(
                            color: ninthColor,
                            borderRadius:
                                BorderRadius.vertical(top: Radius.circular(5)),
                          ),
                        ),
                        gradient: LinearGradient(
                            stops: [0.2, 0.5, 0.6],
                            colors: [ninthColor, fourthColor, ninthColor]),
                      ),
                      errorWidget: (context, url, error) => Icon(Icons.error),
                      fadeInDuration: Duration(milliseconds: 1),
                      fadeOutDuration: Duration(milliseconds: 1),
                    ),
                  ),
                  SizedBox(height: getProportionateScreenWidth(4)),
                  Container(
                    padding: EdgeInsets.only(left: 2),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          title,
                          style: thirdTextStyle.copyWith(
                              letterSpacing: 1,
                              fontSize: getProportionateScreenWidth(12),
                              fontWeight: bold),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                        SizedBox(height: getProportionateScreenWidth(6)),
                        // Row(
                        //   children: [product.description],
                        // ),
                        RichText(
                          text: new TextSpan(
                            style: thirdTextStyle.copyWith(
                              fontSize: getProportionateScreenWidth(10),
                              color: Theme.of(context).dividerColor,
                            ),
                            children: <TextSpan>[
                              new TextSpan(
                                text: '',
                                style: TextStyle(
                                  fontWeight: bold,
                                ),
                              ),
                              new TextSpan(
                                text: instructorName,
                                style: TextStyle(fontWeight: bold),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: getProportionateScreenWidth(6)),
                        Row(
                          children: [
                            Text(
                              specificRating,
                              style: thirdTextStyle.copyWith(
                                fontSize: getProportionateScreenWidth(10),
                                fontWeight: bold,
                              ),
                            ),
                            SizedBox(
                              width: getProportionateScreenWidth(2),
                            ),
                            RatingBarIndicator(
                              itemSize: getProportionateScreenWidth(11),
                              rating: double.parse(rating),
                              direction: Axis.horizontal,
                              itemCount: 5,
                              itemBuilder: (context, _) => FaIcon(
                                FontAwesomeIcons.solidStar,
                                color: thirteenColor,
                              ),
                            ),
                            SizedBox(width: getProportionateScreenWidth(3)),
                            Text(
                              '(' + numberOfRatings + ')',
                              style: thirdTextStyle.copyWith(
                                fontSize: getProportionateScreenWidth(10),
                                fontWeight: bold,
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: getProportionateScreenWidth(6)),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              price,
                              style: thirdTextStyle.copyWith(
                                letterSpacing: 0.5,
                                fontSize: SizeConfig.blockHorizontal! * 3,
                                fontWeight: bold,
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: getProportionateScreenWidth(6)),
                        if (realPrice.isNotEmpty && realPrice != price)
                          Padding(
                            padding: EdgeInsets.only(
                                right: getProportionateScreenWidth(10)),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  realPrice,
                                  style: primaryTextStyle.copyWith(
                                    decoration: TextDecoration.lineThrough,
                                    fontSize: getProportionateScreenWidth(10),
                                    fontWeight: reguler,
                                  ),
                                ),
                                // IconButton(onPressed: whislistPress, icon: iconWishlist)
                              ],
                            ),
                          )
                        else
                          SizedBox.shrink(),
                        if (isPopular)
                          Padding(
                            padding: EdgeInsets.only(
                                right: getProportionateScreenWidth(10)),
                            child: Container(
                              alignment: Alignment.center,
                              width: getProportionateScreenWidth(48),
                              height: getProportionateScreenHeight(17),
                              child: Text(
                                'Populer',
                                style: thirdTextStyle.copyWith(
                                  letterSpacing: 0.5,
                                  color: Colors.white,
                                  fontSize: SizeConfig.blockHorizontal! * 2.5,
                                  fontWeight: bold,
                                ),
                              ),
                              decoration: BoxDecoration(
                                color: primaryColor,
                                borderRadius: BorderRadius.circular(5),
                              ),
                            ),
                          )
                        else
                          SizedBox(height: 0, width: 0),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
        if (totalDiscount != 0)
          Padding(
            padding: EdgeInsets.only(top: getProportionateScreenHeight(7)),
            child: Container(
              height: 20,
              alignment: Alignment.center,
              width: getProportionateScreenWidth(35),
              child: Text(
                '${totalDiscount.toString()}%',
                style: thirdTextStyle.copyWith(
                  letterSpacing: 0.5,
                  color: baruTextutih,
                  fontSize: SizeConfig.blockHorizontal! * 2.5,
                  fontWeight: light,
                ),
              ),
              decoration: BoxDecoration(
                color: primaryColor,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(4),
                  bottomRight: Radius.circular(4),
                ),
              ),
            ),
          )
        else
          SizedBox(height: 0, width: 0),
      ],
    );
  }
}

// Shimmer(
//                     child: Container(
//                       color: Colors.black,
//                     ),
//                     gradient: LinearGradient(
//                         stops: [0.2, 0.5, 0.6],
//                         colors: [ninthColor, fourthColor, ninthColor])),
