import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:krezus/pages/transactions/widgets/transaction_add_dialog.dart';
import 'package:krezus/repositories/transactions/models/transaction.dart';

class TransactionLine extends StatelessWidget {
  final Transaction transaction;
  final double subTotal;

  const TransactionLine({super.key, required this.transaction, required this.subTotal});

  List<Widget> _largeScreenLayout(BuildContext context) {
    return [
      SizedBox(
        width: 100,
        child: Text(
          DateFormat('dd/MM/yyyy', 'fr_FR').format(transaction.date),
          style: const TextStyle(
            fontWeight: FontWeight.w300,
            fontSize: 15
          )
        )
      ),
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              transaction.category,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)
            ),
            Text(
              transaction.description,
              style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 15)
            ),
          ],
        ),
      ),
      SizedBox(
        width: 100,
        child: Text(transaction.account),
      ),
      SizedBox(
        width: 120,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              NumberFormat(transaction.value > 0 ? '+#######.00 €' : '#######.00 €', 'fr_FR').format(transaction.value),
              style: TextStyle(
                color: transaction.value > 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.error
              )
            ),
            Text(
              NumberFormat('#######.00 €', 'fr_FR').format(subTotal),
              style: TextStyle(
                fontWeight: FontWeight.w300,
                fontSize: 15,
                color: subTotal > 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.error
              )
            ),
          ],
        ),
      )
    ];
  }

  List<Widget> _smallScreenLayout(BuildContext context) {
    return [
      SizedBox(
        width: 100,
        child: Text(
          DateFormat('dd/MM/yyyy', 'fr_FR').format(transaction.date),
          style: const TextStyle(
            fontWeight: FontWeight.w300,
            fontSize: 15
          )
        )
      ),
      Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              transaction.category,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)
            ),
            Text(
              transaction.description,
              style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 15)
            ),
            Text(
              transaction.account,
              style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 15)
            ),
          ],
        ),
      ),
      SizedBox(
        width: 120,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              NumberFormat(transaction.value > 0 ? '+#######.00 €' : '#######.00 €', 'fr_FR').format(transaction.value),
              style: TextStyle(
                color: transaction.value > 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.error
              )
            ),
            Text(
              NumberFormat('#######.00 €', 'fr_FR').format(subTotal),
              style: TextStyle(
                fontWeight: FontWeight.w300,
                fontSize: 15,
                color: subTotal > 0 ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.error
              )
            ),
          ],
        ),
      )
    ];
  }

  @override
  Widget build(BuildContext context) {
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 800;
    return InkWell(
      onTap: () => TransactionAddDialog.show(context, transaction),
      child: MergeSemantics(
        child: Container(
          padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
          margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: smallVerticalScreen ? _smallScreenLayout(context) : _largeScreenLayout(context), 
          )
        )
      )
    );
  }
}
