import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/config/request_config.dart';
import 'package:making_school_asignment_app/common/job/annotated_class.dart';
import 'package:making_school_asignment_app/page/global_widget/MyEmptyWidget.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

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

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

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

class _FavoriteStudentDialogState extends State<FavoriteStudentDialog> {
  RxInt defaultIndex = 0.obs;
  RxList<HomeworkFavs> imageList = RxList();
  late PageController pageController;
  late Rx<HomeworkFavs> currentStudent = Rx(HomeworkFavs('', '', '', -1, -1, '', '', -1, '', -1, '-1', '', ''));

  @override
  void initState() {
    super.initState();
    currentStudent.value = widget.item;
    List<HomeworkFavs> list = [];
    for (var element in widget.group) {
      for (var item in element['list']) {
        list.add(item);
      }
    }
    imageList.value = list;
    defaultIndex.value = list.indexWhere((element) => element.id == widget.item.id);

    pageController = PageController(initialPage: defaultIndex.value);
  }

  @override
  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: [
              Obx(() {
                return Text(
                  '${currentStudent.value.className} ${currentStudent.value.studentName}',
                  style: TextStyle(fontSize: 12.sp, color: Theme.of(context).primaryColor),
                );
              }),
              Expanded(child: Container()),
              Obx(() {
                return Text(
                  '第${currentStudent.value.questionNo}页',
                  style: TextStyle(fontSize: 12.sp, color: const Color(0xFF868686)),
                );
              }),
              InkWell(
                onTap: () async {
                  bool confim = await widget.confirmDialog();
                  if (confim) {
                    widget.deleteFav(currentStudent.value);
                    imageList.removeAt(defaultIndex.value);
                    if (imageList.isNotEmpty) {
                      if (defaultIndex.value < imageList.length) {
                        currentStudent.value = imageList[defaultIndex.value];
                      } else {
                        currentStudent.value = imageList[defaultIndex.value - 1];
                        defaultIndex = defaultIndex - 1;
                      }
                    } else {
                      currentStudent.value.className = '';
                      currentStudent.value.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,
          ),
          Obx(() {
            return imageList.isNotEmpty
                ? Expanded(
                    child: Container(
                      color: Colors.white,
                      width: MediaQuery.of(context).size.width,
                      child: PhotoViewGallery.builder(
                        scrollPhysics: const BouncingScrollPhysics(),
                        builder: (BuildContext context, int index) {
                          final HomeworkFavs item = imageList[index];
                          return PhotoViewGalleryPageOptions(
                            imageProvider: NetworkImage(RequestConfig.imgUrl + item.zgtAnswer!),
                            heroAttributes: PhotoViewHeroAttributes(tag: item.id!),
                          );
                        },
                        itemCount: imageList.length,
                        pageController: pageController,
                        onPageChanged: (index) {
                          defaultIndex.value = index;
                          currentStudent.value = imageList[index];
                        },
                        scrollDirection: Axis.horizontal,
                      ),
                    ),
                  )
                : Padding(
                    padding: EdgeInsets.only(top: MediaQuery.of(context).size.height / 2 - 200.r),
                    child: const MyEmptyWidget(),
                  );
          }),
          Obx(() {
            return imageList.isNotEmpty
                ? Padding(
                    padding: EdgeInsets.symmetric(vertical: 15.r),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        InkWell(
                          onTap: () {
                            if (defaultIndex.value > 0) {
                              defaultIndex.value = defaultIndex.value - 1;
                              pageController.jumpToPage(defaultIndex.value);
                            }
                          },
                          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: const Color(0xFFCACACA), style: BorderStyle.solid),
                            ),
                            child: Center(
                                child: Text(
                              '上一页',
                              style: TextStyle(
                                  fontSize: 10.r,
                                  color: defaultIndex == 0 ? const Color(0xFFCACACA) : const Color(0xFF505E6E)),
                            )),
                          ),
                        ),
                        InkWell(
                          onTap: () {
                            if (defaultIndex.value < imageList.length - 1) {
                              defaultIndex.value = defaultIndex.value + 1;
                              pageController.jumpToPage(defaultIndex.value);
                            }
                          },
                          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: const Color(0xFFCACACA), style: BorderStyle.solid),
                            ),
                            child: Center(
                                child: Text(
                              '下一页',
                              style: TextStyle(
                                  fontSize: 10.r,
                                  color: defaultIndex == imageList.length - 1
                                      ? const Color(0xFFCACACA)
                                      : const Color(0xFF505E6E)),
                            )),
                          ),
                        ),
                      ],
                    ),
                  )
                : Container();
          })
        ],
      ),
    );
  }
}
