import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../../application/analytic/profit_loss_analytic_loader/profit_loss_analytic_loader_bloc.dart';
import '../../../../../../common/data/report_menu.dart';
import '../../../../../../common/extension/extension.dart';
import '../../../../../../common/theme/theme.dart';
import '../../../../../components/error/analytic_error_state_widget.dart';
import '../../../../../components/loader/loader_with_text.dart';
import '../../../../../components/spaces/space.dart';
import '../../../../../components/widgets/report/report_header.dart';
import '../../../../../components/widgets/report/report_summary_card.dart';
import '../widgets/profit_loss/report_profit_loss_breakdown.dart';
import '../widgets/profit_loss/report_profit_loss_metric.dart';
import '../widgets/profit_loss/report_profit_loss_product_profitability.dart';
import '../widgets/profit_loss/report_profit_loss_trend_chart.dart';

class ReportProfitLossSection extends StatelessWidget {
  final ReportMenu menu;
  final ProfitLossAnalyticLoaderState state;
  final DateTime startDate;
  final DateTime endDate;

  const ReportProfitLossSection({
    super.key,
    required this.menu,
    required this.state,
    required this.startDate,
    required this.endDate,
  });

  @override
  Widget build(BuildContext context) {
    if (state.isFetching) {
      return const Center(child: LoaderWithText());
    }

    return state.failureOption.fold(
      () => RefreshIndicator(
        onRefresh: () {
          context.read<ProfitLossAnalyticLoaderBloc>().add(
            ProfitLossAnalyticLoaderEvent.fetched(
              startDate: startDate,
              endDate: endDate,
            ),
          );
          return Future.value();
        },
        child: ListView(
          padding: const EdgeInsets.all(16),
          children: [
            ReportHeader(menu: menu, endDate: endDate, startDate: startDate),
            _buildSummary(),
            ReportProfitLossTrendChart(data: state.profitLossAnalytic.data),
            Padding(
              padding: const EdgeInsets.only(top: 16),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                    flex: 3,
                    child: ReportProfitLossProductProfitability(
                      data: state.profitLossAnalytic.productData,
                    ),
                  ),
                  const SpaceWidth(12),
                  Expanded(
                    flex: 2,
                    child: ReportProfitLossBreakdown(
                      data: state.profitLossAnalytic,
                    ),
                  ),
                ],
              ),
            ),
            ReportProfitLossMetric(data: state.profitLossAnalytic),
          ],
        ),
      ),
      (f) => AnalyticErrorStateWidget(
        failure: f,
        menu: menu,
        onRefresh: () {
          context.read<ProfitLossAnalyticLoaderBloc>().add(
            ProfitLossAnalyticLoaderEvent.fetched(
              startDate: startDate,
              endDate: endDate,
            ),
          );
        },
      ),
    );
  }

  Padding _buildSummary() {
    return Padding(
      padding: const EdgeInsets.only(top: 16),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: ReportSummaryCard(
                  color: AppColor.success,
                  icon: Icons.attach_money,
                  title: 'Jumlah Pendapatan',
                  value: state
                      .profitLossAnalytic
                      .summary
                      .totalRevenue
                      .currencyFormatRpV2,
                ),
              ),
              const SpaceWidth(12),
              Expanded(
                child: ReportSummaryCard(
                  color: AppColor.info,
                  icon: Icons.receipt_outlined,
                  title: 'Jumlah Biaya',
                  value: state.profitLossAnalytic.summary.totalCost
                      .toDouble()
                      .currencyFormatRpV2,
                ),
              ),
            ],
          ),
          const SpaceHeight(12),
          Row(
            children: [
              Expanded(
                child: ReportSummaryCard(
                  color: AppColor.warning,
                  icon: Icons.trending_up_outlined,
                  title: 'Laba Kotor',
                  value: state.profitLossAnalytic.summary.grossProfit
                      .toDouble()
                      .currencyFormatRpV2,
                ),
              ),
              const SpaceWidth(12),
              Expanded(
                child: ReportSummaryCard(
                  color: AppColor.primary,
                  icon: Icons.account_balance_outlined,
                  title: 'Laba Bersih',
                  value: state.profitLossAnalytic.summary.netProfit
                      .toDouble()
                      .currencyFormatRpV2,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
