import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/utils/window__change_helpler.dart';
import 'package:flyinsono/pages/index.dart';
import 'package:flyinsono/views/desktop/notification_list/index.dart';
import 'package:get/get.dart';

class TitleBarButtons extends FStatefulWidget implements FInteractiveContainer {
  final VoidCallback onMinimize;
  final VoidCallback onWindowClose;
  final VoidCallback? onMaximize;
  final Color iconColor;
  final Color backgroundColor;
  @override
  final String pageName;
  final bool isLabHeader;
  final bool isShowNotification;
  late final Color? hoverColor;
  late final Color? hoverIconColor;

  TitleBarButtons(this.onMinimize, this.onWindowClose, this.onMaximize,
      this.iconColor, this.backgroundColor,
      {Color? hoverColor,
      Color? hoverIconColor,
      this.isLabHeader = false,
      this.isShowNotification = true,
      this.pageName = 'TitleBarButtons'}) {
    if (hoverColor == null) {
      this.hoverColor = FTheme.ins.colorScheme.secondary;
    } else {
      this.hoverColor = hoverColor;
    }
    if (hoverIconColor == null) {
      this.hoverIconColor = iconColor;
    } else {
      this.hoverIconColor = hoverIconColor;
    }
  }

  @override
  FState<FStatefulWidget> createState() {
    return _TitleBarButtonsState();
  }
}

class _TitleBarButtonsState extends FState<TitleBarButtons> {
  bool _isMaximized = true;

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 40,
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          if (widget.onMaximize != null && widget.isShowNotification)
            NotificationListButton4Win(),
          HoverIconButton(
            widget.iconColor,
            widget.onMinimize,
            FIcon(Icons.remove),
            widget.backgroundColor,
            hoverColor: widget.hoverColor,
            businessParent: this.widget,
            hoverIconColor: widget.hoverIconColor,
            name: 'minimize',
          ),
          if (widget.onMaximize != null) ...[
            HoverIconButton(
              widget.iconColor,
              () {
                widget.onMaximize!.call();
                setState(() {
                  _isMaximized = !_isMaximized;
                });
              },
              widget.isLabHeader
                  ? _isMaximized
                      ? FIcon(
                          Icons.filter_none,
                          size: 17,
                        )
                      : FIcon(Icons.crop_square)
                  : FObx(() {
                      var controller = Get.find<HomeController>();
                      if (!controller.state.isMaximized) {
                        return FIcon(Icons.crop_square);
                      } else {
                        return FIcon(Icons.filter_none);
                      }
                    }),
              widget.backgroundColor,
              businessParent: this.widget,
              hoverColor: widget.hoverColor,
              hoverIconColor: widget.hoverIconColor,
              name: 'maximize',
            ),
          ],
          HoverIconButton(
            widget.iconColor,
            widget.onWindowClose,
            FIcon(Icons.close),
            widget.backgroundColor,
            hoverColor: Colors.red,
            hoverIconColor: widget.hoverIconColor,
            businessParent: this.widget,
            name: "close",
          ),
        ],
      ),
    );
  }
}

class HoverIconButton extends FStatefulWidget implements FWidget {
  final Color iconColor;
  final String name;
  final Color backgroundColor;
  final VoidCallback onPressed;
  final FWidget icon;

  ///父级节点
  final FInteractiveContainer businessParent;
  late final Color? hoverColor;
  late final Color? hoverIconColor;
  late final double? width;
  late final double? height;

  HoverIconButton(
    this.iconColor,
    this.onPressed,
    this.icon,
    this.backgroundColor, {
    Color? hoverColor,
    Color? hoverIconColor,
    double? height,
    double? width,
    required this.businessParent,
    required this.name,
  }) {
    if (hoverColor == null) {
      this.hoverColor = backgroundColor;
    } else {
      this.hoverColor = hoverColor;
    }
    this.height = height;
    this.width = width;
    if (hoverIconColor == null) {
      this.hoverIconColor = iconColor;
    } else {
      this.hoverIconColor = hoverIconColor;
    }
  }
  @override
  FState<FStatefulWidget> createState() {
    return HoverIconButtonState();
  }
}

class HoverIconButtonState extends FState<HoverIconButton> {
  Color _backgroundColor = Colors.transparent;
  late Color _iconColor;
  late final Color hoverColor;
  late final Color hoverIconColor;
  @override
  void initState() {
    _iconColor = widget.iconColor;
    hoverColor = widget.hoverColor ?? widget.backgroundColor;
    hoverIconColor = widget.hoverIconColor ?? _iconColor;
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FMouseRegion(
      onEnter: (event) {
        setState(() {
          _iconColor = hoverIconColor;
          _backgroundColor = hoverColor;
        });
      },
      onExit: (event) {
        setState(() {
          _iconColor = widget.iconColor;
          _backgroundColor = widget.backgroundColor;
        });
      },
      child: FContainer(
        width: widget.width,
        height: widget.height,
        color: _backgroundColor,
        child: FIconButton(
          name: widget.name,
          businessParent: widget.businessParent,
          color: _iconColor,
          onPressed: () => widget.onPressed.call(),
          icon: widget.icon,
        ),
      ),
    );
  }
}
