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/app/module/coursedetail/model/list_lesson.dart';
import 'package:ongbut_ntmanh/app/module/lession/widgets/lession_widget.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';

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

  @override
  Widget build(BuildContext context) {
      CourseDetailBloc bloc = CourseDetailBloc()..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.26,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.26,width: double.maxFinite,);
              },
            ),
            Expanded(
              child:
              BlocBuilder<CourseDetailBloc, CourseDetailState>(
                  bloc: bloc,
                  builder: (context, state) {
                    if(state is CourseDetailInitial){
                      return const CircularProgressIndicator();
                    }
                    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, 0, 0, 0),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.fromLTRB(10, 0, 0, 0),
                                      child: Text(name,style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                                      child: Image.asset(Images.rate,height: 20,width: 150,),
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        TextButton(onPressed: (){},
                                            child: Row(
                                              children: [

                                                 Text('Thong Tin chi tiet',
                                                  style: TextStyle(fontSize: 15),),
                                              ],
                                            )),
                                        // const Text('Thong Tin chi tiet',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                                        TextButton(
                                            child: const Text('Nhan xet ve khoa hoc',style: TextStyle(color: AppColors.black),),
                                            onPressed: () {
                                              print('Pressed');
                                            }
                                        )
                                      ],
                                    ),
                                  ],
                                ),
                              ),
                              Expanded(
                                child: Padding(
                                  padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                                  child:
                                  ListView.builder(
                                    itemCount: chapters.length,
                                    physics: const AlwaysScrollableScrollPhysics(),
                                    itemBuilder: (context, index) {
                                      return
                                        ExpansionTile(
                                          textColor: AppColors.primary,
                                          iconColor: AppColors.red_light,
                                          backgroundColor: AppColors.white,
                                          title: Text(chapters[index].name!,style: TextStyle(color: AppColors.semi_grey),),
                                          children: [
                                            chapters[index].listLesson!.isNotEmpty?_buildLesion(chapters[index]):const Text(''),
                                          ],
                                        );
                                    },
                                  ),
                                ),
                              ),
                            ],);
                      }
                    }
                    return const Text('Default CourseDetail!!!!!');
                  }
              ),
            ),
          ],)
      );
  }

  ListView _buildLesion(Chapters chapters){
    return
      ListView.builder(
          shrinkWrap: true,
          itemCount: chapters.listLesson!.length,
          itemBuilder: (context, index){
            return
              InkWell(
                onTap: (){
                  chapters.listLesson![0].free == true?
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => LessionWidget(lessonID: chapters.listLesson![index].lessonId!)),
                  ):const Text('');
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Padding(
                        padding: const EdgeInsets.fromLTRB(30, 3, 0, 3),
                        child: Image.asset(Images.o,height: 10,width: 10,)),
                    Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child:
                      Container(
                        width: checkLandscape(context)?getWidth(context)*0.25:getHeight(context)*0.25,
                        child: Text(chapters.listLesson![index].name!=null?chapters.listLesson![index].name!:'',style:  TextStyle(fontSize: 14, overflow: TextOverflow.ellipsis),
                          softWrap: false,
                          maxLines: 1,),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.fromLTRB(20, 3, 20, 3),
                      child: chapters.listLesson![0].free == true ?Padding(
                        padding: const EdgeInsets.fromLTRB(20, 3, 20, 3),
                        child: Image.asset(Images.see,height: 20,width: 20,),
                      ):Padding(
                        padding: const EdgeInsets.fromLTRB(20, 3, 20, 3),
                        child: Image.asset(Images.uneye,height: 20,width: 20,),
                      ),
                    ),
                  ],),
              );
          }
      );
  }
}