import 'package:achievement_view/achievement_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:marking_app/common/model/event_bus/bottom_annotation_switch_cleanall.dart';
import 'package:marking_app/common/model/event_bus/jobs/job_do_papers_switch_operation_bus.dart';
import 'package:marking_app/common/model/marking/annotation_graffiti_switch.dart';
import 'package:marking_app/common/model/marking/do_marking_keyboard_model.dart';
import 'package:marking_app/pages/common/event_bus_mixin.dart';
import 'package:marking_app/pages/homework_correction/eventBus/job_do_papers_switch_operation_sub_bus.dart';
import 'package:marking_app/pages/homework_correction/eventBus/job_notes_view_bus.dart';
import 'package:marking_app/provider/annotation_graffiti_switch_provider.dart';
import 'package:marking_app/provider/do_marking_provider.dart';
import 'package:marking_app/utils/index.dart';

// 作业底部批注开关
class BottomAnnotationSwitchJob extends StatefulHookConsumerWidget {
  final double? maxWidth;
  final bool homework;
  const BottomAnnotationSwitchJob({this.maxWidth, this.homework = false, Key? key}) : super(key: key);

  @override
  _BottomAnnotationSwitchJobState createState() => _BottomAnnotationSwitchJobState();
}

class _BottomAnnotationSwitchJobState extends ConsumerState<BottomAnnotationSwitchJob>
    with SingleTickerProviderStateMixin, EventBusMixin {
  JobNotesViewBus? jobNotesModel;
  late RemoveListener _annotationsListener; // 批注关闭监听
  late AnimationController _animationController; // 动画
  late AnnotationGraffitiSwitch graffitiSwitch;
  late DoMarkingKeyboardModel _preferenceModel;
  late double upperBound;
  late double lowerBound;
  Color? bgc;
  bool isIos = false;

  @override
  void initState() {
    var graffitiHander = ref.read(annotationGraffitiSwitchProvider.notifier);
    if (widget.homework) {
      setTimeOut(500, () {
        graffitiHander.setSwitch(true);
        if (!graffitiHander.state.openBrush) graffitiHander.setSwitchBrush();
      }); // 默认打开可以书写
    }

    _preferenceModel = ref.read(markingKeyboardProvider); // 偏好设置

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

    _annotationsListener = graffitiHander.addListener((state) {
      graffitiSwitch = state;
      if (state.annotationSwitch) {
        _animationController.forward().then((_) {
          eventFire(model: JobDoPapersSwitchOperationSubBus(44.h, true));
        });
      } else {
        _animationController.reverse().then((_) {
          eventFire(model: JobDoPapersSwitchOperationSubBus(44.h, false));
        });
      }
    });

    //
    eventOn(callback: (eventVal) {
      switch (eventVal.runtimeType) {
        case JobDoPapersSwitchOperationBus: // 底部功能栏操作开关
          bool flag = (eventVal as JobDoPapersSwitchOperationBus).flag;
          graffitiHander.setSwitch(flag);
          break;
        case JobNotesViewBus: // 笔记回显模式
          var eventModel = eventVal as JobNotesViewBus;
          if (eventModel.questionNo == jobNotesModel?.questionNo) {
            jobNotesModel = null; // 清空笔记会先
          } else {
            jobNotesModel = eventModel; // 赋值笔记回显
          }
          break;
        default:
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _annotationsListener();
    _animationController
      ..removeListener(toUp)
      ..dispose();
    eventCancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double iconSize = 24.sp;
    Color actionColor = Colors.white;
    Color defaultColor = Color.fromRGBO(132, 146, 163, 1);
    AnnotationGraffitiSwitch _graffitiSwitch = ref.watch(annotationGraffitiSwitchProvider);
    return Container(
      width: double.infinity,
      height: _animationController.value * 44.h,
      color: Color.fromRGBO(83, 83, 83, 1),
      padding: EdgeInsets.symmetric(vertical: 1.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Expanded(
            flex: 8,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                InkWell(
                  onTap: () {
                    if (jobNotesModel != null) {
                      return AchievementView(
                        elevation: 5,
                        duration: Duration(seconds: 1),
                        title: "笔记回显提示",
                        subTitle: "当前正处于笔记回显",
                        color: Theme.of(context).primaryColor,
                      ).show(context);
                    }
                    easyThrottle(
                        'setSwitchBrush', () => ref.read(annotationGraffitiSwitchProvider.notifier).setSwitchBrush());
                  },
                  child: Icon(
                    const IconData(0xe635, fontFamily: "AlibabaIcon"),
                    size: iconSize,
                    color: _graffitiSwitch.openBrush ? actionColor : defaultColor,
                  ),
                ),
                InkWell(
                  onTap: () {
                    if (jobNotesModel != null) {
                      return AchievementView(
                        elevation: 5,
                        duration: Duration(seconds: 1),
                        title: "笔记回显提示",
                        subTitle: "当前正处于笔记回显",
                        color: Theme.of(context).primaryColor,
                      ).show(context);
                    }
                    eventFire(model: BottomAnnotationSwitchCleanall(previousStep: true));
                  },
                  child: Icon(const IconData(0xe638, fontFamily: "AlibabaIcon"), size: iconSize, color: defaultColor),
                ),
                InkWell(
                  onTap: () {
                    if (jobNotesModel != null) {
                      return AchievementView(
                        elevation: 5,
                        duration: Duration(seconds: 1),
                        title: "笔记回显提示",
                        subTitle: "当前正处于笔记回显",
                        color: Theme.of(context).primaryColor,
                      ).show(context);
                    }
                    easyThrottle(
                        'setSwitchMagnifier', () => ref.read(annotationGraffitiSwitchProvider.notifier).setMagnifier());
                  },
                  // IconData(0xe62f, fontFamily: "AlibabaIcon")
                  child: Icon(const IconData(0xe636, fontFamily: "AlibabaIcon"),
                      size: iconSize, color: _graffitiSwitch.magnifier ? actionColor : defaultColor),
                ),
                InkWell(
                  onTap: () {
                    if (jobNotesModel != null) {
                      return AchievementView(
                        elevation: 5,
                        duration: Duration(seconds: 1),
                        title: "笔记回显提示",
                        subTitle: "当前正处于笔记回显",
                        color: Theme.of(context).primaryColor,
                      ).show(context);
                    }
                    eventFire(model: BottomAnnotationSwitchCleanall(cleanAll: true));
                  },
                  child: Icon(
                    const IconData(0xe637, fontFamily: "AlibabaIcon"),
                    size: iconSize,
                    color: defaultColor,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              child: InkWell(
                onTap: () {
                  if (jobNotesModel != null) {
                    return AchievementView(
                      elevation: 5,
                      duration: Duration(seconds: 1),
                      title: "笔记回显提示",
                      subTitle: "当前正处于笔记回显",
                      color: Theme.of(context).primaryColor,
                    ).show(context);
                  }
                  eventFire(model: JobDoPapersSwitchOperationBus(false));
                },
                child: Icon(
                  const IconData(0xe639, fontFamily: "AlibabaIcon"),
                  size: iconSize,
                  color: _graffitiSwitch.trajectoryDisplay ? actionColor : defaultColor,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

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