import 'package:fis_common/index.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/main/home/controller.dart';
import 'package:get/get.dart';
import 'package:flyinsono/architecture/define.dart';

/// 侧边导航菜单路由
class FSideNavMenuItemRoute {
  FSideNavMenuItemRoute(this.iconData, [this.title = "", this.iconUrl = '']);

  /// 侧边导航栏标题
  final String title;

  /// 侧边导航栏icon
  final IconData iconData;

  final String? iconUrl;
}

/// 侧边导航菜单栏
class FSideNavMenuBar extends StatefulWidget implements FInteractiveContainer {
  @override
  final String pageName;
  FSideNavMenuBar({
    this.pageName = 'FSideNavMenuBar',
    this.width = 120,
    this.routes = const [],
    this.onActiveMenuChanged,
  });

  /// 宽度
  final double width;

  /// 子路由集合
  final List<FSideNavMenuItemRoute> routes;

  /// 菜单切换事件
  final WidgetIndexCallback? onActiveMenuChanged;

  @override
  State<StatefulWidget> createState() => _FSideNavMenuBarState();
}

///FIS导航栏页面状态
class _FSideNavMenuBarState extends State<FSideNavMenuBar> {
  final ValueNotifier _index = ValueNotifier(0);
  final ScrollController _scrollController = ScrollController();
  final _navController = Get.find<HomeNavigatorController>();

  @override
  void initState() {
    super.initState();
    _navController.onRouterToPersonCenter.addListener(_onRouterToPersonCenter);
  }

  @override
  void dispose() {
    _navController.onRouterToPersonCenter
        .removeListener(_onRouterToPersonCenter);
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    final navMenuBar = FValueListenableBuilder(
      builder: (context, index, child) {
        int leng = widget.routes.length;
        final navMenuBarList = <FWidget>[];
        for (var i = 0; i < leng; i++) {
          navMenuBarList.add(
            _FSideNavMenuItem(
              widget,
              widget.routes[i],
              index == i,
              () {
                _index.value = i;
                widget.onActiveMenuChanged?.call(context, i);
              },
              iconUrl: widget.routes[i].iconUrl,
            ),
          );
        }
        return FAlwaysScrollListView(
          scrollController: _scrollController,
          child: QuickFWidget(ScrollConfiguration(
              behavior:
                  ScrollConfiguration.of(context).copyWith(scrollbars: false),
              child: FListView(
                children: navMenuBarList,
                controller: _scrollController,
              ))),
        );
      },
      valueListenable: _index,
    );
    return FContainer(
      child: FThemeWidget(
        child: FScrollbar(
          child: navMenuBar,
        ),
        data: Theme.of(context).copyWith(
          scrollbarTheme: ScrollbarThemeData(
            thumbColor: MaterialStateProperty.all(
              Colors.white.withOpacity(.4),
            ),
          ),
        ),
      ),
    );
  }

  void _onRouterToPersonCenter(e, _) {
    _index.value = -1;
  }
}

///FIS侧边导航栏条目
class _FSideNavMenuItem extends FStatelessWidget {
  _FSideNavMenuItem(
      this.businessParent, this.route, this.active, this.onActived,
      {this.iconUrl});
  final FInteractiveContainer businessParent;
  final FSideNavMenuItemRoute route;
  final bool active;
  final VoidCallback onActived;
  final double width = 120;
  final String? iconUrl;

  static const double _height = 68;

  @override
  FWidget build(BuildContext context) {
    Color? bgColor = active ? Theme.of(context).primaryColor : null;
    var icon = FIcon(
      route.iconData,
      size: 36,
      color: Colors.white,
    );
    final navMenuItem = FContainer(
      width: this.width,
      height: _height,
      color: bgColor,
      alignment: Alignment.topCenter,
      child: FColumn(
        children: [
          if (!active) const _FSideNavMenuItemLine(),
          FExpanded(
            child: iconUrl.isNotNullOrEmpty
                ? FImage.network(
                    iconUrl!,
                    errorBuilder: (context, error, stackTrace) {
                      return icon;
                    },
                  )
                : icon,
          ),
        ],
      ),
    );

    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        businessParent: this.businessParent,
        name: route.title,
        onTap: () {
          this.onActived.call();
        },
        child: FTextTooltip(
          position: DisplayPosition.right,
          textStyle: TextStyle(
            fontSize: 16,
            color: Colors.white,
          ),
          height: 36,
          message: route.title,
          child: navMenuItem,
          margin: EdgeInsets.all(0),
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(1.0),
            borderRadius: BorderRadius.circular(4),
          ),
        ),
      ),
    );
  }
}

///FIS侧边导航栏条目间隔横线
class _FSideNavMenuItemLine extends FStatelessWidget {
  const _FSideNavMenuItemLine();

  static final _lightColor = Color.fromARGB(255, 48, 74, 132);
  static final _dartColor = Color.fromARGB(255, 10, 31, 48);

  static const double _width = 42;
  static const double _height = 2.4;

  @override
  FWidget build(BuildContext context) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _createPartLine(
          _dartColor,
          _lightColor,
        ),
        _createPartLine(
          _lightColor,
          _dartColor,
        ),
      ],
    );
  }

  _createPartLine(Color left, Color right) {
    return FContainer(
      height: _height,
      width: _width,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [
            left,
            right,
          ],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        ),
      ),
    );
  }
}
