import 'package:design/design.dart';
import 'package:guru_widgets/appbar/guru_app_bar.dart';
import 'package:guru_widgets/common/flexible_container.dart';
import 'package:guru_widgets/tabbar/guru_tab_bar.dart';
import 'package:guru_widgets/theme/guru_theme.dart';

part 'guru_awards_page.g.dart';

@DesignSpec(width: 750, height: 1624)
abstract class GuruAwardsPageDesignSpec implements BasicDesignSpec {
  @SpecHeight(24, consistent: true)
  double get tabbarTopSpacing;

  @SpecHeight(12, consistent: true)
  double get tabbarBottomSpacing;

  static GuruAwardsPageDesignSpec get() => _GuruAwardsPageDesignSpec.get();
}

class GuruAwardsPage extends StatefulWidget {
  /// initialIndex
  /// [GuruAwardsPage] Whether the tab bar is displayed on the awards page
  /// [GuruAwardsPage] awards page 是否显示 tabbar。

  final bool showTabbar;

  /// initialIndex
  /// [GuruTabBar] tab index initially selected. The default value is 0.
  /// [GuruTabBar] 初始选中的标签的index， 默认为0。

  final int? initialIndex;

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

  final List<GuruTabBarItem>? items;

  /// color
  /// [GuruAwardsPage] The page background color.
  /// [GuruAwardsPage] 页面背景颜色

  final Color? backgroundColor;

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

  final ValueChanged<int>? onChange;

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

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

  @override
  State<GuruAwardsPage> createState() => _GuruAwardsPageState();
}

class _GuruAwardsPageState extends State<GuruAwardsPage> {
  int index = 0;

  late GuruAwardsPageDesignSpec designSpec;

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

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

    return Scaffold(
      backgroundColor: widget.backgroundColor ?? colorScheme.backgroundColor,
      appBar: const GuruAppBar(title: 'Awards', leadingType: LeadingType.back),
      body: FlexibleContainer(
        child: widget.showTabbar
          ? GuruTabBar(
              initialIndex: widget.initialIndex,
              tabbarTopSpacing: designSpec.tabbarTopSpacing,
              tabbarBottomSpacing: designSpec.tabbarBottomSpacing,
              items: widget.items ?? [],
              tabViews: widget.bodyBuilders(context, designSpec),
              onChange: widget.onChange,)
          : widget.bodyBuilders(context, designSpec)[0]),
    );
  }
}
