import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
import 'package:tp5/csv/data.dart';
import 'package:tp5/widgets/my_row.dart';

class RostersCrewFilter extends ConsumerStatefulWidget {
  const RostersCrewFilter(
      {super.key, required this.qualif, required this.crewFilter});
  final List<Qualif> qualif;
  final Map<String, List<String>> crewFilter;

  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _RostersCrewFilterState();
}

class _RostersCrewFilterState extends ConsumerState<RostersCrewFilter> {
  late Map<String, List<String>> _crewFilter;

  @override
  void initState() {
    _crewFilter = widget.crewFilter;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colleges =
        widget.qualif.fold(<String>{}, (t, e) => {...t, e.college ?? ""});
    final acs =
        widget.qualif.fold(<String>{}, (t, e) => {...t, e.ac ?? ""}).sorted();

    final bases =
        widget.qualif.fold(<String>{}, (t, e) => {...t, e.base ?? ""});
    return Container(
        padding: const EdgeInsets.all(20),
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Row(
            children: [
              const SizedBox(width: 90, child: Text("College: ")),
              Expanded(
                child: MyRow(
                  children: colleges
                      .map(
                        (e) => _check(
                            value: _crewFilter["college"]!.contains(e),
                            type: "college",
                            label: e),
                      )
                      .toList(),
                ),
              ),
            ],
          ),
          const Gap(15),
          Row(
            children: [
              const SizedBox(width: 90, child: Text("Aircrafts: ")),
              Expanded(
                child: MyRow(
                  children: acs
                      .map(
                        (e) => _check(
                            value: _crewFilter["ac"]!.contains(e),
                            type: "ac",
                            label: e),
                      )
                      .toList(),
                ),
              ),
            ],
          ),
          const Gap(15),
          Row(
            children: [
              const SizedBox(width: 90, child: Text("Bases: ")),
              Expanded(
                child: MyRow(
                  children: bases
                      .map(
                        (e) => _check(
                            value: _crewFilter["base"]!.contains(e),
                            type: "base",
                            label: e),
                      )
                      .toList(),
                ),
              ),
            ],
          ),
          const Gap(15),
          const SizedBox(height: 35),
          SizedBox(
            width: 360,
            child: ElevatedButton(
              onPressed: () async =>
                  Navigator.pop(context, _crewFilter), // Close the bottom sheet
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.green, // Set button color to green
                shape: RoundedRectangleBorder(
                  borderRadius:
                      BorderRadius.circular(15), // Add rounded corners
                ),
                padding:
                    const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
              ),
              child: const Text(
                'Crew filter',
                style: TextStyle(fontSize: 18, color: Colors.white),
              ),
            ),
          ),
        ]));
  }

  Widget _check({bool value = false, String type = "", String label = "--"}) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 2),
      decoration: BoxDecoration(
          border: Border.all(color: value ? Colors.yellow : Colors.black)),
      child: Column(
        children: [
          Text(
            label,
            style: TextStyle(
                fontWeight: (value) ? FontWeight.bold : FontWeight.normal),
          ),
          IconButton(
            icon: Icon(value ? Icons.check_box : Icons.square),
            onPressed: () {
              _crewFilter[type]!.remove(label);
              if (!value) _crewFilter[type]!.add(label);
              setState(() {});
            },
          ),
        ],
      ),
    );
  }
}
