import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:app_hit_a_wod/src/styles.dart';

class PopupLayout extends ModalRoute {
  double top;
  double bottom;
  double left;
  double right;
  Color bgColor;
  String animationType; // position or center
  String position; // top, bottom or center
  double height;
  final Widget child;

  @override
  Duration get transitionDuration => Duration(milliseconds: 300);

  @override
  bool get opaque => false;

  @override
  bool get barrierDismissible => false;

  @override
  Color get barrierColor => bgColor == null ? Colors.black.withOpacity(0.5) : bgColor;

  @override
  String get barrierLabel => null;

  @override
  bool get maintainState => false;

  PopupLayout(
      {Key key,
      this.bgColor,
      @required this.child,
      this.top,
      this.bottom,
      this.left,
      this.right,
      this.animationType,
      this.position,
      this.height});

  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    if (top == null) this.top = 10;
    if (bottom == null) this.bottom = 20;
    if (left == null) this.left = 20;
    if (right == null) this.right = 20;

    return GestureDetector(
      onTap: () {
        // call this method here to hide soft keyboard
        SystemChannels.textInput.invokeMethod('TextInput.hide');
        /* try {
          Navigator.pop(context); //close the popup
          print('a');
        } catch (e) {} */
      },
      child: Material(
        // This makes sure that text and other content follows the material style
        type: MaterialType.transparency,
        //type: MaterialType.canvas,
        // make sure that the overlay content is not cut off
        child: GestureDetector(
          /* onTap: (){}, */
          child: SafeArea(
            bottom: true,
            child: _buildOverlayContent(context)
          ),
        ),
      ),
    );
  }

  Widget _buildOverlayContent(BuildContext context) {
    return Container(
      color: Colors.transparent,
      margin: getMargin(),
      child: neuContainer(child)
    );
  }

  getMargin() {
    if(this.animationType=='position-bottom' || this.animationType=='position-top') { return EdgeInsets.all(0);}
    else return EdgeInsets.only(
      top: this.top,
      bottom: this.bottom,
      left: this.left,
      right: this.right,
    );
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    double h = MediaQuery.of(context).size.height;
    if(this.animationType=='position-bottom' || this.animationType=='position-top') {
      Animation<RelativeRect> rectAnimation;
      if (this.animationType=='position-top') {
        rectAnimation = new RelativeRectTween(
          begin: new RelativeRect.fromLTRB(0.0, 0.0, 0.0, h),
          end: new RelativeRect.fromLTRB(this.left, this.top, this.right, this.bottom),
        ).animate(controller);
      } else if (this.animationType=='position-bottom')  {
        rectAnimation = new RelativeRectTween(
          begin: new RelativeRect.fromLTRB(0.0, h, 0.0, 0.0),
          end: new RelativeRect.fromLTRB(this.left, this.top, this.right, this.bottom),
        ).animate(controller);
      }
      return Container(
        child: Stack(
          children: <Widget>[
            PositionedTransition(
              rect: rectAnimation,
              child: new Material(
                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.only(topLeft: new Radius.circular(15.0), topRight: new Radius.circular(15.0))),
                color: Colors.transparent,
                elevation: 150.0,
                child: child,
              )
            ),
          ],
        ),
      );
    } else {
      return FadeTransition(
        opacity: animation,
        child: ScaleTransition(
          scale: animation,
          child: child,
        ),
      );
    }

  }
}
