import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:home/src/presentation/wallet_item.dart';
import 'package:sf_shared/sf_shared.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:navigation/navigation.dart';

class HomeScreen extends ConsumerWidget {
  final String name = "Juan";
  final double total = 95.03;
  final List<Kid> kids = [
    Kid(name: "Carlos", balance: 25.47, savings: 8.32),
    Kid(name: "Ana", balance: 25.47, savings: 8.32),
  ];
  late final double available = double.parse(
    kids.fold(total, (t, e) => t - e.balance).toStringAsFixed(2),
  );
  late final double savings = double.parse(
    kids.fold(0.0, (t, e) => t + e.savings).toStringAsFixed(2),
  );

  HomeScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final theme = ref.watch(themePortProvider);
    final NavigationContract navigationContract = GetIt.I<NavigationContract>();

    return SafeArea(
      child: SingleChildScrollView(
        child: Container(
          color: theme.getColorFor(ThemeCode.backgroundSecondary),
          margin: EdgeInsets.all(30),
          child: Column(
            children: [
              Align(
                alignment: Alignment.topLeft,
                child: Text.rich(
                  TextSpan(
                    text: "Hola, ",
                    style: TextStyle(fontSize: 25),
                    children: <TextSpan>[
                      TextSpan(
                        text: name,
                        style: TextStyle(fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                ),
              ),
              walletsList(context, kids, ref),
              Align(
                alignment: Alignment.topLeft,
                child: TextButton(
                  onPressed: () => navigationContract.pushTo('/device_signup'),
                  child: Text(
                    "+ Añadir otro peque",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: theme.getColorFor(ThemeCode.textPrimary),
                    ),
                  ),
                ),
              ),
              WalletBalanceBlock(max: total, value: total - available, savings: savings),
              DepositBlock(max: 150 - total),
            ],
          ),
        ),
      ),
    );
  }

  Widget walletsList(BuildContext context, List<Kid> kids, WidgetRef ref) {

    return Column(
      spacing: 20,
      children: List<Widget>.generate(kids.length, (int index) {
        return WalletItem(context, kids[index], index);
      }),
    );
  }
}
