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;
  double? top;
  final double left;
  double? height;
  double? width;
  bool? isShape;
  final Bloc? bloc;
  final Alignment textAlignment;
  final VoidCallback? onTap;
  final bool? isLocked;
  bool? isMarked;
  int? buttonIndex;
  Color? textColor;

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

  @override
  Widget build(BuildContext context) {
    if (top == null) {
      top = height! - (style!.fontSize! - 5) * text.length - 2 * left < 0
          ? height! - 2 * style!.fontSize! - 22
          : height! - style!.fontSize! - 37;
    }
    final double width = MediaQuery.of(context).size.width;

    return Stack(alignment: AlignmentDirectional.bottomStart, children: [
      TextButton(
        style: TextButton.styleFrom(
          padding: EdgeInsets.only(left: 0.0, bottom: 0),
          shape: getShape(isShape!),
        ),
        child: image == null
            ? _getButtonImage("asset/image/WT_menu_dark.jpg")
            : 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!),
        onPressed: onTap ?? onTap,
      ),
      Container(
          padding: EdgeInsets.all(4),
          child: InkWell(
            onTap: onTap ?? onTap,
            child: Text(text,
                maxLines: 2,
                style: GoogleFonts.archivoBlack(
                  fontSize: 16,
                  color: textColor,
                  shadows: <Shadow>[
                    Shadow(
                      offset: Offset(2.0, 2.0),
                      blurRadius: 6.0,
                      color: Colors.black54,
                    ),
                    Shadow(
                      offset: Offset(-3.0, 3.0),
                      blurRadius: 12.0,
                      color: Colors.black54,
                    ),
                  ],
                )),
          )),
      isMarked == null || Cache().hasPurchased
          ? Offstage()
          : Stack(alignment: Alignment.center, children: [
              Positioned(
                  top: 10,
                  left: (width / 2 - 30) / 2 - 25,
                  child: !isLocked!
                      ? Offstage()
                      : GestureDetector(
                          child: Image.asset(
                            'asset/image/lock.png',
                            height: 50,
                            width: 50,
                          ),
                          onTap: onTap ?? onTap,
                        ))
            ]),
      isLocked == null
          ? Offstage()
          : Stack(alignment: Alignment.topCenter, children: [
              Positioned(
                top: 10,
                left: (width / 2 - 30) / 2 - 75,
                child: isMarked!
                    ? GestureDetector(
                        child: Image.asset(
                          'asset/image/haken.png',
                          height: 70,
                          width: 70,
                        ),
                        onTap: onTap ?? onTap,
                      )
                    : Offstage(),
              )
            ]),
    ]
        //)
        // )
        );
  }

  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;
  }
}
