import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:ongbut_ntmanh/app/module/course/model/list_data.dart';
import 'package:ongbut_ntmanh/const/colors.dart';

import '../../../../res/images/images.dart';
import '../../../ui/search_page.dart';
import '../../coursedetail/widgets/course_detail.dart';
import '../bloc/course_bloc.dart';
import '../model/course_model.dart';


class CourseWidget extends StatelessWidget {
  CourseWidget({Key? key}) : super(key: key);
  CourseModel? courseModel;
  List<ListData> listData = [];
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<CourseBloc>(context).add(
        RequestCourse(context: context));
    return
      BlocConsumer<CourseBloc, CourseState>(
        listener: (context, state) async {},
        builder: (context, state) {
          return Container(
            color: AppColors.background,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 0, 0, 0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Tham Gia Khoa Hoc Thu',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                      TextButton(
                          child: const Text('Xem Them>>'),
                          onPressed: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (_) => SearchWidget(),

                                )).then((value) {
                              if (value != null && value is bool && value) {}
                            });
                          }
                      )
                    ],
                  ),
                ),
                BlocBuilder<CourseBloc, CourseState>(
                    builder: (context, state) {
                      if(state is CourseInitial){
                        const CircularProgressIndicator();
                      }
                      if(state is CourseLoading){
                        return const CircularProgressIndicator();
                      }
                      if(state is CourseFailed){
                        return Text(state.message.toString());
                      }
                      if(state is CourseSuccess){
                        listData = state.courseModel.data!.listData!;
                        return
                          Swiper(
                            itemHeight: 340,
                            itemWidth: double.maxFinite,
                            layout: SwiperLayout.TINDER,

                            itemBuilder: (BuildContext context, int index) {
                              return
                                Container(
                                  decoration: const BoxDecoration(
                                    boxShadow: [
                                      BoxShadow(
                                        color: AppColors.primary,
                                        spreadRadius: 1,
                                      ),
                                    ],
                                    color: Colors.white,
                                    borderRadius: BorderRadius.all(Radius.circular(10)),
                                  ),
                                  child: Column(children: [

                                    ClipRRect(
                                      borderRadius: BorderRadius.circular(8.0),
                                      child:
                                      Image.network(listData[index].image!=null?listData[index].image!:'https://oss.myitsol.com/imes-public/2022/12/11/neymar_145527553.PNG',
                                        fit: BoxFit.fill,height: 200,width: double.maxFinite,
                                        errorBuilder: (BuildContext context, Object exception,
                                            StackTrace? stackTrace) {
                                          return Image.asset(Images.load_err);
                                        },
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.all(10.0),
                                      child:
                                      Text(listData[index].type!),
                                      //Text('abc'),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.all(10.0),
                                      child:
                                      Text(listData[index].name!),
                                      //Text('123'),
                                    ),
                                  ],),
                                );
                            },

                            itemCount: listData.length,
                            onTap: (index){

                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (_) => CourseDetailWidget(listData[index].courseId!,listData[index].image!=null?listData[index].image!
                                          :'https://oss.myitsol.com/imes-public/2022/12/11/neymar_145527553.PNG'),

                                  )).then((value) {
                                if (value != null && value is bool && value) {}
                              });
                            },

                          );
                      }
                      return const Text('CourseFailed');
                    }
                )
              ],
            ),
          );
        }
      );
  }
}
