import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/annotated_class.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/global_widget/ReturnToHomepage.dart';
import 'package:making_school_asignment_app/page/home_page/children/annotate_class/widget/annotate_item.dart';

import 'annotate_class_logic.dart';
import 'widget/completed_annotate_item.dart';

class AnnotateClassPage extends StatefulWidget {
  const AnnotateClassPage({super.key});

  @override
  State<AnnotateClassPage> createState() => _AnnotateClassPageState();
}

class _AnnotateClassPageState extends State<AnnotateClassPage> {
  final logic = Get.find<AnnotateClassLogic>();
  final state = Get.find<AnnotateClassLogic>().state;

  @override
  Widget build(BuildContext context) {
    String homeworkId = state.homeworkId.value;
    return AnnotatedRegion(
      value: const SystemUiOverlayStyle(
        systemNavigationBarColor: Colors.transparent,
        systemNavigationBarDividerColor: null,
        statusBarColor: Colors.transparent,
        systemNavigationBarIconBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.light,
        statusBarBrightness: Brightness.light,
      ),
      child: OrientationBuilder(builder: (BuildContext context, Orientation orientation) {
        return Scaffold(
          backgroundColor: const Color.fromRGBO(245, 245, 245, 1),
          appBar: AppBar(
            backgroundColor: Colors.white,
            elevation: 0.3,
            title: Obx(() {
              return Text(state.name.value, style: TextStyle(fontSize: 14.sp, color: const Color(0xFF333333)));
            }),
            centerTitle: true,
            leading: IconButton(
              icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
              onPressed: () {
                Get.back();
              },
            ),
            actions: const [
              ReturnToHomepage(),
            ],
          ),
          body: Padding(
            padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 14.r),
            child: Obx(() {
              return EasyRefresh(
                  firstRefresh: false,
                  taskIndependence: true,
                  controller: logic.refreshController,
                  header: MaterialHeader(),
                  footer: TaurusFooter(),
                  onRefresh: () async {
                    return logic.getList();
                  },
                  child: state.completed.value
                      ? Utils.isPad()
                          ? GridView(
                              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 2, //横轴三个子widget
                                  mainAxisSpacing: 10.h,
                                  crossAxisSpacing: 6.w,
                                  childAspectRatio: 1.48 //宽高比为1时，子widget
                                  ),
                              children: state.classList.map((taskItem) {
                                return CompletedAnnotateItem(
                                  taskItem: taskItem,
                                  logic: logic,
                                  name: state.name.value,
                                );
                              }).toList(),
                            )
                          : ListView.builder(
                              itemCount: state.classList.length,
                              itemBuilder: (context, index) {
                                AnnotatedClass taskItem = state.classList[index];
                                return CompletedAnnotateItem(
                                  taskItem: taskItem,
                                  logic: logic,
                                  name: state.name.value,
                                );
                              })
                      : Utils.isPad()
                          ? MasonryGridView.count(
                              crossAxisCount: 2, //几列
                              mainAxisSpacing: 4.w, // 间距
                              crossAxisSpacing: 4.h, // 纵向间距？
                              itemCount: state.classList.length,
                              itemBuilder: (context, index) {
                                AnnotatedClass item = state.classList[index];
                                return AnnotateItem(
                                  homeworkId: homeworkId,
                                  item: item,
                                  font: 11.sp,
                                  name: state.name.value,
                                  logic: logic,
                                );
                              },
                            )
                          : ListView.builder(
                              itemCount: state.classList.length,
                              itemBuilder: (context, index) {
                                AnnotatedClass item = state.classList[index];
                                return AnnotateItem(
                                  homeworkId: homeworkId,
                                  item: item,
                                  font: 12.sp,
                                  name: state.name.value,
                                  logic: logic,
                                );
                              }));
            }),
          ),
        );
      }),
    );
  }

  @override
  void dispose() {
    Get.delete<AnnotateClassLogic>();
    super.dispose();
    if (state.preIndex != 3) {
      // logic.readOverController.state.tabIndex.value = state.preIndex;
    } else {
      // logic.homeController.getList();
    }
  }
}
