import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:router/observer/getx_router_observer.dart';
import 'mixin_state_lifecycle.dart';

/*
 * StateLessWidget 的生命周期实现基于 Getx 框架扩展实现
 */
mixin StateLessLifecycleMixin on StatelessWidget implements RouteAware, WidgetsBindingObserver, IStateLifecycle {
  bool _lifecyclePerformed = false;

  void _ensureLifecycle(BuildContext context) {
    if (!_lifecyclePerformed) {
      final stateLessLifecycle = Get.put(StateLessLifecycle(), tag: key?.toString());
      stateLessLifecycle.setLifecycleCallback(MyLifecycleCallback(
        onReadyAction: () => _onReady(context),
        onCloseAction: _onClose,
      ));
      _lifecyclePerformed = true;
    }
  }

  void _onReady(BuildContext context) {
    final route = ModalRoute.of(context);
    if (route != null && route is PageRoute) {
      routeObserver.subscribe(this, route);  // 订阅至RouteObserver
    }
    WidgetsBinding.instance.addObserver(this); // 添加至WidgetsBinding观察者
  }

  void _onClose() {
    routeObserver.unsubscribe(this); // 取消订阅RouteObserver
    WidgetsBinding.instance.removeObserver(this); // 移除WidgetsBinding观察者
  }

  @protected
  Widget buildWidget(BuildContext context);

  @override
  Widget build(BuildContext context) {
    _ensureLifecycle(context);
    return buildWidget(context);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      onResume();
    } else if (state == AppLifecycleState.paused) {
      onPause();
    }
  }

  @override
  void didPop() {
    onStop();
  }

  @override
  void didPopNext() {
    onResume();
  }

  @override
  void didPush() {
    onStart();
  }

  @override
  void didPushNext() {
    onPause();
  }

  //  =========================== 默认空实现 ===========================

  @override
  void didChangeAccessibilityFeatures() {}

  @override
  void didChangeLocales(List<Locale>? locales) {}

  @override
  void didChangeMetrics() {}

  @override
  void didChangePlatformBrightness() {}

  @override
  void didChangeTextScaleFactor() {}

  @override
  void didHaveMemoryPressure() {}

  @override
  Future<bool> didPopRoute() {
    return Future<bool>.value(false);
  }

  @override
  Future<bool> didPushRoute(String route) {
    return Future<bool>.value(false);
  }

  @override
  Future<bool> didPushRouteInformation(RouteInformation routeInformation) {
    return didPushRoute(routeInformation.location!);
  }

  @override
  Future<AppExitResponse> didRequestAppExit() async {
    return AppExitResponse.exit;
  }

  @override
  void onStart() {}

  @override
  void onStop() {}

  @override
  void onResume() {}

  @override
  void onPause() {}
}

/*
 * 继承自 DisposableInterface 实现 Getx 的生命周期
 */
class StateLessLifecycle extends DisposableInterface {
  @override
  void onReady() {
    super.onReady();
    callback?.onReadyCallback();
  }

  @override
  void onClose() {
    callback?.onCloseCallback();
    super.onClose();
  }

  ILifecycleCallback? callback;

  void setLifecycleCallback(ILifecycleCallback callback) {
    this.callback = callback;
  }
}

// 自定义生命周期回调接口
abstract class ILifecycleCallback {
  void onReadyCallback();

  void onCloseCallback();
}

class MyLifecycleCallback implements ILifecycleCallback {
  final VoidCallback onReadyAction;
  final VoidCallback onCloseAction;

  MyLifecycleCallback({required this.onReadyAction, required this.onCloseAction});

  @override
  void onReadyCallback() {
    onReadyAction();
  }

  @override
  void onCloseCallback() {
    onCloseAction();
  }
}
