import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:ongbut_ntmanh/widgets/widgets_util.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import '../../../../const/colors.dart';
import '../../../../res/images/images.dart';
import '../bloc/comment_bloc.dart';
import '../model/comment_model.dart';

class CommentWidget extends StatelessWidget {
  //CommentWidget({Key? key}) : super(key: key);

  CommentModel? commentModel;
  Function(CommentModel commentModel)? onData;
  CommentWidget(this.onData, {super.key});
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<CommentBloc>(context).add(
        RequestComment(context: context));
    PageController _pageController = new PageController();
    return
      BlocConsumer<CommentBloc, CommentState>(
          listener: (context, state) async {},
          builder: (context, state) {
            return
              Container(
                color: AppColors.background,
                child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.fromLTRB(20, 0, 0, 0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('Nhan xet cua phu huynh',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                        TextButton(
                            child: Text('Xem Them  >>'),
                            onPressed: () {
                              print('Pressed');
                            }
                        )
                      ],
                    ),
                  ),
                  BlocBuilder<CommentBloc, CommentState>(
                      builder: (context, state) {
                        if(state is CommentInitial){
                          return const Text('CommentInitial');
                        }
                        if(state is CommentFailed){
                          //const CircularProgressIndicator();
                          return Text('CommentFailed');
                        }
                        if(state is CommentLoading){
                          return const Text('CommentLoading');
                        }
                        if(state is CommentSuccess){
                          commentModel = state.commentModel;
                          onData!(commentModel!);
                          return
                            Container(
                              decoration: const BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(10)),
                                boxShadow: [
                                  BoxShadow(
                                    color: AppColors.primary,
                                    spreadRadius: 0.5,
                                  ),
                                ],
                              ),
                              height: checkLandscape(context)?getHeight(context)*0.5:getHeight(context)*0.3,
                              width: double.maxFinite,
                              child:
                              PageView.builder(
                                  //itemCount: state.commentModel.data!.length,
                                  itemBuilder: (BuildContext context, int index) {
                                    return
                                      Stack(children: [
                                        Container(
                                          decoration: const BoxDecoration(
                                            borderRadius: BorderRadius.all(Radius.circular(10)),
                                            color: AppColors.white,
                                            boxShadow: [
                                              BoxShadow(
                                                color: AppColors.primary_light,
                                                spreadRadius: 1,
                                              ),
                                            ],
                                          ),
                                          child: PageView.builder(
                                            controller: _pageController,
                                            itemBuilder: (context, index) {
                                              return
                                                Column(
                                                  children: [
                                                    Padding(
                                                      padding: const EdgeInsets.all(8.0),
                                                      child: Row(children: [
                                                        ClipRRect(
                                                          borderRadius: BorderRadius.circular(50),
                                                          child: Image.network(state.commentModel.data![index].createUserAvatar!=null?state.commentModel.data![index].createUserAvatar!:Images.ongbut_happy,
                                                            fit: BoxFit.fill,height: 60,width: 60,
                                                            ),
                                                        ),
                                                        Padding(
                                                          padding: const EdgeInsets.all(8.0),
                                                          child: Column(children: [
                                                            Text(state.commentModel.data![index].createUserFullName!),
                                                            Text(state.commentModel.data![index].createUserFullName!)
                                                          ],),
                                                        )
                                                      ],),
                                                    ),
                                                    Text(state.commentModel.data![index].courseKeyword!=null?state.commentModel.data![index].courseKeyword!:'Null',style: const TextStyle(fontWeight: FontWeight.bold),),
                                                    Padding(
                                                      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                                                      child:
                                                      Text(state.commentModel.data![index].content!=null?state.commentModel.data![index].content!:'Null',style: const TextStyle(fontSize: 15),
                                                        softWrap: false,
                                                        maxLines: 4,
                                                          overflow: TextOverflow.ellipsis),
                                                    ),
                                                  ],
                                                );
                                            },
                                            itemCount: state.commentModel.data!.length,
                                          ),
                                        ),
                                        Column(
                                          mainAxisAlignment: MainAxisAlignment.end,
                                          crossAxisAlignment: CrossAxisAlignment.center,
                                          children: [
                                            Container(
                                              padding: const EdgeInsets.all(10),
                                              child: Center(child: SmoothPageIndicator(                                                controller: _pageController,
                                                count: state.commentModel.data!.length,
                                                effect: const WormEffect(),
                                                onDotClicked: (index) => _pageController.animateToPage(index, duration: const Duration(milliseconds: 1), curve: Curves.bounceOut),
                                              ),),
                                            ),
                                          ],),
                                      ],);
                                  },
                                  onPageChanged: (int index) {

                                  }),
                            );
                        }
                        return const Text('NewFailed');
                      }
                  ),
                  const SizedBox(height: 100,),
                ],
            ),
              );
          }
      );
  }
}
