import 'dart:ui';

import 'package:aitrainer_app/model/cache.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:google_fonts/google_fonts.dart';

// ignore: must_be_immutable
class ImageButton extends StatelessWidget {
  final String text;
  TextStyle style = TextStyle(fontSize: 14);
  final String image;
  final double top;
  final double left;
  double height;
  double width;
  bool isShape;
  final Bloc bloc;
  final Alignment textAlignment;
  final VoidCallback onTap;
  bool isLocked;
  bool isMarked;
  int buttonIndex;

  ImageButton(
      {this.text,
      this.style,
      this.image,
      this.top,
      this.left,
      this.height,
      this.width,
      this.bloc,
      this.isShape,
      this.textAlignment,
      this.onTap,
      this.buttonIndex,
      this.isMarked,
      @required this.isLocked}) {
    width = width ?? 180;
    height = height ?? 180;
    isMarked = isMarked ?? false;
    style = style ??
        GoogleFonts.archivoBlack(
          fontSize: 14,
        );
  }

  @override
  Widget build(BuildContext context) {
    double top =
        height - (style.fontSize - 5) * text.length - 2 * left < 0 ? height - 2 * style.fontSize - 22 : height - style.fontSize - 17;
    //print("Top: " + top.toStringAsFixed(0) + " length: " + ((style.fontSize - 5) * text.length).toString());
    return Stack(
        //alignment: textAlignment,
        fit: StackFit.passthrough,
        overflow: Overflow.clip,
        children: [
          FlatButton(
            child: image == null
                ? _getButtonImage("asset/image/WT_menu_dark.png")
                : isMarked
                    ? Stack(
                        children: [
                          _getButtonImage(image),
                          Container(
                            width: width,
                            height: height,
                            child: BackdropFilter(
                              filter: ImageFilter.blur(sigmaX: 1, sigmaY: 1),
                              child: Container(
                                color: Colors.black.withOpacity(0.3),
                              ),
                            ),
                          )
                        ],
                      )
                    : _getButtonImage(image),
            padding: EdgeInsets.only(left: 0.0, bottom: 0),
            shape: getShape(isShape),
            onPressed: onTap ?? onTap,
          ),
          Positioned(
            top: top,
            left: left,
            child: Container(
              height: width - 2 * left,
              width: width - 2 * left,
              child: InkWell(
                onTap: onTap ?? onTap,
                child: Text(
                  text,
                  maxLines: 2,
                  style: style,
                ),
              ),
              color: Colors.transparent,
            ),
          ),
          Stack(alignment: Alignment.topLeft, children: [
            Positioned(
              top: height / 2 - 30,
              left: width / 2 - 30,
              child: this.isLocked
                  ? GestureDetector(
                      child: Image.asset(
                        'asset/image/lock.png',
                        height: 60,
                        width: 60,
                      ),
                      onTap: onTap ?? onTap,
                    )
                  : isMarked
                      ? GestureDetector(
                          child: Image.asset(
                            'asset/image/haken.png',
                            height: 70,
                            width: 70,
                          ),
                          onTap: onTap ?? onTap,
                        )
                      : Container(),
            )
          ]),
        ]
        //)
        // )
        );
  }

  dynamic getShape(bool isShape) {
    dynamic returnCode = (isShape == true)
        ? RoundedRectangleBorder(
            side: BorderSide(width: 4, color: Colors.orangeAccent),
          )
        : null;
    return returnCode;
  }

  dynamic _getButtonImage(String imageName) {
    dynamic image;
    try {
      image = Image.asset(
        imageName,
        fit: BoxFit.fitWidth,
        alignment: Alignment.center,
        errorBuilder: (context, error, stackTrace) {
          String url = Cache.mediaUrl + '/' + imageName; //.substring(11);
          Widget image = FadeInImage.assetNetwork(placeholder: 'asset/image/dots.gif', image: url, height: this.height);
          return image;
        },
      );
    } on Exception catch (_) {
      String url = Cache.mediaUrl + '/images/' + imageName;
      image = FadeInImage.assetNetwork(
        placeholder: 'asset/image/dots.gif',
        image: url,
        height: 50,
      );
    }

    return image;
  }
}