import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:initial_folder/providers/my_course_provider.dart';
import 'package:initial_folder/providers/theme_provider.dart';
import 'package:initial_folder/screens/course/search_my_course_page.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/size_config.dart';
import 'package:initial_folder/widgets/loading/loading_my_course.dart';
import 'package:initial_folder/widgets/my_course_list.dart';
import 'package:provider/provider.dart';
import '../../theme.dart';

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

  @override
  Widget build(BuildContext context) {
     final themeProvider = Provider.of<ThemeProvider>(context);
    Widget notLogin() {
      return Center(
        child: Container(
          margin: EdgeInsets.symmetric(
            horizontal: getProportionateScreenWidth(40),
          ),
          child: Text(
            'Kamu belum login, silahkan login untuk melihat kursus mu',
            textAlign: TextAlign.center,
            style: primaryTextStyle,
          ),
        ),
      );
    }

    Widget myCourse() {
      return RefreshIndicator(
        displacement: 40,
        color: primaryColor,
        onRefresh: () async {
          await Provider.of<MyCourseProvider>(context, listen: false)
              .getMyCourse();
        },
        child: Consumer<MyCourseProvider>(
          builder: (BuildContext context, state, _) {
            if (state.state == ResultState.Loading) {
              return Column(
                children: [
                  LoadingMyCourse(),
                  LoadingMyCourse(),
                  LoadingMyCourse(),
                ],
              );
            } else if (state.state == ResultState.HasData) {
              return Container(
                margin:
                    EdgeInsets.only(bottom: getProportionateScreenHeight(4)),
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: state.result!.data[0].length,
                  itemBuilder: (context, index) {
                    var myCourse = state.result!.data[0][index];

                    return MyCourseList(
                      dataMyCourseModel: myCourse,
                    );
                  },
                ),
              );
            } else if (state.state == ResultState.NoData) {
              return ListView(
                children: [
                  Container(
                    child: Center(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          SizedBox(height: getProportionateScreenHeight(47)),
                          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: getProportionateScreenHeight(15)),
                          Text(
                            "Kursus tidak tersedia",
                            style: secondaryTextStyle.copyWith(
                              letterSpacing: 1,
                              fontWeight: semiBold,
                              fontSize: getProportionateScreenWidth(14),
                              color: tenthColor,
                            ),
                          ),
                          SizedBox(height: getProportionateScreenHeight(3)),
                          Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: getProportionateScreenWidth(16)),
                            child: Text(
                              "Kamu belum memiliki kursus, daftar kursus sekarang agar kamu dapat mengikuti kursus",
                              textAlign: TextAlign.center,
                              style: thirdTextStyle.copyWith(
                                fontWeight: semiBold,
                                fontSize: getProportionateScreenWidth(12),
                              ),
                            ),
                          ),
                          SizedBox(height: getProportionateScreenHeight(15)),
                          PopulerCourse(text: 'Kursus Teratas'),
                          LatestCourse(text: 'Kursus Terbaru'),
                        ],
                      ),
                    ),
                  ),
                ],
              );
            } else if (state.state == ResultState.Error) {
              return Center(
                child: ListView(
                  children: [
                    Container(
                      padding: const EdgeInsets.all(20.0),
                      constraints: BoxConstraints(
                          minHeight: MediaQuery.of(context).size.height / 1.5),
                      child: Center(
                        child: Text('Terjadi Kesalahan'),
                      ),
                    )
                  ],
                ),
              );
            } else {
              return Center(child: Text(''));
            }
          },
        ),
      );
    }

    return Scaffold(
      appBar: (Condition.loginEmail || Condition.loginFirebase)
          ? AppBar(
              scrolledUnderElevation: 0.0,
              backgroundColor: Theme.of(context).colorScheme.background,
              title: Text(
                'Kursusku',
                style: thirdTextStyle.copyWith(
                  fontWeight: semiBold,
                  letterSpacing: 1,
                  fontSize: getProportionateScreenWidth(20),
                ),
              ),
              actions: [
                IconButton(
                  onPressed: () {
                    Provider.of<MyCourseProvider>(context, listen: false)
                        .clearSearch();
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => SearchMyCourse(),
                      ),
                    );
                  },
                  icon: Icon(
                    FeatherIcons.search,
                    color: themeProvider.themeData == ThemeClass.darkmode
                              ?primaryColor : primaryColorligtmode,
                  ),
                ),
                SizedBox(width: getProportionateScreenWidth(5)),
              ],
            )
          : AppBar(
              scrolledUnderElevation: 0.0,
              backgroundColor: Theme.of(context).colorScheme.background,
              title: Text(
                'Kursusku',
                style: secondaryTextStyle.copyWith(
                  fontWeight: semiBold,
                  letterSpacing: 2.3,
                  fontSize: getProportionateScreenWidth(16),
                ),
              ),
            ),
      body: (Condition.loginEmail || Condition.loginFirebase)
          ? myCourse()
          : notLogin(),
    );
  }
}
