import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class ActivityList extends ConsumerStatefulWidget {
  final List activity;
  final bool edit;

  const ActivityList({super.key, required this.activity, this.edit = false});

  @override
  ConsumerState<ActivityList> createState() => ActivityListState();
}

class ActivityListState extends ConsumerState<ActivityList> {
  late List<bool> values;

  @override
  void initState() {
    super.initState();
    values = List<bool>.generate(widget.activity.length, (_) => false);
  }

  @override
  Widget build(BuildContext context) {
    final theme = ref.watch(themePortProvider);

    const colors = [
      Colors.cyan,
      Colors.pinkAccent,
      Colors.deepOrangeAccent,
      Colors.red,
    ];

    const icons = {
      "wage": Icons.wallet,
      "goal": Icons.emoji_events_outlined,
      "lock": Icons.lock_outline,
      "reload": Icons.attach_money_outlined,
    };

    const titles = {
      "wage": "Entrega de paga",
      "goal": "¡Objetivo cumplido!",
      "lock": "Bloqueo de pago",
      "reload": "Recarga familiar",
    };

    return Column(
      spacing: 20,
      children: List<Widget>.generate(widget.activity.length, (int index) {
        final color = colors[index % colors.length];
        final type = widget.activity[index]["type"] as String;

        final logItem = Container(
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: theme.getColorFor(ThemeCode.backgroundPrimary),
            borderRadius: const BorderRadius.all(Radius.circular(20)),
            border: Border(left: BorderSide(color: color, width: 5)),
          ),
          child: Column(
            spacing: 15,
            children: [
              Row(
                children: [
                  Icon(icons[type], color: color),
                  const SizedBox(width: 8),
                  Text(
                    titles[type]!,
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                  const Spacer(),
                  const Text("14/01/2005"),
                ],
              ),
              const Align(
                alignment: Alignment.topLeft,
                child: Text("Ana ya tiene su paga de 5€ en el reloj"),
              ),
            ],
          ),
        );

        if (widget.edit) {
          return Row(
            children: [
              Checkbox(
                value: values[index],
                onChanged: (_) {
                  setState(() {
                    values[index] = !values[index];
                  });
                },
                activeColor: theme.getColorFor(ThemeCode.buttonPrimary),
                semanticLabel: "Eliminar",
              ),
              Expanded(child: logItem),
            ],
          );
        } else {
          return logItem;
        }
      }),
    );
  }
}
