import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:localstore/localstore.dart';
import 'package:quiosco_soler_2/quiosco/models/db_estante_model.dart';
import 'package:quiosco_soler_2/quiosco/widgets/file_name_up.dart';

class HorizontalListview extends StatefulWidget {
  final List<DbEstantes> files;
  final String nextPage;
  final String infoKey;
  final dynamic info;

  const HorizontalListview({
    Key? key,
    required this.files,
    required this.nextPage,
    required this.infoKey,
    required this.info
  }) : super(key: key);

  @override
  State<HorizontalListview> createState() => _HorizontalListviewState();
}

class _HorizontalListviewState extends State<HorizontalListview> {
  final _db = Localstore.instance;

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      separatorBuilder: (context, idnex) => const Divider(),
      itemCount: widget.files.length,
      scrollDirection: Axis.horizontal,
      itemBuilder: (context, index) => TextButton(
        style: TextButton.styleFrom(
          padding: EdgeInsets.zero,
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          minimumSize: const Size(0, 0),
        ),
        child: FileNameUp(
          name: widget.files[index].esta!,
          image: widget.files[index].idim!,
        ),
        onPressed: () async{
          final id = widget.infoKey.replaceAll('/quiosco/', '');
          final info = widget.info;

          info['ides'] = widget.files[index].ides;
          info['esta'] = widget.files[index].esta;

          _db.collection('quiosco').doc(id).set(info);

          await Navigator.pushNamed(context, 'secc');

          final infoAct = {
            'user':        info['user'],
            'accessToken': info['accessToken'],
            'dinum':       info['dinum'],
          };

          _db.collection('quiosco').doc(id).set(infoAct);
        },
        onLongPress: () => Fluttertoast.showToast(
          msg: '${widget.files[index].esta}',
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 2,
        ),
      ),
    );
  }
}

