import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_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/widgets/analytics_date_filter_button.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/widgets/chart_title.dart';
import 'package:syncrow_web/pages/analytics/modules/occupancy/blocs/occupancy/occupancy_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/occupancy/helpers/fetch_occupancy_data_helper.dart';
import 'package:syncrow_web/pages/analytics/modules/occupancy/widgets/occupancy_chart.dart';
import 'package:syncrow_web/pages/analytics/widgets/analytics_chart_empty_state_widget.dart';
import 'package:syncrow_web/pages/analytics/widgets/analytics_error_widget.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart';

class OccupancyChartBox extends StatelessWidget {
  const OccupancyChartBox({super.key});

  @override
  Widget build(BuildContext context) {
    final spaceTreeState = context.watch<SpaceTreeBloc>().state;
    return BlocBuilder<OccupancyBloc, OccupancyState>(
      builder: (context, state) {
        return Container(
          padding: const EdgeInsets.all(30),
          decoration: containerWhiteDecoration,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              AnalyticsErrorWidget(state.errorMessage),
              Row(
                children: [
                  const Expanded(
                    flex: 3,
                    child: FittedBox(
                      alignment: AlignmentDirectional.centerStart,
                      fit: BoxFit.scaleDown,
                      child: ChartTitle(title: Text('Occupancy')),
                    ),
                  ),
                  const Spacer(),
                  Expanded(
                    child: FittedBox(
                      fit: BoxFit.scaleDown,
                      alignment: AlignmentDirectional.centerEnd,
                      child: AnalyticsDateFilterButton(
                        onDateSelected: (DateTime value) {
                          context.read<AnalyticsDatePickerBloc>().add(
                                UpdateAnalyticsDatePickerEvent(montlyDate: value),
                              );
                          if (spaceTreeState.selectedSpaces.isNotEmpty) {
                            FetchOccupancyDataHelper.loadOccupancyChartData(
                              context,
                              spaceUuid:
                                  spaceTreeState.selectedSpaces.firstOrNull ?? '',
                              date: value,
                            );
                          }
                        },
                        selectedDate: context
                            .watch<AnalyticsDatePickerBloc>()
                            .state
                            .monthlyDate,
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              const Divider(),
              const SizedBox(height: 20),
              Visibility(
                visible: state.chartData.isNotEmpty &&
                    state.chartData.every(
                      (e) => e.occupancy.isNotEmpty,
                    ),
                replacement: AnalyticsChartEmptyStateWidget(
                  isLoading: state.status == OccupancyStatus.loading,
                  isError: state.status == OccupancyStatus.failure,
                  isInitial: state.status == OccupancyStatus.initial,
                  errorMessage: state.errorMessage,
                  iconPath: Assets.emptyBarredChart,
                ),
                child: Expanded(
                  child: OccupancyChart(
                    chartData: state.chartData,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
