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/lession/widgets/lession_widget.dart';

import '../model/data.dart';
import '../bloc/course_detail_bloc.dart';


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:
        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){

                        // Image.network('https://oss.myitsol.com/imes-public/2022/12/11/neymar_145527553.PNG',
                        //   fit: BoxFit.fill,height: 200,width: double.maxFinite,);
                        chapters = state.courseDetailModel.data!.chapters!;
                        if(chapters.isEmpty == true){
                          return const Text('Chua Co Noi Dung Khoa Hoc');
                        }
                        if(chapters.isEmpty == false){
                          return
                              Column(children: [
                                Image.network(image!=null?image:'https://oss.myitsol.com/imes-public/2022/12/11/neymar_145527553.PNG',
                                  fit: BoxFit.fill,height: 200,width: double.maxFinite,),
                                LessionWidget( chapters[0].listLesson![0].lessonId! ),
                              ],);
                            // LessionWidget( chapters[0].listLesson![0].lessonId! );

                        }
                      }
                      return const Text('Default CourseDetail!!!!!');
                    }
                );
            }
        ),


      );
  }
}


