import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../const/colors.dart';
import '../../../../res/images/images.dart';
import '../../playvideo/page/play_video.dart';
import '../bloc/lesson_bloc.dart';
import '../model/lesson_model.dart';

class LessionWidget extends StatefulWidget {
  String lessonID;

   LessionWidget( this.lessonID, {Key? key}) : super(key: key);

  @override
  State<LessionWidget> createState() => _LessionWidgetState();
}

class _LessionWidgetState extends State<LessionWidget> {
  LessonModel? lessonModel;
  String pl = 'Playlist video';
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<LessonBloc>(context).add(
        RequestLesson(context: context, lessonID: widget.lessonID));
    return
      BlocConsumer<LessonBloc, LessonState>(
          listener: (context, state) async {},
          builder: (context, state) {
            return
              BlocBuilder<LessonBloc, LessonState>(
                  builder: (context, state) {
                    if(state is LessonInitial){
                      return const Text('LessonInitial');
                    }
                    if(state is LessonFailed){
                      return const Text('LessonFailed');
                    }
                    if(state is LessonLoading){
                      return const CircularProgressIndicator();
                    }
                    if(state is LessonSuccess){
                      return
                        ListView.builder(
                            shrinkWrap: true,
                            itemCount: state.lessonModel.data!.length,
                            itemBuilder: (context, index){
                              return
                                InkWell(
                                  onTap: (){
                                    PlayVideoPage(link: state.lessonModel.data![index].playlist![0].link!);
                                  },
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.fromLTRB(20, 3, 0, 3),
                                        child: Image.asset(Images.o,height: 10,width: 10,)),
                                      Padding(
                                      padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                                      child: Text(state.lessonModel.data![index].title!=null?state.lessonModel.data![index].title!:'Null'),),
                                      Padding(
                                        padding: const EdgeInsets.fromLTRB(20, 3, 20, 3),
                                        child: Image.asset(Images.see,height: 20,width: 20,),
                                      ),

                                  ],),
                                );
                              // Column(children: [
                              //   Text(state.lessonModel.data![index].title!),
                              //   // TextButton(
                              //   //     onPressed: (){
                              //   //       Navigator.push(
                              //   //         context,
                              //   //         MaterialPageRoute(builder: (context) => PlayVideoPage(link: state.lessonModel.data![index].playlist![0].link!)),
                              //   //       );
                              //   //     },
                              //   //     child:
                              //   //     Text(state.lessonModel.data![index].title == pl ?state.lessonModel.data![index].playlist![0].link!:'...'),
                              //   // ),
                              // ],);

                            }
                        );
                    }
                    return const Text('NewFailed');
                  }
              );
          }
      );
  }
}
