import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/app/module/coursedetail/model/chapters.dart';
import 'package:ongbut_ntmanh/app/module/coursedetail/model/course_detail_model.dart';
import 'package:ongbut_ntmanh/const/colors.dart';
import 'package:ongbut_ntmanh/widgets/widgets_util.dart';
import '../../../../res/images/images.dart';
import '../bloc/course_detail_bloc.dart';

// dang loi
class CourseDetailWidget extends StatelessWidget {
  String courseId;
  String image;
  CourseDetailModel? courseDetailModel;
  List<Chapters> chapters = [];
  CourseDetailWidget(this.courseId,this.image, {super.key});


  @override
  Widget build(BuildContext context) {
    BlocProvider.of<CourseDetailBloc>(context).add(
        RequestCourseDetail(context: context,idCourse: courseId));
    return

      Scaffold(
        appBar: AppBar(title: const Text('Chi Tiet Khoa Hoc'),),
        body:
        Column(children: [
          Image.network(image, fit: BoxFit.fill,height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.3,width: double.maxFinite,
            errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
              return Image.asset(Images.load_err,fit: BoxFit.fill,height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.3,width: double.maxFinite,);
            },
          ),
          Row(
            children: [
              const Text('ID Course = ',style: TextStyle(fontSize: 15,color: AppColors.red_light)),
              Text(courseId),
            ],
          ),
          Expanded(
            child: BlocConsumer<CourseDetailBloc, CourseDetailState>(
                listener: (context, state) async {},
                builder: (context, state) {
                  return
                    BlocBuilder<CourseDetailBloc, CourseDetailState>(
                        builder: (context, state) {
                          if(state is CourseDetailInitial){
                            return const Text('SchoolInitial');
                          }
                          if(state is CourseDetailFailed){
                            return Text(state.message.toString());
                          }
                          if(state is CourseDetailLoading){
                            return const CircularProgressIndicator();
                          }
                          if(state is CourseDetailSuccess){
                            chapters = state.courseDetailModel.data!.chapters!;
                            if(chapters.isEmpty){
                              return const Text('Chua Co Noi Dung Khoa Hoc',style: TextStyle(fontSize: 15,color: AppColors.red_light),);
                            }
                            if(chapters.isNotEmpty){
                              return
                                Column(
                                  mainAxisSize: MainAxisSize.max,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                  Padding(
                                    padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
                                    child: Column(
                                      children: [
                                        // Text(chapters[0].name.toString(),style: const TextStyle(fontSize: 15,fontWeight: FontWeight.bold),
                                        //     softWrap: false,
                                        //     maxLines: 1,
                                        //     overflow: TextOverflow.ellipsis),
                                        Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            const Text('Thong Tin chi tiet',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                                            TextButton(
                                                child: Text('Nhan xet ve khoa hoc'),
                                                onPressed: () {
                                                  print('Pressed');
                                                }
                                            )
                                          ],
                                        ),

                                      ],
                                    ),
                                  ),
                                  Text(chapters.length.toString()),
                                  Expanded(
                                    child: Padding(
                                      padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
                                      child:
                                      ListView.builder(
                                          itemCount: chapters.length,
                                          physics: const AlwaysScrollableScrollPhysics(),
                                          itemBuilder: (context, index) {
                                            return
                                              ExpansionTile(
                                                title: Text(chapters[index].name!),
                                              );
                                          },
                                        ),
                                    ),
                                  ),
                                    // ExpansionTile(
                                    //   title: null,
                                    //
                                    // ),

                                  // Text(chapters[0].listLesson![0].name!),
                                  // Text(chapters[0].listLesson![0].createUser!),
                                  // Text(chapters[0].listLesson![0].listPart![0].partType!),
                                  // LessionWidget( chapters[0].listLesson![0].lessonId!=null?chapters[0].listLesson![0].lessonId!
                                  //     :'fb228efa-7b32-419e-98b4-a6d5135f4361' ),
                                ],);
                            }
                          }
                          return const Text('Default CourseDetail!!!!!');
                        }
                    );
                }
            ),
          ),
        ],)
      );
  }
}


