import 'dart:collection';

import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

///FIS的页面过度动效
class FTransitions {
  FTransitions._();

  static final _cache = HashMap<String, CustomTransition>();

  static CustomTransition get fadeThrough => _findInCache(() => _FadeThrough());

  static CustomTransition get sharedAxisHorizontal => _findInCache(
        () => _SharedAxis(SharedAxisTransitionType.horizontal),
        tag: "x",
      );

  static CustomTransition get sharedAxisVertical => _findInCache(
        () => _SharedAxis(SharedAxisTransitionType.vertical),
        tag: "y",
      );

  static CustomTransition get sharedAxisScaled => _findInCache(
        () => _SharedAxis(SharedAxisTransitionType.scaled),
        tag: "z",
      );

  static CustomTransition _findInCache<T extends CustomTransition>(
    T Function() builder, {
    String? tag,
  }) {
    final key = tag != null ? '${T.toString()}_$tag' : T.toString();
    if (!_cache.containsKey(key)) {
      _cache[key] = builder();
    }
    return _cache[key]!;
  }
}

///公用过度动效轴
class _SharedAxis extends CustomTransition {
  _SharedAxis(this.transitionType);

  final SharedAxisTransitionType transitionType;

  @override
  Widget buildTransition(
    BuildContext context,
    Curve? curve,
    Alignment? alignment,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return SharedAxisTransition(
      animation: animation,
      secondaryAnimation: secondaryAnimation,
      transitionType: transitionType,
      child: child,
    );
  }
}

///闪现过度动效
class _FadeThrough extends CustomTransition {
  @override
  Widget buildTransition(
    BuildContext context,
    Curve? curve,
    Alignment? alignment,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return FadeThroughTransition(
      animation: animation,
      secondaryAnimation: secondaryAnimation,
      child: child,
    );
  }
}
