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 '../../../../widgets/widgets_util.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 Column(
            children: [
              Padding(
                padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
                child: Row(
                  children: [
                    const Text('Tham Gia Khoa Hoc Thu',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                    const SizedBox(width: 100),
                    TextButton(
                        child: const Text('Xem Them>>'),
                        onPressed: () {
                          print('Pressed');
                        }
                    )
                  ],
                ),
              ),
              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
                        //Text(listData.length.toString());
                        Swiper(
                          itemHeight: 340,
                          itemWidth: checkLandscape(context)?getWidth(context)*1:getWidth(context)*1,
                          layout: SwiperLayout.TINDER,
                          itemBuilder: (BuildContext context, int index) {
                            return
                              Container(
                                height: 300,
                                width: 300,
                                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 const Text('😢');
                                      },
                                    ),
                                  ),

                                  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,
                          viewportFraction: 0.8,
                          scale: 0.9,
                          // state.courseModel.data!.listData!.length,

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