import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/analytics/models/analytics_device.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/energy_management/blocs/realtime_device_changes/realtime_device_changes_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/params/get_analytics_devices_param.dart';
import 'package:syncrow_web/pages/analytics/params/get_occupancy_heat_map_param.dart';
import 'package:syncrow_web/pages/analytics/params/get_occupancy_param.dart';

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

  static void loadOccupancyData(
    BuildContext context, {
    required String communityId,
    required String spaceId,
  }) {
    if (communityId.isEmpty && spaceId.isEmpty) {
      clearAllData(context);
      return;
    }

    final datePickerState = context.read<AnalyticsDatePickerBloc>().state;

    loadAnalyticsDevices(context, communityUuid: communityId, spaceUuid: spaceId);
    final selectedDevice = context.read<AnalyticsDevicesBloc>().state.selectedDevice;

    loadOccupancyChartData(
      context,
      spaceUuid: spaceId,
      date: datePickerState.monthlyDate,
    );
    loadHeatMapData(context, spaceUuid: spaceId, year: datePickerState.yearlyDate);

    if (selectedDevice case final AnalyticsDevice device) {
      context.read<RealtimeDeviceChangesBloc>()
        ..add(const RealtimeDeviceChangesClosed())
        ..add(
          RealtimeDeviceChangesStarted(device.uuid),
        );
    }
  }

  static void loadHeatMapData(
    BuildContext context, {
    required String spaceUuid,
    required DateTime year,
  }) {
    context.read<OccupancyHeatMapBloc>().add(
          LoadOccupancyHeatMapEvent(
            GetOccupancyHeatMapParam(spaceUuid: spaceUuid, year: year),
          ),
        );
  }

  static void loadOccupancyChartData(
    BuildContext context, {
    required String spaceUuid,
    required DateTime date,
  }) {
    context.read<OccupancyBloc>().add(
          LoadOccupancyEvent(
            GetOccupancyParam(
              monthDate: '${date.year}-${date.month.toString().padLeft(2, '0')}',
              spaceUuid: spaceUuid,
            ),
          ),
        );
  }

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

  static void clearAllData(BuildContext context) {
    context.read<OccupancyBloc>().add(
          const ClearOccupancyEvent(),
        );
    context.read<OccupancyHeatMapBloc>().add(
          const ClearOccupancyHeatMapEvent(),
        );
    context.read<RealtimeDeviceChangesBloc>().add(
          const RealtimeDeviceChangesClosed(),
        );

    context.read<AnalyticsDevicesBloc>().add(
          const ClearAnalyticsDeviceEvent(),
        );
  }
}
