import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/job/job_fav_student.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class FavoriteStudentDialog extends StatefulWidget {
  final Items item;
  final List group;
  final Function deleteFav;
  final Future<bool> Function() confirmDialog;

  const FavoriteStudentDialog(
      {Key? key,
      required this.item,
      required this.group,
      required this.deleteFav,
      required this.confirmDialog})
      : super(key: key);

  @override
  State<FavoriteStudentDialog> createState() => _FavoriteStudentDialogState();
}

class _FavoriteStudentDialogState extends State<FavoriteStudentDialog> {
  int defaultIndex = 0;
  List<Items> imageList = [];
  late PageController pageController;
  late Items currentStudent;

  void initState() {
    currentStudent = widget.item;
    List<Items> list = [];
    widget.group.forEach((element) {
      for (var item in element['list']) {
        list.add(item);
      }
    });
    setState(() {
      imageList = list;
      defaultIndex = list.indexWhere((element) => element.id == widget.item.id);
    });
    pageController = PageController(initialPage: defaultIndex);
  }

  void dispose() {
    super.dispose();
    pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 14.r),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                '${currentStudent.className} ${currentStudent.studentName}',
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF4E73FD)),
              ),
              Expanded(child: Container()),
              Text(
                '第${currentStudent.questionPage}页',
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF868686)),
              ),
              InkWell(
                onTap: () async {
                  bool confim = await widget.confirmDialog();
                  if (confim) {
                    widget.deleteFav(currentStudent);
                    imageList.removeAt(defaultIndex);
                    setState(() {
                      if (imageList.length > 0) {
                        if (defaultIndex < imageList.length) {
                          currentStudent = imageList[defaultIndex];
                        } else {
                          currentStudent = imageList[defaultIndex - 1];
                          defaultIndex = defaultIndex - 1;
                        }
                      } else {
                        currentStudent.className = '';
                        currentStudent.studentName = '';
                      }
                    });
                  }
                },
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 8.r),
                  child: Image.asset(
                    'assets/images/favorite_delete_icon.png',
                    width: 22.r,
                    height: 22.r,
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Image.asset(
                  'assets/images/favorite_dialog_close.png',
                  width: 22.r,
                  height: 22.r,
                ),
              ),
            ],
          ),
          SizedBox(
            height: 10.r,
          ),
          imageList.length > 0
              ? Expanded(
                  child: Container(
                    color: Colors.white,
                    width: MediaQuery.of(context).size.width,
                    child: PhotoViewGallery.builder(
                      scrollPhysics: const BouncingScrollPhysics(),
                      builder: (BuildContext context, int index) {
                        final Items item = imageList[index];
                        return PhotoViewGalleryPageOptions(
                          imageProvider: NetworkImage(item.questionPicture!),
                          heroAttributes: PhotoViewHeroAttributes(tag: item.id),
                        );
                      },
                      itemCount: imageList.length,
                      pageController: pageController,
                      onPageChanged: (index) {
                        setState(() {
                          defaultIndex = index;
                          currentStudent = imageList[index];
                        });
                      },
                      scrollDirection: Axis.horizontal,
                    ),
                  ),
                )
              : Padding(
                  padding: EdgeInsets.only(
                      top: MediaQuery.of(context).size.height / 2 - 200.r),
                  child: MyEmptyWidget(),
                ),
          if (imageList.length > 0)
            Padding(
              padding: EdgeInsets.symmetric(vertical: 15.r),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () {
                      if (defaultIndex > 0) {
                        setState(() {
                          defaultIndex = defaultIndex - 1;
                          pageController.jumpToPage(defaultIndex);
                        });
                      }
                    },
                    child: Container(
                      width:
                          (MediaQuery.of(context).size.width - 78.r) / 2 - 10.r,
                      height: 28.r,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(6.r)),
                        border: Border.all(
                            width: 1.r,
                            color: Color(0xFFCACACA),
                            style: BorderStyle.solid),
                      ),
                      child: Center(
                          child: Text(
                        '上一页',
                        style: TextStyle(
                            fontSize: 10.r,
                            color: defaultIndex == 0
                                ? Color(0xFFCACACA)
                                : Color(0xFF505E6E)),
                      )),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      if (defaultIndex < imageList.length - 1) {
                        setState(() {
                          defaultIndex = defaultIndex + 1;
                          pageController.jumpToPage(defaultIndex);
                        });
                      }
                    },
                    child: Container(
                      width:
                          (MediaQuery.of(context).size.width - 78.r) / 2 - 10.r,
                      height: 28.r,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(6.r)),
                        border: Border.all(
                            width: 1.r,
                            color: Color(0xFFCACACA),
                            style: BorderStyle.solid),
                      ),
                      child: Center(
                          child: Text(
                        '下一页',
                        style: TextStyle(
                            fontSize: 10.r,
                            color: defaultIndex == imageList.length - 1
                                ? Color(0xFFCACACA)
                                : Color(0xFF505E6E)),
                      )),
                    ),
                  ),
                ],
              ),
            )
        ],
      ),
    );
  }
}
