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/air_quality/widgets/aqi_type_dropdown.dart';
import 'package:syncrow_web/pages/analytics/modules/analytics/blocs/analytics_devices/analytics_devices_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/params/get_air_quality_distribution_param.dart';
import 'package:syncrow_web/pages/analytics/params/get_analytics_devices_param.dart';
import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.dart';
import 'package:syncrow_web/pages/analytics/params/get_range_of_aqi_param.dart';

abstract final class FetchAirQualityDataHelper {
  const FetchAirQualityDataHelper._();

  static void loadAirQualityData(
    BuildContext context, {
    required DateTime date,
    required String communityUuid,
    required String spaceUuid,
    bool shouldFetchAnalyticsDevices = true,
  }) {
    final aqiType = context.read<AirQualityDistributionBloc>().state.selectedAqiType;
    if (shouldFetchAnalyticsDevices) {
      loadAnalyticsDevices(
        context,
        communityUuid: communityUuid,
        spaceUuid: spaceUuid,
      );
    }
    loadRangeOfAqi(
      context,
      spaceUuid: spaceUuid,
      date: date,
    );
    loadAirQualityDistribution(
      context,
      spaceUuid: spaceUuid,
      date: date,
      aqiType: aqiType,
    );
  }

  static void clearAllData(BuildContext context) {
    context.read<AnalyticsDevicesBloc>().add(
          const ClearAnalyticsDeviceEvent(),
        );
    context.read<RealtimeDeviceChangesBloc>().add(
          const RealtimeDeviceChangesClosed(),
        );
    context.read<AirQualityDistributionBloc>().add(
          const ClearAirQualityDistribution(),
        );
    context.read<RangeOfAqiBloc>().add(const ClearRangeOfAqiEvent());

    context.read<DeviceLocationBloc>().add(const ClearDeviceLocationEvent());
  }

  static void loadAnalyticsDevices(
    BuildContext context, {
    required String communityUuid,
    required String spaceUuid,
  }) {
    context.read<AnalyticsDevicesBloc>().add(
          LoadAnalyticsDevicesEvent(
            param: GetAnalyticsDevicesParam(
              communityUuid: communityUuid,
              spaceUuid: spaceUuid,
              deviceTypes: ['AQI'],
              requestType: AnalyticsDeviceRequestType.occupancy,
            ),
            onSuccess: (device) {
              context.read<RealtimeDeviceChangesBloc>()
                ..add(const RealtimeDeviceChangesClosed())
                ..add(RealtimeDeviceChangesStarted(device.uuid));

              context.read<DeviceLocationBloc>().add(
                    LoadDeviceLocationEvent(
                      GetDeviceLocationDataParam(
                        latitude: device.latitude ?? 0,
                        longitude: device.longitude ?? 0,
                      ),
                    ),
                  );
            },
          ),
        );
  }

  static void loadRangeOfAqi(
    BuildContext context, {
    required String spaceUuid,
    required DateTime date,
  }) {
    context.read<RangeOfAqiBloc>().add(
          LoadRangeOfAqiEvent(
            GetRangeOfAqiParam(
              date: date,
              spaceUuid: spaceUuid,
            ),
          ),
        );
  }

  static void loadAirQualityDistribution(
    BuildContext context, {
    required String spaceUuid,
    required DateTime date,
    required AqiType aqiType,
  }) {
    context.read<AirQualityDistributionBloc>().add(
          LoadAirQualityDistribution(
            GetAirQualityDistributionParam(
              spaceUuid: spaceUuid,
              date: date,
              aqiType: aqiType,
            ),
          ),
        );
  }
}
