import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/services/permission/permission_service.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

/// FloatingButton widget
/// [onTap] button function
/// [elevation] color elevation value
class FloatingButton extends StatefulWidget {
  FloatingButton({Key key, this.onTap, this.elevation: true}) : super(key: key);

  final VoidCallback onTap;
  final bool elevation;

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

class _FloatingButtonState extends State<FloatingButton>
    with TickerProviderStateMixin {
  double _buttonSize = 1.0;
  AnimationController _animationController;
  Animation _animation;
  PermissionService permission = new PermissionService();

  @override
  void initState() {
    _animationController = AnimationController(
        vsync: this,
        lowerBound: 0.7,
        upperBound: 1.0,
        duration: Duration(milliseconds: 120));
    _animation = CurvedAnimation(
        parent: _animationController,
        curve: Curves.easeOutQuad,
        reverseCurve: Curves.easeOutQuad);
    _animation.addListener(() {
      setState(() {
        _buttonSize = _animation.value;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return (GestureDetector(
      onTapDown: (TapDownDetails tap) {
        _animationController.reverse(from: 1.0);
      },
      onTapUp: (TapUpDetails tap) {
        _animationController.forward();
      },
      onTapCancel: () {
        _animationController.forward();
      },
      onTap: () {
        permission.vibrate(widget.onTap, context);
      },
      behavior: HitTestBehavior.opaque,
      child: Transform.scale(
          scale: _buttonSize,
          child: AnimatedContainer(
            duration: Duration(milliseconds: 150),
            margin: EdgeInsets.only(bottom: 4),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(70.0)),
                color: Theme.of(context).primaryColor,
                boxShadow: [
                  BoxShadow(
                      color: Color.fromRGBO(
                          120, 71, 80, widget.elevation ? 0.28 : 0.0),
                      spreadRadius:
                          _buttonSize < 1.0 ? -(1 - _buttonSize) * 50 : 0.0,
                      offset: Offset(0, 7.0),
                      blurRadius: 70.0)
                ]),
            child: Image.asset(
              projectViewModel.isArabic
                  ? 'assets/images/book_appointment_ar.png'
                  : 'assets/images/appointment_booking_en.png',
              // fit: BoxFit.cover,
              width: 90,
              height: 90,
            ),
          )),
    ));
  }
}
