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/widgets/aqi_distribution_chart.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/widgets/aqi_distribution_chart_title.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/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AirQualityDistributionBloc, AirQualityDistributionState>(
      builder: (context, state) {
        return Container(
          padding: const EdgeInsetsDirectional.all(30),
          decoration: subSectionContainerDecoration.copyWith(
            borderRadius: BorderRadius.circular(30),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (state.errorMessage != null) ...[
                AnalyticsErrorWidget(state.errorMessage),
                const SizedBox(height: 10),
              ],
              AqiDistributionChartTitle(
                isLoading: state.status == AirQualityDistributionStatus.loading,
              ),
              const SizedBox(height: 10),
              const Divider(),
              const SizedBox(height: 20),
              Visibility(
                visible: state.chartData.isNotEmpty,
                replacement: AnalyticsChartEmptyStateWidget(
                  isLoading: state.status == AirQualityDistributionStatus.loading,
                  isError: state.status == AirQualityDistributionStatus.failure,
                  isInitial: state.status == AirQualityDistributionStatus.initial,
                  errorMessage: state.errorMessage,
                  iconPath: Assets.emptyBarredChart,
                ),
                child: Expanded(
                  child: AqiDistributionChart(
                    chartData: state.chartData,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
