import 'package:flutter/material.dart';
import 'package:vnoteapp/components/floating_window/click_notification.dart';
import 'package:vnoteapp/components/floating_window/floating_button.dart';
import 'package:vnoteapp/components/floating_window/floating_item.dart';
import 'package:vnoteapp/components/floating_window/floating_items.dart';
import 'package:vnoteapp/components/floating_window/floating_window_model.dart';
import 'package:vnoteapp/components/floating_window/floating_window_shared_data_widget.dart';

/// [FloatingWindow] 悬浮窗
class FloatingWindow extends StatefulWidget {
  const FloatingWindow({super.key});

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

class _FloatingWindowState extends State<FloatingWindow> {
  List<Map<String, String>> ls = [
    {'title': "测试一下", "imageUrl": "assets/images/no_data.png"},
  ];

  /// 悬浮窗共享数据
  FloatingWindowModel? windowModel;

  /// [isEntering] 列表项是否拥有进场动画
  bool isEntering = true;
  @override
  void initState() {
    super.initState();
    windowModel = FloatingWindowModel(dataList: ls, isLeft: true);
    isEntering = true;
  }

  @override
  Widget build(BuildContext context) {
    return FloatingWindowSharedDataWidget(
      data: windowModel!,
      child: windowModel!.isEmpty
          ? Container()
          : Stack(
              fit: StackFit.expand,
              children: [
                /// 列表项遮盖层，增加淡化切换动画
                AnimatedSwitcher(
                  duration: const Duration(milliseconds: 100),
                  child: windowModel!.isButton
                      ? Container()
                      : GestureDetector(
                          onTap: () {
                            FloatingItem.reverse();
                            Future.delayed(const Duration(milliseconds: 110),
                                () {
                              setState(() {
                                windowModel!.isButton = true;
                              });
                            });
                          },
                          child: Container(
                            decoration: const BoxDecoration(
                                color: Color.fromRGBO(0xEF, 0xEF, 0xEF, 0.9)),
                          ),
                        ),
                ),
                NotificationListener<ClickNotification>(
                  onNotification: (notification) {
                    /// 列表项关闭事件
                    if (notification.deletedIndex != -1) {
                      windowModel?.deleteIndex = notification.deletedIndex;
                      setState(() {
                        FloatingItem.resetList();
                        windowModel?.dataList
                            .removeAt(notification.deletedIndex);
                        isEntering = false;
                      });
                    }

                    /// 列表点击事件
                    if (notification.clickIndex != -1) {
                      print(notification.clickIndex);
                    }

                    /// 悬浮按钮点击Widget改变事件
                    if (notification.changeWidget) {
                      setState(() {
                        /// 释放列表进出场动画资源
                        FloatingItem.resetList();
                        windowModel?.isButton = false;
                        isEntering = true;
                      });
                    }

                    return false;
                  },
                  child: windowModel!.isButton
                      ? const FloatingButton()
                      : FloatingItems(
                          isEntering: isEntering,
                        ),
                )
              ],
            ),
    );
  }
}
