import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_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/energy_management/widgets/chart_title.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/widgets/total_energy_consumption_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/analytics/widgets/charts_loading_widget.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<TotalEnergyConsumptionBloc, TotalEnergyConsumptionState>(
      builder: (context, state) => Container(
        decoration: subSectionContainerDecoration.copyWith(
          borderRadius: BorderRadius.circular(30),
        ),
        padding: const EdgeInsets.all(30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            AnalyticsErrorWidget(state.errorMessage),
            Row(
              children: [
                ChartsLoadingWidget(
                  isLoading: state.status == TotalEnergyConsumptionStatus.loading,
                ),
                const Expanded(
                  flex: 3,
                  child: FittedBox(
                    alignment: AlignmentDirectional.centerStart,
                    fit: BoxFit.scaleDown,
                    child: ChartTitle(title: Text('Space energy consumed')),
                  ),
                ),
                const Spacer(flex: 4),
              ],
            ),
            const SizedBox(height: 20),
            const Divider(),
            const SizedBox(height: 20),
            Visibility(
              visible: state.chartData.isNotEmpty &&
                  state.chartData.every((e) => e.value != 0),
              replacement: AnalyticsChartEmptyStateWidget(
                isLoading: state.status == TotalEnergyConsumptionStatus.loading,
                isError: state.status == TotalEnergyConsumptionStatus.failure,
                isInitial: state.status == TotalEnergyConsumptionStatus.initial,
                errorMessage: state.errorMessage,
                iconPath: Assets.emptyEnergyManagementChart,
              ),
              child: TotalEnergyConsumptionChart(chartData: state.chartData),
            ),
          ],
        ),
      ),
    );
  }
}
