import 'package:citycards_customer/itinerary_creation/bloc/itinerary_steps_selection_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'itinerary_creation_steps/art_gallery_selection_view.dart';
import 'itinerary_creation_steps/city_selection_view.dart';
import 'itinerary_creation_steps/date_selection_view.dart';
import 'itinerary_creation_steps/dietary_selection_view.dart';
import 'itinerary_creation_steps/energy_selection_view.dart';
import 'itinerary_creation_steps/historical_site_rating_view.dart';
import 'itinerary_creation_steps/itinerary_completion_view.dart';
import 'itinerary_creation_steps/kids_selection_view.dart';
import 'itinerary_creation_steps/scenic_viewpoints_rating_view.dart';
import 'itinerary_creation_steps/shopping_rating_view.dart';
import 'itinerary_creation_steps/wildlife_rating_view.dart';

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

  @override
  State<ItineraryCreationPage> createState() => _ItineraryCreationPageState();
}

class _ItineraryCreationPageState extends State<ItineraryCreationPage> {
  final PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFFFF5F5),
      appBar: AppBar(
        backgroundColor: Color(0xFFFFF5F5),
        centerTitle: true,
        leading: GestureDetector(
          onTap: () {
            context.read<ItineraryStepNavigationBloc>().add(
              ItineraryStepNavigationPreviousEvent(),
            );
          },
          child: Icon(Icons.arrow_back),
        ),
        title:
            BlocBuilder<
              ItineraryStepNavigationBloc,
              ItineraryStepNavigationState
            >(
              builder: (context, state) {
                return Text(
                  "${state.selectedIndex} / 10",
                  style: TextStyle(color: Color(0xFF4A5565), fontSize: 14.sp),
                );
              },
            ),
      ),
      body:
          BlocListener<
            ItineraryStepNavigationBloc,
            ItineraryStepNavigationState
          >(
            listener: (context, state) {
              _pageController.animateToPage(
                state.selectedIndex,
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeInOut,
              );
            },
            child: Column(
              children: [
                Padding(
                  padding: EdgeInsets.only(
                    left: 20.w,
                    right: 20.w,
                    bottom: 20.h,
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child:
                        BlocBuilder<
                          ItineraryStepNavigationBloc,
                          ItineraryStepNavigationState
                        >(
                          builder: (context, state) {
                            return LinearProgressIndicator(
                              value: state.selectedIndex / 10,
                              borderRadius: BorderRadius.circular(10),
                              backgroundColor: Colors.white,
                              color: const Color(0xFFF95F62),
                              minHeight: 6.h,
                            );
                          },
                        ),
                  ),
                ),

                Expanded(
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    child: PageView(
                      controller: _pageController,
                      physics: const NeverScrollableScrollPhysics(),
                      children: [
                        DateSelectionView(),
                        CitySelectionView(),
                        EnergySelectionView(),
                        KidsSelectionView(),
                        DietarySelectionView(),
                        ArtGallerySelectionView(),
                        ScenicViewpointsRatingView(),
                        HistoricalSiteRatingView(),
                        WildlifeRatingView(),
                        ShoppingRatingView(),
                        ItineraryCompletionView(),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
    );
  }
}
