import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:krezus/domains/budget/budget_bloc.dart';
import 'package:krezus/repositories/metadata/models/budget.dart';

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

  List<Widget> _computeBudgetsCompare(BuildContext context, List<Budget> targetBudgets, List<Budget> realBudgets) {
    List<Widget> list = [
      const Text('Budget'),
      Container(
        margin: const EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 2,
              color: Theme.of(context).colorScheme.onPrimaryContainer
            )
          )
        ),
      )
    ];

    list.addAll(targetBudgets.map((budget) => Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Container(width: 5),
            Text(budget.label),
          ],
        ),
        Text(
          '${NumberFormat("#00").format(budget.value.abs())}/${NumberFormat("#00 €").format(realBudgets.firstWhere((rbudget) => rbudget.label == budget.label).value.abs())}',
          style: const TextStyle(
            fontFamily: 'NovaMono',
          )
        )
      ],
    )));
    return list;
  }

  List<Widget> _computeOtherBudgets(BuildContext context, List<Budget> otherBudgets) {
    List<Widget> list = [
      const Text('Hors budget'),
      Container(
        margin: const EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 2,
              color: Theme.of(context).colorScheme.onPrimaryContainer
            )
          )
        ),
      )
    ];

    list.addAll(otherBudgets.map((budget) => Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Container(width: 5),
            Text(budget.label),
          ],
        ),
        Text(
          NumberFormat("#00 €").format(budget.value.abs()),
          style: const TextStyle(
            fontFamily: 'NovaMono',
          )
        )
      ],
    )));
    return list;
  }

  @override
  Widget build(BuildContext context) {
    bool smallVerticalScreen = MediaQuery.sizeOf(context).width < 800;
    return BlocBuilder<BudgetBloc, BudgetState>(
      builder: (context, state) => Column(
        children: [
          Container(
            width: smallVerticalScreen ? null : 300,
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5),
              color: Theme.of(context).colorScheme.secondaryContainer
            ),
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: _computeBudgetsCompare(context, state.budgets, state.compareBudgets),
              ),
            ) 
          ),
          Container(
            width: smallVerticalScreen ? null : 300,
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5),
              color: Theme.of(context).colorScheme.secondaryContainer
            ),
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: _computeOtherBudgets(context, state.otherBudgets),
              ),
            ) 
          ),
        ],
      ),
    );
  }
}