import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/payment/operation.dart';

import 'package:pweb/controllers/payouts/payout_volumes.dart';
import 'package:pweb/pages/report/charts/payout_volumes/aggregator.dart';
import 'package:pweb/pages/report/charts/payout_volumes/pie_chart.dart';
import 'package:pweb/pages/report/charts/payout_volumes/range_label.dart';
import 'package:pweb/pages/report/charts/payout_volumes/range_picker.dart';
import 'package:pweb/pages/report/charts/payout_volumes/totals_list.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


class PayoutVolumesChart extends StatelessWidget {
  final List<OperationItem> operations;

  const PayoutVolumesChart({super.key, required this.operations});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => PayoutVolumesController(),
      child: _PayoutVolumesChartBody(operations: operations),
    );
  }
}

class _PayoutVolumesChartBody extends StatelessWidget {
  final List<OperationItem> operations;

  const _PayoutVolumesChartBody({required this.operations});

  @override
  Widget build(BuildContext context) {
    return Consumer<PayoutVolumesController>(
      builder: (context, controller, child) {
        final l10n = AppLocalizations.of(context)!;
        final theme = Theme.of(context);
        final range = controller.range;

        final totals = aggregateCurrencyTotals(operations, range);
        final rangeLabel = formatRangeLabel(context, range);

        return SizedBox(
          height: 200,
          child: Card(
            margin: const EdgeInsets.all(16),
            elevation: 2,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
            child: InkWell(
              borderRadius: BorderRadius.circular(12),
              onTap: () => pickPayoutVolumesRange(context, controller),
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Row(
                      children: [
                        Text(
                          l10n.debitAmountLabel,
                          style: theme.textTheme.titleSmall?.copyWith(
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        const Spacer(),
                        Expanded(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Flexible(
                                child: Text(
                                  rangeLabel,
                                  style: theme.textTheme.labelMedium,
                                  textAlign: TextAlign.right,
                                  maxLines: 2,
                                  softWrap: true,
                                  overflow: TextOverflow.clip,
                                ),
                              ),
                              const SizedBox(width: 6),
                              Icon(
                                Icons.date_range_outlined,
                                size: 16,
                                color: theme.iconTheme.color?.withAlpha(160),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 8),
                    Expanded(
                      child: totals.isEmpty
                          ? Center(child: Text(l10n.noPayouts))
                          : Row(
                              children: [
                                Expanded(
                                  child: PayoutTotalsList(
                                    totals: totals,
                                  ),
                                ),
                                const SizedBox(width: 12),
                                Expanded(
                                  child: PayoutVolumesPieChart(
                                    totals: totals,
                                  ),
                                ),
                              ],
                            ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
