import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class WaveBottomNavigationItem {
  final String title;
  final Widget icon;

  WaveBottomNavigationItem({required this.title, required this.icon});
}

class WaveBottomNavigation extends StatefulWidget {
  const WaveBottomNavigation({super.key, this.initPage = 0, required this.items, required this.onPageChanged});

  final List<WaveBottomNavigationItem> items;
  final Function(int) onPageChanged;
  final int initPage;

  @override
  State<WaveBottomNavigation> createState() => _WaveBottomNavigationState();
}

class _WaveBottomNavigationState extends State<WaveBottomNavigation> {
  late PageController _controller;

  @override
  void initState() {
    super.initState();
    _controller = PageController(viewportFraction: 0.3, initialPage: widget.initPage);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  double _calculateScale(double currentPage, int index) {
    final double distance = (currentPage - index).abs();
    if (distance >= 1) {
      return 0.9;
    } else {
      return 1.50;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 68,
      color: AppColor.blueNormal,
      clipBehavior: Clip.none,
      padding: const EdgeInsets.all(10),
      child: Stack(
        clipBehavior: Clip.none,
        alignment: Alignment.center,
        children: [backgroundWidget(), waveScrolledLists()],
      ),
    );
  }

  Widget backgroundWidget() {
    return Positioned(
      top: -43,
      child: Container(
        width: 90,
        height: 90,
        clipBehavior: Clip.none,
        decoration: ShapeDecoration(
          color: AppColor.greenNormal,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(200),
            side: BorderSide(width: 5, color: Colors.white),
          ),
        ),
      ),
    );
  }

  Widget waveScrolledLists() {
    return PageView.builder(
      controller: _controller,
      clipBehavior: Clip.none,
      onPageChanged: widget.onPageChanged,
      scrollDirection: Axis.horizontal,
      itemCount: widget.items.length,
      physics: const ClampingScrollPhysics(),
      itemBuilder: (context, index) {
        final WaveBottomNavigationItem item = widget.items[index];
        return GestureDetector(
          onTap: () {
            _controller.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
          },
          child: Center(
            child: AnimatedBuilder(
              animation: _controller,
              builder: (context, child) {
                double value = 0.0;
                final scale = _calculateScale(_controller.page ?? _controller.initialPage.toDouble() ?? 0.0, index);
                value = index - (_controller.page ?? _controller.initialPage.toDouble() ?? 0.0);
                value = (value).clamp(-1, 1);
                double offset = value * 30;
                if (value.abs() < 0.2 || value.abs() > 0.2) {
                  offset = -15 * (1 - value.abs() * 2);
                }

                return Transform.scale(
                  scale: scale,
                  child: Transform.translate(
                    offset: Offset(0, offset),
                    child: Column(
                      children: [
                        Tooltip(message: item.title, child: item.icon),
                        Visibility(
                          visible: (_controller.page ?? 0) == index,
                          child: Text(item.title, style: AppFonts.yekan10.copyWith(color: Colors.white)),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        );
      },
    );
  }
}
