import 'package:flutter/material.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/subtitle.dart';
import 'package:test_sa/models/visits/visit.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/pages/user/visits/visit_details.dart';
import 'package:test_sa/views/widgets/loaders/lazy_loading.dart';
import 'package:test_sa/views/widgets/loaders/no_item_found.dart';
import 'package:test_sa/views/widgets/visits/visit_item.dart';

class VisitsList extends StatefulWidget {
  final List<Visit> visits;
  final bool nextPage;
  final Future<void> Function() onLazyLoad;
  final Function(List<Visit>) onEditGroup;

  const VisitsList({Key key, this.visits, this.nextPage, this.onLazyLoad, this.onEditGroup}) : super(key: key);

  @override
  _VisitsListState createState() => _VisitsListState();
}

class _VisitsListState extends State<VisitsList> {
  final List<Visit> _selectedVisits = [];

  @override
  void initState() {
    _selectedVisits.clear();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.visits.isEmpty) {
      return NoItemFound(
        message: context.translation.noVisitsFound,
      );
    }
    return Stack(
      children: [
        LazyLoading(
          nextPage: widget.nextPage,
          onLazyLoad: widget.onLazyLoad,
          child: ListView.builder(
              //physics: BouncingScrollPhysics(),
              itemCount: widget.visits.length,
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              itemBuilder: (context, itemIndex) {
                Visit visit = widget.visits[itemIndex];
                bool isSelected = _selectedVisits.contains(visit);
                return VisitItem(
                  visit: visit,
                  isSelected: isSelected,
                  index: itemIndex,
                  activeSelectMod: _selectedVisits.isNotEmpty,
                  onPressed: (visit) {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (_) => VisitDetailsPage(
                              visit: visit,
                            )));
                  },
                  onSelect: (visit) {
                    if (isSelected) {
                      _selectedVisits.remove(visit);
                    } else {
                      _selectedVisits.add(visit);
                    }
                    setState(() {});
                  },
                  onLongPress: (visit) {
                    if (isSelected) {
                      _selectedVisits.remove(visit);
                    } else {
                      _selectedVisits.add(visit);
                    }
                    setState(() {});
                  },
                );
              }),
        ),
        Align(
          alignment: Alignment.bottomLeft,
          child: Padding(
            padding: EdgeInsets.all(8.0 * AppStyle.getScaleFactor(context)),
            child: AnimatedSwitcher(
              duration: const Duration(milliseconds: 400),
              child: Visibility(
                key: ValueKey(_selectedVisits.isNotEmpty),
                visible: _selectedVisits.isNotEmpty,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    FloatingActionButton(
                      heroTag: "cancel",
                      child: const Icon(Icons.cancel),
                      onPressed: () {
                        _selectedVisits.clear();
                        setState(() {});
                      },
                    ),
                    FloatingActionButton(
                      heroTag: "edit",
                      child: const Icon(Icons.edit),
                      onPressed: () {
                        if (!widget.visits.contains(_selectedVisits.first)) {
                          _selectedVisits.clear();
                          setState(() {});
                          return;
                        }
                        widget.onEditGroup(_selectedVisits);
                        //_selectedVisits.clear();
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        )
      ],
    );
  }
}
