import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/access_management/bloc/access_bloc.dart';
import 'package:syncrow_web/pages/access_management/bloc/access_event.dart';
import 'package:syncrow_web/pages/access_management/booking_system/view/booking_page.dart';
import 'package:syncrow_web/pages/access_management/manage_bookable_spaces/presentation/screens/manage_bookable_spaces_screen.dart';
import 'package:syncrow_web/pages/access_management/view/access_overview_content.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
import 'package:syncrow_web/utils/theme/responsive_text_theme.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';

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

  @override
  State<AccessManagementPage> createState() => _AccessManagementPageState();
}

class _AccessManagementPageState extends State<AccessManagementPage>
    with HelperResponsiveLayout {
  final PageController _pageController = PageController(initialPage: 0);
  int _currentPageIndex = 0;

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => AccessBloc()..add(FetchTableData()),
      child: WebScaffold(
        enableMenuSidebar: false,
        appBarTitle: Text(
          'Access Management',
          style: ResponsiveTextTheme.of(context).deviceManagementTitle,
        ),
        centerBody: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextButton(
              onPressed: () => _switchPage(0),
              child: Text(
                'Access Overview',
                style: context.textTheme.titleMedium?.copyWith(
                  color: _currentPageIndex == 0 ? Colors.white : Colors.grey,
                  fontWeight: _currentPageIndex == 0
                      ? FontWeight.w700
                      : FontWeight.w400,
                ),
              ),
            ),
            TextButton(
              onPressed: () => _switchPage(1),
              child: Text(
                'Booking System',
                style: context.textTheme.titleMedium?.copyWith(
                  color: _currentPageIndex == 1 ? Colors.white : Colors.grey,
                  fontWeight: _currentPageIndex == 1
                      ? FontWeight.w700
                      : FontWeight.w400,
                ),
              ),
            ),
          ],
        ),
        rightBody: const NavigateHomeGridView(),
        scaffoldBody: PageView(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: [
            const AccessOverviewContent(),
            BookingPage(
              pageController: _pageController,
            ),
            ManageBookableSpacesPage(
              pageController: _pageController,
            ),
          ],
        ),
      ),
    );
  }

  void _switchPage(int index) {
    setState(() => _currentPageIndex = index);
    _pageController.jumpToPage(index);
  }
}
