import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/blocs/air_quality_distribution/air_quality_distribution_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/blocs/device_location/device_location_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/blocs/range_of_aqi/range_of_aqi_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/blocs/analytics_date_picker_bloc/analytics_date_picker_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/blocs/analytics_devices/analytics_devices_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/blocs/analytics_tab/analytics_tab_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/widgets/analytics_communities_sidebar.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/widgets/analytics_page_tabs_and_children.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/energy_consumption_by_phases/energy_consumption_by_phases_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/energy_consumption_per_device/energy_consumption_per_device_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/power_clamp_info/power_clamp_info_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/realtime_device_changes/realtime_device_changes_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/total_energy_consumption/total_energy_consumption_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/occupancy/blocs/occupancy/occupancy_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/occupancy/blocs/occupancy_heat_map/occupancy_heat_map_bloc.dart';
import 'package:syncrow_web/pages/analytics/services/air_quality_distribution/remote_air_quality_distribution_service.dart';
import 'package:syncrow_web/pages/analytics/services/analytics_devices/analytics_devices_service_delagate.dart';
import 'package:syncrow_web/pages/analytics/services/analytics_devices/remote_energy_management_analytics_devices_service.dart';
import 'package:syncrow_web/pages/analytics/services/analytics_devices/remote_occupancy_analytics_devices_service.dart';
import 'package:syncrow_web/pages/analytics/services/device_location/device_location_details_service_decorator.dart';
import 'package:syncrow_web/pages/analytics/services/device_location/remote_device_location_service.dart';
import 'package:syncrow_web/pages/analytics/services/energy_consumption_by_phases/remote_energy_consumption_by_phases_service.dart';
import 'package:syncrow_web/pages/analytics/services/energy_consumption_per_device/remote_energy_consumption_per_device_service.dart';
import 'package:syncrow_web/pages/analytics/services/occupacy/remote_occupancy_service.dart';
import 'package:syncrow_web/pages/analytics/services/occupancy_heat_map/remote_occupancy_heat_map_service.dart';
import 'package:syncrow_web/pages/analytics/services/power_clamp_info/remote_power_clamp_info_service.dart';
import 'package:syncrow_web/pages/analytics/services/range_of_aqi/remote_range_of_aqi_service.dart';
import 'package:syncrow_web/pages/analytics/services/realtime_device_service/firebase_realtime_device_service.dart';
import 'package:syncrow_web/pages/analytics/services/total_energy_consumption/remote_total_energy_consumption_service.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
import 'package:syncrow_web/services/api/http_service.dart';
import 'package:syncrow_web/utils/theme/responsive_text_theme.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';

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

  @override
  State<AnalyticsPage> createState() => _AnalyticsPageState();
}

class _AnalyticsPageState extends State<AnalyticsPage> {
  late final HTTPService _httpService;

  @override
  void initState() {
    super.initState();
    _httpService = HTTPService();
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AnalyticsTabBloc>(
          create: (context) => AnalyticsTabBloc(),
        ),
        BlocProvider(
          create: (context) => TotalEnergyConsumptionBloc(
            RemoteTotalEnergyConsumptionService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => EnergyConsumptionByPhasesBloc(
            RemoteEnergyConsumptionByPhasesService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => EnergyConsumptionPerDeviceBloc(
            RemoteEnergyConsumptionPerDeviceService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => PowerClampInfoBloc(
            RemotePowerClampInfoService(_httpService),
          ),
        ),
        BlocProvider<RealtimeDeviceChangesBloc>(
          create: (context) => RealtimeDeviceChangesBloc(
            FirebaseRealtimeDeviceService(),
          ),
        ),
        BlocProvider(
          create: (context) => OccupancyBloc(
            RemoteOccupancyService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => OccupancyHeatMapBloc(
            RemoteOccupancyHeatMapService(_httpService),
          ),
        ),
        BlocProvider(create: (context) => AnalyticsDatePickerBloc()),
        BlocProvider(
          create: (context) => AnalyticsDevicesBloc(
            AnalyticsDevicesServiceDelegate(
              RemoteOccupancyAnalyticsDevicesService(_httpService),
              RemoteEnergyManagementAnalyticsDevicesService(_httpService),
            ),
          ),
        ),
        BlocProvider(
          create: (context) => RangeOfAqiBloc(
            RemoteRangeOfAqiService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => AirQualityDistributionBloc(
            RemoteAirQualityDistributionService(_httpService),
          ),
        ),
        BlocProvider(
          create: (context) => DeviceLocationBloc(
            DeviceLocationDetailsServiceDecorator(
              RemoteDeviceLocationService(_httpService),
              Dio(
                BaseOptions(
                  baseUrl: 'https://nominatim.openstreetmap.org/',
                ),
              ),
            ),
          ),
        ),
      ],
      child: const AnalyticsPageForm(),
    );
  }
}

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

  @override
  State<AnalyticsPageForm> createState() => _AnalyticsPageFormState();
}

class _AnalyticsPageFormState extends State<AnalyticsPageForm> {
  @override
  void initState() {
    context.read<SpaceTreeBloc>().add(InitialEvent());
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return WebScaffold(
      rightBody: const NavigateHomeGridView(),
      appBarTitle: Text(
        'Syncrow Analytics',
        style: ResponsiveTextTheme.of(context).deviceManagementTitle,
      ),
      enableMenuSidebar: false,
      scaffoldBody: const Row(
        children: [
          AnalyticsCommunitiesSidebar(),
          Expanded(flex: 5, child: AnalyticsPageTabsAndChildren()),
        ],
      ),
    );
  }
}
