import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../const/colors.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
      // Container(child: Text(lessonID.toString()),);
      SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(
              children: [
                Text('ID Lesson = ',style: TextStyle(fontSize: 15,color: AppColors.red_light)),
                Text(widget.lessonID),
              ],
            ),

            // Padding(
            //   padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
            //   child: Row(
            //     children: [
            //       const Text('Thong Tin chi tiet',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
            //       const SizedBox(width: 100),
            //       TextButton(
            //           child: Text('Nhan xet ve khoa hoc'),
            //           onPressed: () {
            //             print('Pressed');
            //           }
            //       )
            //     ],
            //   ),
            // ),
            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){
                        //const CircularProgressIndicator();
                        return const Text('LessonFailed');
                      }
                      if(state is LessonLoading){
                        return const CircularProgressIndicator();
                      }
                      if(state is LessonSuccess){
                        return
                          // Column(children: [
                          //   Text(state.lessonModel.data![2].title!=null?state.lessonModel.data![2].title!:'khong co Video'),
                          //   //Text(state.lessonModel.data![index].playlist![0]!=null?state.lessonModel.data![index].playlist![0].link!:'....'),
                          //   TextButton(
                          //     onPressed: (){
                          //       Navigator.push(
                          //         context,
                          //         MaterialPageRoute(builder: (context) => PlayVideoPage(link: state.lessonModel.data![2].playlist![0].link!)),
                          //       );
                          //     },
                          //     child:
                          //     Text(state.lessonModel.data![2].title == pl ?state.lessonModel.data![2].playlist![0].link!:'...'),
                          //     // Text(state.lessonModel.data![index].playlist![0]!=null?state.lessonModel.data![index].playlist![0].link!:'....'),
                          //   ),
                          // ],);
                        ListView.builder(
                            shrinkWrap: true,
                            itemCount: state.lessonModel.data!.length,
                            itemBuilder: (context, index){
                              return
                                Column(children: [
                                  // Text(state.lessonModel.data![index].title!=null?state.lessonModel.data![index].title!:'khong co du lieu'),
                                  //Text(state.lessonModel.data![index].playlist![0]!=null?state.lessonModel.data![index].playlist![0].link!:'....'),

                                  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!:'...'),
                                      // Text(state.lessonModel.data![index].playlist![0]!=null?state.lessonModel.data![index].playlist![0].link!:'....'),
                                  ),
                                ],);

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


  //  onPressHander()  {
  //   setState(() {
  //     Container(
  //         height: 200,
  //         width: 200,
  //         child: const Text('111111',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 30),));
  //   });
  // }
}
