import 'package:flutter/material.dart';
import 'package:sport/widgets/misc.dart';

class PrimaryButton extends StatelessWidget {
  final VoidCallback callback;
  final String content;
  final double width;
  final double height;
  final double fontSize;
  final Widget child;
  final bool shadow;
  final bool bold;
  final Color buttonColor;

  PrimaryButton({
    @required this.callback,
    @required this.content,
    this.child,
    this.width = double.infinity,
    this.height = 44,
    this.fontSize = 28,
    this.shadow = true,
    this.bold = false,
    this.buttonColor,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: callback,
      borderRadius: BorderRadius.all(Radius.circular(height / 2)),
      child: Container(
        width: width,
        height: height,
        alignment: Alignment.center,
        decoration: callback == null
            ? BoxDecoration(
                color: Color(0xffdcdcdc),
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.all(Radius.circular(height / 2)),
              )
            : shadow
                ? BoxDecoration(
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.all(Radius.circular(height / 2)),
                    boxShadow: [
                      BoxShadow(
                          offset: Offset(0.0, 3),
                          blurRadius: 3,
                          spreadRadius: 0,
                          color: Color(0x82FF9100))
                    ],
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: <Color>[Color(0xffFFC400), Color(0xffFFAA00)],
                    ),
                  )
                : buttonColor != null
                    ? BoxDecoration(
                        color: buttonColor,
                        shape: BoxShape.rectangle,
                        borderRadius:
                            BorderRadius.all(Radius.circular(height / 2)),
                      )
                    : BoxDecoration(
                        shape: BoxShape.rectangle,
                        borderRadius:
                            BorderRadius.all(Radius.circular(height / 2)),
                        gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: <Color>[Color(0xffFFC400), Color(0xffFFAA00)],
                        ),
                      ),
        child: content?.isNotEmpty == true
            ? Text(
                content,
                style: TextStyle(
                    color: Colors.white,
                    fontSize:
                        height >= 40 ? callback == null ? 14.0 : 16.0 : 14.0,
                    fontWeight: bold ? FontWeight.w600 : FontWeight.normal),
                strutStyle: fixedLine,
              )
            : child,
      ),
    );
  }
}

class MainButton extends StatelessWidget {
  final VoidCallback callback;
  final String content;
  final double width;
  final double height;
  final double fontSize;
  final Widget child;
  final bool shadow;
  final bool bold;
  final Color buttonColor;
  final Color textColor;
  final String type; // cancel or confirm

  MainButton({
    @required this.callback,
    @required this.content,
    @required this.type,
    this.child,
    this.width = double.infinity,
    this.height = 44,
    this.fontSize = 28,
    this.shadow = true,
    this.bold = false,
    this.buttonColor,
    this.textColor,
  });

  @override
  Widget build(BuildContext context) {
    BoxDecoration cancelDecoration = BoxDecoration(
      color: buttonColor,
      shape: BoxShape.rectangle,
      borderRadius: BorderRadius.all(Radius.circular(height / 2)),
      boxShadow: [
        BoxShadow(
            offset: Offset(0.0, 3),
            blurRadius: 3,
            spreadRadius: 0,
            color: Color(0xffF1F1F1))
      ],
      gradient: LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: <Color>[Color(0xffF0F0F0), Color(0xffF6F6F6)],
      ),
    );

    BoxDecoration confirmDecoration = BoxDecoration(
      color: buttonColor,
      shape: BoxShape.rectangle,
      borderRadius: BorderRadius.all(Radius.circular(height / 2)),
      boxShadow: buttonColor == null
          ? [
              BoxShadow(
                  offset: Offset(0.0, 3),
                  blurRadius: 3,
                  spreadRadius: 0,
                  color: Color(0x82FF9100))
            ]
          : null,
      gradient: buttonColor == null
          ? LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: <Color>[Color(0xffFFC400), Color(0xffFFAA00)],
            )
          : null,
    );

    var map = {
      "cancel": cancelDecoration,
      "confirm": confirmDecoration,
    };

    return InkWell(
      onTap: callback,
      borderRadius: BorderRadius.all(Radius.circular(height / 2)),
      child: Container(
        width: width,
        height: height,
        alignment: Alignment.center,
        decoration: map['$type'],
        child: content?.isNotEmpty == true
            ? Text(
                content,
                style: TextStyle(
                    color: textColor != null ? textColor : Colors.white,
                    fontSize:
                        height >= 40 ? callback == null ? 14.0 : 16.0 : 14.0,
                    fontWeight: bold ? FontWeight.w600 : FontWeight.normal),
                strutStyle: fixedLine,
              )
            : child,
      ),
    );
  }
}
