import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:initial_folder/base_service.dart';
import 'package:initial_folder/helper/validator.dart';
import 'package:initial_folder/providers/promo_course_provider.dart';
// import 'package:initial_folder/providers/top_course_provider.dart';
import 'package:initial_folder/screens/detail_course/detail_course_screen.dart';
import 'package:initial_folder/screens/home/components/body_comp/product_card/product_card.dart';
import 'package:initial_folder/widgets/custom_navigator.dart';
import 'package:provider/provider.dart';
import 'package:shimmer/shimmer.dart';

import '../../../../size_config.dart';
import '../../../../theme.dart';

class PromoCourse extends StatelessWidget {
  const PromoCourse({Key? key, this.text}) : super(key: key);
  final String? text;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Padding(
          padding:
              EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(16)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                text.toString(),
                textAlign: TextAlign.left,
                style: thirdTextStyle.copyWith(
                  letterSpacing: 1,
                  fontSize: getProportionateScreenWidth(15),
                  fontWeight: semiBold,
                ),
              ),
            ],
          ),
        ),
        SizedBox(height: getProportionateScreenHeight(10)),
        Consumer<PromoCourseProvider>(
          builder: (context, state, _) {
            if (state.state == ResultState.Loading) {
              return Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 105,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 85,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 120,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                          baseColor: Colors.white,
                          highlightColor: Colors.grey,
                          child: Container(
                            height: 15,
                            width: 85,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 105,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 85,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 120,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                          baseColor: Colors.white,
                          highlightColor: Colors.grey,
                          child: Container(
                            height: 15,
                            width: 85,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              );
            } else if (state.state == ResultState.HasData) {
              return Container(
                alignment: Alignment.centerLeft,
                margin: EdgeInsets.only(left: getProportionateScreenWidth(10)),
                height: getProportionateScreenHeight(220),
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  physics: ScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: state.result.length,
                  itemBuilder: (context, index) {
                    var promoCourse = state.result[index];
                    // var finalRating = double.parse(
                    //     (promoCourse.specificRating![0] / 20).toStringAsFixed(2));
                    int price =
                        int.tryParse(promoCourse.price.replaceAll('.', '')) ??
                            0;
                    int discountPrice = int.tryParse(
                            promoCourse.discountPrice.replaceAll('.', '')) ??
                        0;

                    int calculatedPrice = (promoCourse.discountPrice != '0')
                        ? price - discountPrice
                        : price;

                    String displayedPrice = (calculatedPrice == 0)
                        ? promoCourse.price
                        : calculatedPrice.toString();
                    return Container(
                      margin: EdgeInsets.only(
                        top: getProportionateScreenHeight(10),
                      ),
                      child: Padding(
                        padding: EdgeInsets.only(
                          right: getProportionateScreenWidth(18),
                          bottom: getProportionateScreenWidth(18),
                        ),
                        child: ProductCard(
                          totalDiscount: promoCourse.totalDiscount ?? 0,
                          students: promoCourse.students ?? '0',
                          pad: 12,
                          id: promoCourse.idCourse,
                          thumbnail: promoCourse.thumbnail ??
                              '$baseUrl/uploads/courses_thumbnail/course_thumbnail_default_57.jpg',
                          title: promoCourse.title,
                          instructorName: promoCourse.instructorName,
                          specificRating: (promoCourse.rating.isNotEmpty &&
                                  promoCourse.rating[0]?.avgRating != null)
                              ? promoCourse.rating[0]!.avgRating.toString()
                              : '0',
                          rating: (promoCourse.rating.isNotEmpty &&
                                  promoCourse.rating[0]?.avgRating != null)
                              ? promoCourse.rating[0]!.avgRating.toString()
                              : '5.0',
                          numberOfRatings: (promoCourse.rating.isNotEmpty &&
                                  promoCourse.rating[0]?.totalReview != null)
                              ? promoCourse.rating[0]!.totalReview!
                              : '0',
                          isTopCourse: promoCourse.topCourse!,
                          price: (promoCourse.price == '0')
                              ? 'Gratis'
                              : (promoCourse.promoPrice != '0')
                                  ? numberFormat(promoCourse.promoPrice)
                                  : numberFormat(displayedPrice),
                          realPrice: (promoCourse.price == '0')
                              ? ''
                              : numberFormat(promoCourse.price),
                          press: () {
                            print(promoCourse.idCourse);
                            Navigator.of(context, rootNavigator: true).push(
                              CustomNavigator(
                                child: DetailCourseScreen(
                                  isPromo: true,
                                  idcourse: promoCourse.idCourse,
                                ),
                              ),
                            );
                          },
                        ),
                      ),
                    );
                  },
                ),
              );
            } else if (state.state == ResultState.NoData) {
              return Center(child: Text(state.message));
            } else if (state.state == ResultState.Error) {
              return Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 105,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 85,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 120,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                          baseColor: Colors.white,
                          highlightColor: Colors.grey,
                          child: Container(
                            height: 15,
                            width: 85,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 105,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 85,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 190,
                              color: Colors.white,
                            )),
                        SizedBox(height: 10),
                        Shimmer.fromColors(
                            baseColor: Colors.white,
                            highlightColor: Colors.grey,
                            child: Container(
                              height: 15,
                              width: 120,
                              color: Colors.white,
                            )),
                        SizedBox(height: 8),
                        Shimmer.fromColors(
                          baseColor: Colors.white,
                          highlightColor: Colors.grey,
                          child: Container(
                            height: 15,
                            width: 85,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              );
            } else {
              return Center(child: Text(''));
            }
          },
        )
      ],
    );
  }
}
