import 'package:flutter/material.dart';
import 'package:gz_video_player/video_speed.dart';

class VideoSpeedBar extends StatelessWidget {
  final List<VideoSpeedItem> speedItemList;
  final VideoSpeedItemStyle style;
  final ValueChanged<int>? onTapItem;
  final bool hidden;

  VideoSpeedBar({
    super.key,
    required this.speedItemList,
    VideoSpeedItemStyle? style,
    this.onTapItem,
    this.hidden = true,
  }) : style = style ?? VideoSpeedItemStyle();

  @override
  Widget build(BuildContext context) {
    return Offstage(
      offstage: hidden,
      child: Align(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: buildItems(),
        ),
      ),
    );
  }

  List<Widget> buildItems() {
    List<Widget> list = [];
    for (int i = 0; i < speedItemList.length; i++) {
      VideoSpeedItem item = speedItemList[i];
      Widget widget = VideoSpeedItemWidget(
        style: style,
        title: item.title,
        onTap: () {
          onTapItem?.call(i);
        },
      );
      list.add(widget);
    }
    return list;
  }
}

class VideoSpeedItemWidget extends StatelessWidget {
  final VoidCallback? onTap;
  final VideoSpeedItemStyle style;
  final String title;

  const VideoSpeedItemWidget({
    super.key,
    required this.style,
    required this.title,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Padding(
        padding: style.padding,
        child: Container(
          width: style.itemSize.width,
          height: style.itemSize.height,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: style.backgroundColor,
            borderRadius: style.radius,
          ),
          child: Padding(
            padding: style.margin,
            child: Text(
              title,
              style: style.textStyle,
            ),
          ),
        ),
      ),
    );
  }
}
