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;
  bool edit = false;

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

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

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

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

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

    final colors = [
      Colors.cyan,
      Colors.pinkAccent,
      Colors.deepOrangeAccent,
      Colors.red,
    ];
    final icons = {
      "wage": Icons.wallet,
      "goal": Icons.emoji_events_outlined,
      "lock": Icons.lock_outline,
      "reload": Icons.attach_money_outlined,
    };
    final 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) {
        var logItem = Container(
          padding: EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: theme.getColorFor(ThemeCode.backgroundPrimary),
            borderRadius: BorderRadius.all(Radius.circular(20)),
            border: BoxBorder.fromLTRB(
              left: BorderSide(color: colors[index % colors.length], width: 5),
            ),
          ),
          child: Column(
            spacing: 15,
            children: [
              Row(
                children: [
                  Icon(
                    icons[widget.activity[index]["type"]],
                    color: colors[index % colors.length],
                  ),
                  Text(
                    titles[widget.activity[index]["type"]]!,
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                  Spacer(),
                  Text("14/01/2005"),
                ],
              ),
              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: (value) => {
                  setState(() {
                    values[index] = !values[index];
                  }),
                },
                activeColor: theme.getColorFor(ThemeCode.buttonPrimary),
                semanticLabel: "Eliminar",
              ),
              Expanded(child: logItem),
            ],
          );
        } else {
          return logItem;
        }
      }),
    );
  }
}
