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/coursedetail/model/course_detail_model.dart';
import 'package:ongbut_ntmanh/app/module/school/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/school_bloc.dart';
import '../model/school_model.dart';

class SchoolWidget extends StatelessWidget {
  CourseDetailModel? courseDetailModel;
  SchoolModel? schoolModel;
  List<ListData> listData = [];
  // tao function gan listData vao onData de tra du lieu khi Widget khac goi den Widget nay
  Function(List<ListData> listData)? onData;
  SchoolWidget(this.onData, {super.key});

  @override
  Widget build(BuildContext context) {
    BlocProvider.of<SchoolBloc>(context).add(
        RequestSchool(context: context));
    return
      BlocConsumer<SchoolBloc, SchoolState>(
        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: Text('Xem Them  >>'),
                          onPressed: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (_) => SearchWidget(),

                                )).then((value) {
                              if (value != null && value is bool && value) {}
                            });
                          }
                      )
                    ],
                  ),
                ),
                BlocBuilder<SchoolBloc, SchoolState>(
                    builder: (context, state) {
                      if(state is SchoolInitial){
                        return const Text('SchoolInitial');
                      }
                      if(state is SchoolFailed){
                        const CircularProgressIndicator();
                        return Text(state.message.toString());
                      }
                      if(state is SchoolLoading){
                        return const CircularProgressIndicator();
                      }
                      if(state is SchoolSuccess){
                        listData = state.schoolModel.data!.listData!;
                        onData!(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!,
                                        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!),

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

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


