
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_wisetronic/utils/utils.dart';
import 'hover_widget.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../routes.dart';

class TextLink extends StatelessWidget {
  final String title;
  final String url;
  final Color? color;
  final Color? hoverColor;
  final double? paddingHorizontal;
  final double? paddingVertical;
  final FontWeight? fontWeight;
  final bool? selected;
  final bool isLink;
  final bool replace;
  final bool clearStack;
  final bool maintainState;
  final bool rootNavigator;
  final TransitionType? transition;
  final bool closeDrawer;
  final bool isEmail;
  final bool isPhone;
  final double? fontSize;
  final bool isAddress;
  final TextOverflow overflow;
  final int maxLines;

  TextLink(this.title, this.url, {
    this.color,
    this.hoverColor,
    this.paddingHorizontal,
    this.paddingVertical,
    this.fontWeight,
    this.selected,
    bool? isLink,
    bool? replace,
    bool? clearStack,
    bool? maintainState,
    bool? rootNavigator,
    this.transition,
    bool? closeDrawer,
    bool? isEmail,
    bool? isPhone,
    double? fontSize,
    bool? isAddress,
    TextOverflow? overflow,
    int? maxLines,
  }) :
        isLink = isLink ?? false,
        replace = replace ?? false,
        clearStack = clearStack ?? false,
        maintainState = maintainState ?? true,
        rootNavigator = rootNavigator ?? false,
        closeDrawer = closeDrawer ?? false,
        isEmail = isEmail ?? false,
        isPhone = isPhone ?? false,
        fontSize = fontSize,
        isAddress = isAddress ?? false,
        overflow = overflow ?? TextOverflow.ellipsis,
        maxLines = maxLines ?? 1;

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        child: Container(
          padding: EdgeInsets.symmetric(
              vertical: paddingVertical ?? 0.0,
              horizontal: paddingHorizontal ?? 0.0
          ),
          child: HoverWidget(
            child: Text(
              title,
              style: TextStyle(
                color: color ?? Colors.blue,
                fontWeight: fontWeight ?? FontWeight.normal,
                fontSize: fontSize,
              ),
              overflow: overflow,
              maxLines: maxLines,
            ),
            hoverChild: Text(
              title,
              style: TextStyle(
                color: hoverColor ?? Colors.grey,
                fontSize: fontSize,
                fontWeight: fontWeight ?? FontWeight.normal,
              ),
              overflow: overflow,
              maxLines: maxLines,
            ),
            onHover: (event) {

            },
          ),
          decoration: BoxDecoration(
            border: (selected == true) ? Border(
                bottom: BorderSide(
                  color: color ?? Colors.blue,
                  width: 3.0,
                )
            ) : null,
          ),
        ),
        onTap: () async {
          if (selected != true) {
            if (isEmail) {
              Utils.openEmail(url);
            } else if (isPhone) {
              Utils.callPhone(url);
            } else if (isAddress) {
              await launch('https://maps.google.com/?q=${url}');
            } else if (!isLink) {
              if (closeDrawer) {
                Routes.router.pop(context);
              }
              Routes.router.navigateTo(
                context, url,
                replace: replace,
                clearStack: clearStack,
                maintainState: maintainState,
                rootNavigator: rootNavigator,
              );
            } else {
              if (await canLaunch(url)) {
                await launch(url);
              } else {
                throw 'Could not launch $url';
              }
            }
          }
        },
      ),
    );
  }
}