import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/usual/hover_text_button.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/desktop/names.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/router/translator.dart';
import 'package:flyinsono/pages/main/home/index.dart';
import 'package:get/get.dart';

class FHeaderCrumbs extends FStatelessWidget {
  /// 前景色
  static const Color C_FOREGROUND_COLOR = Colors.white;

  /// 高度
  static const double C_BAR_HEIGHT = 32.0;

  /// 边框圆角
  static const double C_BORDER_RADIUS = C_BAR_HEIGHT / 2;

  FHeaderCrumbs({required this.businessParent});

  final FPage businessParent;

  final _homeController = Get.find<HomeController>();
  final _navController = Get.find<HomeNavigatorController>();

  @override
  FWidget build(BuildContext context) {
    final isTopOnly = _navController.routeDepth == 1; // 是否只有一级页面
    final child = isTopOnly ? _buildTopOnlyBody() : _buildMultiRouteBody();

    return FContainer(
      height: C_BAR_HEIGHT,
      decoration: BoxDecoration(
        border: Border.all(color: C_FOREGROUND_COLOR),
        borderRadius: BorderRadius.circular(C_BORDER_RADIUS),
      ),
      padding: const EdgeInsets.symmetric(horizontal: C_BORDER_RADIUS),
      child: child,
    );
  }

  FWidget _buildTopOnlyBody() {
    final routeName = _navController.routeStack.first;
    return FCenter(
      child: _buildRouteLink(routeName),
    );
  }

  FWidget _buildMultiRouteBody() {
    return FRow(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        _buildBackButton(),
        FSizedBox(width: 5),
        FVerticalDivider(
          color: Colors.white,
          // indent: 4,
          // endIndent: 4,
          thickness: 1,
        ),
        FSizedBox(width: 5),
        ..._buildRouteStackWidgetList(),
      ],
    );
  }

  List<FWidget> _buildRouteStackWidgetList() {
    final stack = _navController.routeStack;
    if (stack.isEmpty) {
      return [];
    }
    final arr = <FWidget>[
      _buildRouteLink(stack.first),
    ];
    final len = stack.length;
    for (int i = 1; i < len; i++) {
      final name = stack[i];
      arr.add(_StackForwordIcon());
      arr.add(_buildRouteLink(name));
    }
    return arr;
  }

  FWidget _buildBackButton() {
    return FHoverTextButton(
      businessParent: businessParent,
      name: 'back',
      label: i18nBook.common.back.t,
      fontSize: FFontSizes.BODY,
      fontWeight: FontWeight.w500,
      fontHeight: 1.2,
      iconData: Icons.arrow_back_ios_new_rounded,
      color: C_FOREGROUND_COLOR,
      onTap: () {
        router.back(id: NavIds.HOME);
        _navController.subBack();
        _homeController.state.currentRouter = '';
      },
    );
  }

  FWidget _buildRouteLink(String routeName) {
    final title = RouteTranslator.translate(routeName) ?? " ";
    const btnName = 'crumbs_back';
    return _createTextButton(
      title,
      btnName,
      () {
        _navController.backTo(routeName);
      },
    );
  }

  FWidget _createTextButton(String label, String name, VoidCallback onTap) {
    return FHoverTextButton(
      businessParent: businessParent,
      name: name,
      label: label,
      fontSize: FFontSizes.BODY,
      fontWeight: FontWeight.w500,
      fontHeight: 1.2,
      color: C_FOREGROUND_COLOR,
      onTap: onTap,
    );
  }
}

class _StackForwordIcon extends FStatelessWidget {
  const _StackForwordIcon();

  @override
  FWidget build(BuildContext context) {
    return FIcon(
      Icons.chevron_right_rounded,
      size: FFontSizes.H6,
      color: Colors.white,
    );
  }
}
