mirrored_repositories/future_of_workout/lib/src/setup
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:future_of_workout/src/setup/setup.dart';
import 'package:future_of_workout/src/setup/view/view.dart';
import 'package:future_of_workout/src/widgets/widgets.dart';
import 'package:measurement_repository/measurement_repository.dart';
import 'package:user_repository/user_repository.dart';

class SetupPage extends StatelessWidget {
  const SetupPage({super.key});

  static String name = '/setup';
  static String path = name;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SetupBloc(
        userRepository: context.read<UserRepository>(),
        measurementRepository: context.read<MeasurementRepository>(),
      ),
      child: const SetupPageView(),
    );
  }
}

class SetupPageView extends StatefulWidget {
  const SetupPageView({super.key});

  @override
  State<SetupPageView> createState() => _SetupPageViewState();
}

class _SetupPageViewState extends State<SetupPageView> {
  late PageController _pageController;

  int _pageIndex = 0;

  @override
  void initState() {
    _pageController = PageController(initialPage: _pageIndex);
    super.initState();
  }

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

  final _pages = const <Widget>[
    GenderSetup(),
    AgeSetup(),
    HeightSetup(),
    WeightSetup(),
    FinishSetup(),
  ];

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      body: SafeArea(
        child: Padding(
          padding:
              const EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 32),
          child: Column(
            children: [
              PositionIndicator(
                length: _pages.length,
                pageIndex: _pageIndex,
              ),
              Expanded(
                child: PageView(
                  physics: const NeverScrollableScrollPhysics(),
                  onPageChanged: (value) => setState(() {
                    _pageIndex = value;
                  }),
                  controller: _pageController,
                  children: _pages,
                ),
              ),
              ButtonsRow(
                length: _pages.length,
                pageIndex: _pageIndex,
                nextPage: () {
                  _pageController.nextPage(
                    duration: const Duration(milliseconds: 300),
                    curve: Curves.ease,
                  );
                },
                previousPage: () {
                  _pageController.previousPage(
                    duration: const Duration(milliseconds: 300),
                    curve: Curves.ease,
                  );
                },
                onFinish: () {
                  context.read<SetupBloc>().add(const SetupFinish());
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}
