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

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

  ImageButton({
    this.text,
    this.style,
    this.image,
    this.top,
    this.left,
    this.height,
    this.width,
    this.bloc,
    this.isShape,
    this.textAlignment,
    this.onTap,
    @required this.isLocked
  }) {
    width = width ?? 180;
    style = style ?? TextStyle(fontSize: 14, fontFamily: "Roboto Mono");
  }

  @override
  Widget build(BuildContext context) {
    double top = width - (style.fontSize - 5) * text.length - 2 * left < 0  ? width - 2 * style.fontSize - 10 : width - style.fontSize - 10;
    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") :
              _getButtonImage(image),
            padding: EdgeInsets.only(left: 0.0, bottom: 0),
            shape: getShape(isShape),
            onPressed: onTap ?? onTap,
          ),
          Stack(
            alignment: Alignment.topLeft,
            children: [
            Positioned(
              top: 50,
              left: 50,
              child: this.isLocked?
                Image.asset(
                  'asset/image/lock.png',
                  height: 60,
                  width: 60,
                )
              : Container(),
            )]
           ),
          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,
            ),
          ),
        ]
        //)
        // )
        );
  }

  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 + 'images/' + imageName.substring(11);
          Widget image = FadeInImage.assetNetwork(
            placeholder: 'asset/image/dots.gif',
            image: url,
            height: 180,
          );
          return image;
        },
      );
    } on Exception catch (_) {
      String url = Cache.mediaUrl + '/images/' + imageName;
      image = FadeInImage.assetNetwork(
        placeholder: 'asset/image/dots.gif',
        image: url,
        height: 180,
      );
    }

    return image;
  }
}