
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../../generated/l10n.dart';
import '../../routes.dart';
import 'navigationbar.dart';

class BreadCrumbs extends StatelessWidget {
  final List<BreadCrumb>? breadCrumbs;
  final bool hasBack;
  final OnBackPress? onBackPress;
  const BreadCrumbs(this.hasBack, {this.breadCrumbs, Key? key, this.onBackPress}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = [];
    if (this.hasBack) {
      widgets.add(Container(
        child: MouseRegion(
          cursor: SystemMouseCursors.click,
          child: GestureDetector(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Icon(
                  Icons.arrow_back_ios,
                  size: 16.0,
                  color: Colors.black38,
                ),
                Container(
                  padding: EdgeInsets.only(left: 0.0, right: 4.0),
                  child: Text(
                    S.of(context).back,
                    style: TextStyle(
                      color: Colors.black38,
                      fontSize: 14.0,
                    ),
                  ),
                ),
              ],
            ),
            onTap: onBackPress != null ? onBackPress : () {
              Navigator.of(context).maybePop();
            },
          ),
        ),
      ));
    }
    if (breadCrumbs != null) {
      for (int i = 0; i < breadCrumbs!.length; i++) {
        BreadCrumb breadCrumb = breadCrumbs![i];
        if (breadCrumb.text == null && breadCrumb.item != null) {
          if (breadCrumb.onTap == null) {
            widgets.add(breadCrumb.item!);
          } else {
            widgets.add(MouseRegion(
              cursor: SystemMouseCursors.click,
              child: GestureDetector(
                child: breadCrumb.item,
                onTap: breadCrumb.onTap,
              ),
            ));
          }
        } else {
          if (breadCrumb.route != null) {
            widgets.add(Container(
              child: MouseRegion(
                cursor: SystemMouseCursors.click,
                child: GestureDetector(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.only(top: 6.0, left: 4.0, right: 4.0),
                        child: Icon(
                          Icons.circle,
                          size: 6.0,
                          color: Colors.black38,
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(left: 4.0, right: 4.0),
                        child: breadCrumb.icon != null ?
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Icon(
                              breadCrumb.icon,
                              size: 16.0,
                              color: Colors.blueAccent,
                            ),
                            Text(
                              breadCrumb.text,
                              style: TextStyle(
                                color: Colors.blueAccent,
                                fontSize: 14.0,
                              ),
                            )
                          ],
                        ) :
                        Text(
                          breadCrumb.text,
                          style: TextStyle(
                            color: Colors.blueAccent,
                            fontSize: 14.0,
                          ),
                        ),
                      ),
                    ],
                  ),
                  onTap: () {
                    Routes.router.navigateTo(context, breadCrumb.route);
                  },
                ),
              ),
            ));
          } else {
            widgets.add(Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    padding: EdgeInsets.only(top: 6.0, left: 4.0, right: 4.0),
                    child: Icon(
                      Icons.circle,
                      size: 6.0,
                      color: Colors.black38,
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 4.0, right: 4.0),
                    child: breadCrumb.icon != null ?
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Icon(
                          breadCrumb.icon,
                          size: 16.0,
                          color: Colors.lightBlueAccent,
                        ),
                        Text(
                          breadCrumb.text,
                          style: TextStyle(
                            color: Colors.black54,
                            fontSize: 14.0,
                            fontWeight: FontWeight.bold,
                          ),
                        )
                      ],
                    ) :
                    Text(
                      breadCrumb.text,
                      style: TextStyle(
                        color: Colors.black54,
                        fontSize: 14.0,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ],
              ),
            ));
          }
        }
      }
    }
    return Container(
      padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 8.0, top: 8.0),
      height: 38,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    );
  }

}

class BreadCrumb {
  final String text;
  final String route;
  final IconData? icon;
  final Widget? item;
  final GestureTapCallback? onTap;

  BreadCrumb(this.text, this.route, {this.icon, this.item, this.onTap});
}