import 'package:design/design.dart';
import 'package:guru_widgets/navigationbar/guru_navigation_bar.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

part 'guru_navigation_page.g.dart';

@DesignSpec(width: 750, height: 1624)
abstract class GuruNavigationPageDesignSpec implements BasicDesignSpec {

  static GuruNavigationPageDesignSpec get() => _GuruNavigationPageDesignSpec.get();
}

typedef NavigationPageBodyBuilder<T> = List<Widget> Function(BuildContext, T);


class GuruNavigationPage extends StatefulWidget {
  /// initialIndex
  /// [GuruNavigationBar] tab index initially selected. The default value is 0.
  /// [GuruNavigationBar] 初始选中的标签的index， 默认为0。

  final int initialIndex;

  /// Items
  /// [GuruNavigationBar] tabs's configure, Up to four items, item is [GuruNavigationBarItem].
  /// [GuruNavigationBar] 配置选项卡内容，最多四个标签项, 标签为[GuruNavigationBarItem]。

  final List<GuruNavigationBarItem> items;

  /// showLabels
  /// [GuruNavigationBar] The navigation bar show labels.
  /// [GuruNavigationBar] 导航栏是否显示labels
  
  final bool? showLabels;

  /// backgroundColor
  /// [GuruNavigationPage] The navigation page backgroundColor

  final Color? backgroundColor;

  /// onChange 
  /// [GuruNavigationBar] The callback function for tab index change.
  /// [GuruNavigationBar] 标签页切换的回调函数

  final ValueChanged<int>? onChange;

  final List<Widget> Function(BuildContext, GuruNavigationPageDesignSpec)  bodyBuilders;

  const GuruNavigationPage({
    super.key, 
    this.initialIndex = 0,
    required this.items, 
    this.showLabels = false,
    this.backgroundColor,
    this.onChange,
    required this.bodyBuilders});

  @override
  State<GuruNavigationPage> createState() => _GuruNavigationPageState();
}

class _GuruNavigationPageState extends State<GuruNavigationPage> {
  int index = 0;

  late GuruNavigationPageDesignSpec designSpec;

  @override
  void initState() {
    setState(() {
      index = widget.initialIndex;
    });
    designSpec = GuruNavigationPageDesignSpec.get();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final guruTheme = GuruTheme.of(context);
    final colorScheme = guruTheme.colorScheme;

    return Scaffold(
      backgroundColor: widget.backgroundColor ?? colorScheme.backgroundColor,
      body: IndexedStack(
        index: index,
        sizing: StackFit.expand,
        children: widget.bodyBuilders(context, designSpec)),
      bottomNavigationBar: GuruNavigationBar(
        currentIndex: index,
        items: widget.items,
        showLabels: widget.showLabels,
        onChange: (value) {
          setState(() {
            index = value;
          });
          widget.onChange?.call(value);
        },
      ),
    );
  }
}
