import 'package:flutter/material.dart';
import 'package:initial_folder/providers/my_course_provider.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 SearchMyCourse extends StatelessWidget {
  const SearchMyCourse({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    TextEditingController controller = TextEditingController();

    void refreshCourseList() {
      Provider.of<MyCourseProvider>(context, listen: false)
          .getSearchMyCourse(controller.text);
    }

    return Scaffold(
      appBar: AppBar(
        scrolledUnderElevation: 0.0,
        backgroundColor: Theme.of(context).colorScheme.background,
        title: TextField(
          autofocus: true,
          decoration: new InputDecoration.collapsed(
            hintText: 'Cari Kursusku',
          ),
          controller: controller,
          onSubmitted: (value) => refreshCourseList(),
        ),
      ),
      body: RefreshIndicator(
        displacement: 40,
        color: primaryColor,
        onRefresh: () async {
          refreshCourseList();
        },
        child: Consumer<MyCourseProvider>(
          builder: (BuildContext context, state, _) {
            if (state.searchResultState == SearchResultState.Loading) {
              return SingleChildScrollView(
                child: Column(
                  children: [
                    LoadingMyCourse(),
                    LoadingMyCourse(),
                    LoadingMyCourse(),
                  ],
                ),
              );
            } else if (state.searchResultState == SearchResultState.HasData) {
              return ListView.builder(
                shrinkWrap: true,
                itemCount: state.searchResult!.data[0].length,
                itemBuilder: (context, index) {
                  var myCourse = state.searchResult!.data[0][index];

                  return MyCourseList(
                    dataMyCourseModel: myCourse,
                    onDialogClose: refreshCourseList,
                  );
                },
              );
            } else if (state.searchResultState == SearchResultState.NoData) {
              Provider.of<MyCourseProvider>(context, listen: false)
                  .clearSearch();
              return Center(
                child: Text(
                  'Kursus Tidak Ditemukan',
                  style: thirdTextStyle,
                ),
              );
            } else if (state.searchResultState == SearchResultState.Error) {
              Provider.of<MyCourseProvider>(context, listen: false)
                  .clearSearch();
              return Center(
                child: Text(
                  'Terjadi Kesalahan',
                  style: thirdTextStyle,
                ),
              );
            } else {
              return Center(child: Text(''));
            }
          },
        ),
      ),
    );
  }
}
