import 'package:flutter/material.dart';
import 'package:initial_folder/providers/whislist_provider.dart';
import 'package:initial_folder/screens/home/components/body_comp/latest_course.dart';
import 'package:initial_folder/screens/home/components/body_comp/populer_course.dart';
import 'package:initial_folder/screens/splash/splash_screen_login.dart';
import 'package:initial_folder/widgets/wishlist_page.dart';
import 'package:provider/provider.dart';
import '../../size_config.dart';
import '../../theme.dart';

class WishlistPage extends StatelessWidget {
  const WishlistPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Brightness brightnessValue =
        MediaQuery.of(context).platformBrightness;
    bool isDarkMode = brightnessValue == Brightness.dark;
    Widget noWishlist() {
      return Center(
        child: Container(
          margin: EdgeInsets.symmetric(
            horizontal: getProportionateScreenWidth(40),
          ),
          child: Text(
            'Kamu belum login, silahkan login untuk melihat wishlist',
            textAlign: TextAlign.center,
            style: primaryTextStyle,
          ),
        ),
      );
    }

    Widget wishlist() {
      return Consumer<WishlistProvider>(
        builder: (BuildContext context, state, _) {
          if (state.state == ResultState.Loading) {
            return Center(
              child: CircularProgressIndicator(
                color: secondaryColor,
                strokeWidth: 2,
              ),
            );
          } else if (state.state == ResultState.NoData) {
            print("State is NoData - showing empty wishlist message");
            return SingleChildScrollView(
              child: Center(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    SizedBox(height: 35),
                    Container(
                      width: getProportionateScreenWidth(120),
                      height: getProportionateScreenWidth(120),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(2),
                      ),
                      child: Image.asset(
                        'assets/images/kursuskosong.png',
                        scale: 1,
                        color: Theme.of(context).colorScheme.onBackground,
                      ),
                    ),
                    SizedBox(height: 16),
                    Text(
                      "Tidak ada wishlist",
                      style: thirdTextStyle.copyWith(
                        fontWeight: semiBold,
                        letterSpacing: 1,
                        fontSize: getProportionateScreenWidth(14),
                      ),
                    ),
                    SizedBox(height: 4),
                    Container(
                      padding: EdgeInsets.symmetric(
                          horizontal: getProportionateScreenWidth(16)),
                      child: Text(
                        "Kamu belum memiliki wishlist, cari kursus sekarang untuk menyimpan kursus yang ingin dibeli nanti",
                        textAlign: TextAlign.center,
                        style: thirdTextStyle.copyWith(
                          fontWeight: semiBold,
                          fontSize: getProportionateScreenWidth(12),
                        ),
                      ),
                    ),
                    SizedBox(height: getProportionateScreenHeight(25)),
                    PopulerCourse(
                      text: 'Kursus Teratas',
                    ),
                    LatestCourse(
                      text: 'Kursus Terbaru',
                    ),
                  ],
                ),
              ),
            );
          } else if (state.state == ResultState.HasData) {
            print("State punya data");
            return RefreshIndicator(
              displacement: 40,
              color: primaryColor,
              onRefresh: () async {
                await Provider.of<WishlistProvider>(context, listen: false)
                    .getWishlist();
              },
              child: SingleChildScrollView(
                physics:
                    AlwaysScrollableScrollPhysics(), // Memastikan SingleChildScrollView bisa selalu scroll
                child: Column(
                  children: [
                    Container(
                        decoration: BoxDecoration(
                            color:
                                Theme.of(context).colorScheme.primaryContainer,
                            borderRadius: BorderRadius.vertical(
                                bottom: Radius.circular(15)),
                            boxShadow: [
                              BoxShadow(
                                color: Theme.of(context).brightness ==
                                        Brightness.dark
                                    ? Colors.transparent
                                    : Colors.grey,
                                spreadRadius: 0.06,
                                blurRadius: 5,
                                offset: Offset(0, 2), // Shadow position
                              ),
                            ]),
                        child: Padding(
                          padding: EdgeInsets.only(
                              top: getProportionateScreenHeight(20),
                              bottom: getProportionateScreenHeight(20)),
                          child: ListView.builder(
                            shrinkWrap: true,
                            physics:
                                NeverScrollableScrollPhysics(), // ListView tidak perlu scroll sendiri karena sudah di dalam SingleChildScrollView
                            itemCount: state.result!.data[0].length,
                            itemBuilder: (context, index) {
                              var wishlistCourse = state.result!.data[0][index];
                              return MyWishlistPage(
                                wishlistDataModel: wishlistCourse,
                              );
                            },
                          ),
                        )),
                    SizedBox(
                      height: getProportionateScreenHeight(25),
                    ),
                    PopulerCourse(
                      text: 'Kursus Teratas',
                    ),
                    LatestCourse(
                      text: 'Kursus Terbaru',
                    ),
                  ],
                ),
              ),
            );
          } else if (state.state == ResultState.Error) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Coba Lagi'),
                IconButton(
                    onPressed: () async {
                      await Provider.of<WishlistProvider>(context,
                              listen: false)
                          .getWishlist();
                    },
                    icon: Icon(Icons.refresh))
              ],
            );
          } else {
            return Center(child: Text(''));
          }
        },
      );
    }

    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Theme.of(context).colorScheme.background,
        title: Text('Wishlist',
            style: thirdTextStyle.copyWith(
                fontWeight: semiBold,
                letterSpacing: 2.3,
                fontSize: getProportionateScreenWidth(20))),
      ),
      body: Container(
          child: (Condition.loginEmail || Condition.loginFirebase)
              ? wishlist()
              : noWishlist()),
    );
  }
}
