import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/enum/review_marks_bottom_btns_enum.dart';
import 'package:marking_app/common/model/event_bus/bottom_annotation_switch_cleanall.dart';
import 'package:marking_app/pages/common/event_bus_mixin.dart';
import 'package:marking_app/utils/anti_shake_throttling.dart';
import 'package:marking_app/utils/my_text.dart';

import '../provider/do_paper_bottom_review_marks_provider.dart';

// 底部批阅动作切换区域
class DoPaperBottomReviewMarks extends StatefulHookConsumerWidget {
  const DoPaperBottomReviewMarks({super.key});

  @override
  ConsumerState<DoPaperBottomReviewMarks> createState() => _DoPaperBottomReviewMarksState();
}

class _DoPaperBottomReviewMarksState extends ConsumerState<DoPaperBottomReviewMarks> with EventBusMixin<BottomAnnotationSwitchCleanallOfMarking> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    eventCancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var btnEnum = ref.watch(doPaperBottomReviewMarksProvider);
    Color actionColor = Theme.of(context).primaryColor.withOpacity(0.9);
    return Positioned(
      left: 3.w,
      bottom: 6.h,
      child: AbsorbPointer(
        absorbing: false,
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(50.r),
            color: const Color.fromRGBO(24, 32, 32, 0.1),
          ),
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 拖动
              IconButton(
                onPressed: () => easyThrottle(
                  'REVIEW_MARKS_BOTTOM_BTNS',
                  () => ref.read(doPaperBottomReviewMarksProvider.notifier).setState(ReviewMarksBottomBtnsEnum.DRAG),
                ),
                padding: EdgeInsets.zero,
                icon: Icon(Icons.back_hand_outlined, color: btnEnum == ReviewMarksBottomBtnsEnum.DRAG ? actionColor : Colors.white, size: 34.r),
              ),
              SizedBox(width: 4.w),
              // 批阅笔迹
              IconButton(
                onPressed: () => easyThrottle(
                  'REVIEW_MARKS_BOTTOM_BTNS',
                  () => ref.read(doPaperBottomReviewMarksProvider.notifier).setState(ReviewMarksBottomBtnsEnum.HANDWRITING),
                ),
                padding: EdgeInsets.zero,
                icon: Icon(Icons.create_outlined, color: btnEnum == ReviewMarksBottomBtnsEnum.HANDWRITING ? actionColor : Colors.white, size: 34.r),
              ),
              SizedBox(width: 4.w),
              // 批阅痕迹 ==> 返回上一级
              IconButton(
                onPressed: () => easyThrottle(
                  'REVIEW_MARKS_BOTTOM_BTNS',
                  duration: const Duration(milliseconds: 100),
                  () => eventFire(model: BottomAnnotationSwitchCleanallOfMarking(previousStep: true)),
                ),
                padding: EdgeInsets.zero,
                icon: Icon(Icons.reply_outlined,
                    color: btnEnum == ReviewMarksBottomBtnsEnum.RETURN_PREVIOUS_LEVEL ? actionColor : Colors.white, size: 34.r),
              ),
              SizedBox(width: 4.w),
              // 批阅痕迹 ==> 清空
              IconButton(
                onPressed: () => easyThrottle(
                  'REVIEW_MARKS_BOTTOM_BTNS',
                  duration: const Duration(milliseconds: 100),
                  () async {
                    var resFlag = await showDialog<bool>(
                      context: context,
                      builder: (BuildContext context) {
                        return CupertinoAlertDialog(
                          title: quickText('撤销批阅痕迹', size: 14.sp, color: Color.fromARGB(255, 53, 52, 52)),
                          content: SingleChildScrollView(
                            padding: EdgeInsets.only(top: 4.h),
                            child: RichText(text: TextSpan(text: '请确认需要撤销所有批阅痕迹？', style: TextStyle(color: Color.fromARGB(255, 58, 58, 58)))),
                          ),
                          actions: <Widget>[
                            CupertinoDialogAction(
                              child: Text("取消", style: TextStyle(color: Color.fromARGB(255, 58, 58, 58))),
                              onPressed: () => Navigator.of(context).pop(false),
                            ),
                            CupertinoDialogAction(
                              child: Text("确定"),
                              onPressed: () => Navigator.of(context).pop(true),
                            ),
                          ],
                        );
                      },
                    );
                    if (resFlag == true) eventFire(model: BottomAnnotationSwitchCleanallOfMarking(cleanAll: true));
                  },
                ),
                padding: EdgeInsets.zero,
                icon: Icon(Icons.reply_all_outlined, color: btnEnum == ReviewMarksBottomBtnsEnum.CLEAR_ALL ? actionColor : Colors.white, size: 34.r),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
