import 'package:flutter/material.dart';

import 'package:pshared/utils/currency.dart';

import 'package:pweb/models/wallet_transaction.dart';
import 'package:pweb/pages/payout_page/wallet/history/chip.dart';
import 'package:pweb/pages/report/table/badge.dart';

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


class WalletTransactionsTable extends StatelessWidget {
  final List<WalletTransaction> transactions;

  const WalletTransactionsTable({super.key, required this.transactions});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final loc = AppLocalizations.of(context)!;

    if (transactions.isEmpty) {
      return Card(
        color: theme.colorScheme.onSecondary,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Text(loc.walletHistoryEmpty),
        ),
      );
    }

    return Card(
      color: theme.colorScheme.onSecondary,
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: DataTable(
            columnSpacing: 18,
            headingTextStyle: const TextStyle(fontWeight: FontWeight.w600),
            columns: [
              DataColumn(label: Text(loc.colStatus)),
              DataColumn(label: Text(loc.colType)),
              DataColumn(label: Text(loc.colAmount)),
              DataColumn(label: Text(loc.colBalance)),
              DataColumn(label: Text(loc.colCounterparty)),
              DataColumn(label: Text(loc.colDate)),
              DataColumn(label: Text(loc.colComment)),
            ],
            rows: List.generate(
              transactions.length,
              (index) {
                final tx = transactions[index];
                final color = WidgetStateProperty.resolveWith<Color?>(
                  (states) => index.isEven
                      ? theme.colorScheme.surfaceContainerHighest
                      : null,
                );

                return DataRow.byIndex(
                  index: index,
                  color: color,
                  cells: [
                    DataCell(OperationStatusBadge(status: tx.status)),
                    DataCell(TypeChip(type: tx.type)),
                    DataCell(Text(
                        '${tx.type.sign}${amountToString(tx.amount)} ${currencyCodeToSymbol(tx.currency)}')),
                    DataCell(Text(
                      tx.balanceAfter == null
                          ? '-'
                          : '${amountToString(tx.balanceAfter!)} ${currencyCodeToSymbol(tx.currency)}',
                    )),
                    DataCell(Text(tx.counterparty ?? '-')),
                    DataCell(Text(
                      '${TimeOfDay.fromDateTime(tx.date).format(context)}\n'
                      '${tx.date.toLocal().toIso8601String().split("T").first}',
                    )),
                    DataCell(Text(tx.description)),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
