import 'package:flutter/material.dart';
import 'package:test_sa/controllers/localization/localization.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> {
  List<Visit> _selectedVisits = [];
  @override
  void initState() {
    _selectedVisits.clear();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Subtitle _subtitle = AppLocalization.of(context).subtitle;
    if (widget.visits.length == 0) {
      return NoItemFound(
        message: _subtitle.noVisitsFound,
      );
    }
    return Stack(
      children: [
        LazyLoading(
          nextPage: widget.nextPage,
          onLazyLoad: widget.onLazyLoad,
          child: ListView.builder(
              //physics: BouncingScrollPhysics(),
              itemCount: widget.visits.length,
              padding: 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: Duration(milliseconds: 400),
              child: Visibility(
                key: ValueKey(_selectedVisits.isNotEmpty),
                visible: _selectedVisits.isNotEmpty,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    FloatingActionButton(
                      heroTag: "cancel",
                      child: Icon(Icons.cancel),
                      onPressed: () {
                        _selectedVisits.clear();
                        setState(() {});
                      },
                    ),
                    FloatingActionButton(
                      heroTag: "edit",
                      child: Icon(Icons.edit),
                      onPressed: () {
                        if (!widget.visits.contains(_selectedVisits.first)) {
                          _selectedVisits.clear();
                          setState(() {});
                          return;
                        }
                        widget.onEditGroup(_selectedVisits);
                        //_selectedVisits.clear();
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        )
      ],
    );
  }
}
