import 'package:flutter/material.dart';
import 'package:initial_folder/helper/validator.dart';
import 'package:initial_folder/providers/others_course_provider.dart';
import 'package:initial_folder/screens/detail_course/components/app_bar.dart';
import 'package:initial_folder/screens/detail_course/detail_course_screen.dart';
import 'package:initial_folder/theme.dart';
import 'package:provider/provider.dart';
import 'others_course.dart';
import 'product_card/product_card.dart';
import '../../../../size_config.dart';
import 'section_title.dart';
import 'package:intl/intl.dart';

class LihatSemuaKursus extends StatefulWidget {
  static String routeName = "/lihatKursus";

  const LihatSemuaKursus({Key? key}) : super(key: key);

  @override
  State<LihatSemuaKursus> createState() => _LihatSemuaKursusState();
}

class _LihatSemuaKursusState extends State<LihatSemuaKursus> {
  ScrollController _controller = ScrollController();

  @override
  void initState() {
    _controller.addListener(() {
      onScroll();
    });
  }

  void onScroll() async {
    double maxScroll = _controller.position.maxScrollExtent;
    double currentScroll = _controller.position.pixels;
    if (maxScroll == currentScroll) {
      await Provider.of<OthersCourseProvider>(context, listen: false)
          .getOthersCourses();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (_controller.hasClients) {
      if (_controller.position.pixels != _controller.position.maxScrollExtent) {
        _controller.animateTo(0,
            duration: Duration(milliseconds: 900), curve: Curves.linear);
      }
    }
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          scrolledUnderElevation: 0.0,
          backgroundColor: Theme.of(context).colorScheme.background,
          title: Text(
            "Kursus Lainnya",
            style: thirdTextStyle.copyWith(
                fontWeight: bold, fontSize: getProportionateScreenWidth(16)),
          ),
        ),
        body: Column(
          children: [
            Consumer<OthersCourseProvider>(builder: (context, state, _) {
              if (state.state == ResultState.Loading) {
                return Center(
                  child: CircularProgressIndicator(
                    color: secondaryColor,
                    strokeWidth: 2,
                  ),
                );
              } else if (state.state == ResultState.HasData) {
                return Expanded(
                  child: ListView(controller: _controller, children: [
                    OthersCourse(
                      key: PageStorageKey<String>('lihatKursus'),
                      showTitle: true,
                    ),
                    Provider.of<OthersCourseProvider>(context).loading
                        ? Center(
                            child: SizedBox(
                              height: 30,
                              width: 30,
                              child: CircularProgressIndicator(
                                color: secondaryColor,
                                strokeWidth: 2,
                              ),
                            ),
                          )
                        : SizedBox(height: 30),
                    SizedBox(height: 25),
                  ]),
                );
              } else if (state.state == ResultState.NoData) {
                return Center(child: Text(state.message));
              } else if (state.state == ResultState.Error) {
                return Center(
                    child: Column(
                  children: [
                    Text(
                      'Terjadi Kesalahan Coba Lagi',
                      style: thirdTextStyle,
                    ),
                  ],
                ));
              } else {
                return Center(child: Text(''));
              }
            })
          ],
        ),
      ),
    );
  }
}

// courseProvider.course
//               .map(
//                 (product) => ProductCard(
//                   product: product,
//                 ),
//               )
//               .toList(),
