import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/marking_models/do_paper_bus.dart';
import 'package:making_school_asignment_app/common/mixins/event_bus_mixin.dart';
import 'package:making_school_asignment_app/common/utils/anti_shake_throttling.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';

import '../configuration_files/index.dart';

// 底部操作栏
class BottomAnnotationSwitch extends StatefulWidget {
  const BottomAnnotationSwitch({super.key});

  @override
  State<BottomAnnotationSwitch> createState() => _BottomAnnotationSwitchJobState();
}

class _BottomAnnotationSwitchJobState extends State<BottomAnnotationSwitch> with SingleTickerProviderStateMixin, EventBusMixin<BottomOperationBar> {
  late AnimationController _animationController; // 动画
  final _homeworkLogic = Get.find<HomeworkReviewLogic>();
  final _logicControl = Get.find<HomeworkReviewLogic>().annotationState;
  StreamSubscription? _opControllisten;

  @override
  void initState() {
    _animationController = AnimationController(
      vsync: this,
      value: 1, // 设置默认值
      lowerBound: 0,
      upperBound: 1,
      duration: const Duration(milliseconds: 300),
    )..addListener(toUp);

    _opControllisten = _logicControl.opControl.listen((e) {
      if (e) {
        _animationController.forward();
      } else {
        _animationController.reverse();
      }
    });

    super.initState();
  }

  @override
  void dispose() {
    _animationController
      ..removeListener(toUp)
      ..dispose();
    _opControllisten?.cancel();

    eventCancel();
    super.dispose();
  }

  void toUp() => toUpState(setState, () {}, mounted);

  @override
  Widget build(BuildContext context) {
    double iconSize = 18.sp;
    Color actionColor = Colors.white;
    Color defaultColor = const Color.fromRGBO(132, 146, 163, 1);

    return Container(
      width: double.infinity,
      height: _animationController.value * 64.h,
      color: const Color.fromRGBO(83, 83, 83, 1),
      // padding: EdgeInsets.symmetric(vertical: 1.h),
      child: Row(
        children: [
          Expanded(
            flex: 7,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Row(
                    children: [
                      // 注解笔
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: IconButton(
                            icon: Obx(() {
                              return Icon(
                                const IconData(0xe635, fontFamily: "AlibabaIcon"),
                                size: iconSize,
                                color: _logicControl.pen.value ? actionColor : defaultColor,
                              );
                            }),
                            onPressed: () => easyThrottle('homework_bottom_action_bar_annotations', () {
                              _logicControl.gestureMove.value = false;
                              _logicControl.pen.value = !_logicControl.pen.value;
                            }),
                          ),
                        ),
                      ),
                      Container(width: 0.3.w, height: double.infinity, color: Colors.white),
                      // 滑动试题
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: IconButton(
                            icon: Obx(
                              () {
                                return Icon(
                                  const IconData(0xe636, fontFamily: "AlibabaIcon"),
                                  size: iconSize,
                                  color: _logicControl.gestureMove.value ? actionColor : defaultColor,
                                );
                              },
                            ),
                            onPressed: () => easyThrottle('homework_bottom_action_bar_annotations', () {
                              _logicControl.pen.value = false;
                              _logicControl.gestureMove.value = !_logicControl.gestureMove.value;
                            }),
                          ),
                        ),
                      ),
                      Container(width: 0.3.w, height: double.infinity, color: Colors.white),
                      // 全对按钮
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            child: quickText('全 对', color: Colors.white, size: 12.sp),
                            onPressed: () => easyThrottle('homework_bottom_operation_bar_scoring_related', () => _homeworkLogic.allPairs(context)),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(width: double.infinity, color: Colors.white, height: 0.35.h),
                Expanded(
                  child: Row(
                    children: [
                      // 撤销上一步
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: IconButton(
                            onPressed: () => easyThrottle(
                              'homework_bottom_action_bar_annotations',
                              () => eventFire(model: BottomOperationBar(revokeAll: false, revokePreStep: true)),
                            ),
                            icon: Icon(const IconData(0xe638, fontFamily: "AlibabaIcon"), size: iconSize, color: defaultColor),
                          ),
                        ),
                      ),
                      Container(width: 0.3.w, height: double.infinity, color: Colors.white),
                      // 全部撤销
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: IconButton(
                            icon: Icon(const IconData(0xe637, fontFamily: "AlibabaIcon"), size: iconSize, color: defaultColor),
                            onPressed: () => easyThrottle(
                              'homework_bottom_action_bar_annotations',
                              () => eventFire(model: BottomOperationBar(revokeAll: true, revokePreStep: false)),
                            ),
                          ),
                        ),
                      ),
                      Container(width: 0.3.w, height: double.infinity, color: Colors.white),
                      // 全错按钮
                      Expanded(
                        child: SizedBox(
                          width: double.infinity,
                          height: double.infinity,
                          child: TextButton(
                            onPressed: () =>
                                easyThrottle('homework_bottom_operation_bar_scoring_related', () => _homeworkLogic.allWrongRating(context)),
                            child: quickText('全 错', color: Colors.white, size: 12.sp),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Container(width: 0.3.w, height: double.infinity, color: Colors.white),
          Expanded(
            flex: 3,
            child: Row(
              children: [
                Expanded(
                  child: SizedBox(
                    width: double.infinity,
                    height: double.infinity,
                    child: TextButton(
                      onPressed: () => easyThrottle('homework_bottom_operation_bar_scoring_related', () => _homeworkLogic.cancelAllRatings()),
                      child: quickText('取 消', size: 14.sp, color: Colors.white),
                    ),
                  ),
                ),
                Container(width: 0.3.w, height: double.infinity, color: Colors.white),
                Expanded(
                  child: SizedBox(
                    width: double.infinity,
                    height: double.infinity,
                    child: TextButton(
                      onPressed: () => easyThrottle('homework_bottom_operation_bar_scoring_related', () => _homeworkLogic.submit(context)),
                      child: quickText('提 交', size: 14.sp, color: const Color.fromRGBO(76, 199, 147, 1)),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
