// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';

class PaginationButtons extends StatelessWidget {
  final int currentPage;
  final int totalPages;
  final Function(int) onPageChanged;
  Color primaryColor = Colors.blue;
  int customPage;

  PaginationButtons({
    required this.currentPage,
    required this.totalPages,
    required this.onPageChanged,
  }) : customPage = currentPage;

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    bool isBigScreen = size.width >= 900;
    primaryColor = Theme.of(context).primaryColor;
    List<int> pageNumbers = [];
    if (totalPages > 0 && currentPage <= 2) pageNumbers.add(1);
    if (currentPage > 2) pageNumbers.add(currentPage - 1);
    if (currentPage > 1) pageNumbers.add(currentPage);
    if (currentPage < totalPages) pageNumbers.add(currentPage + 1);
    if (isBigScreen && currentPage < totalPages - 1) pageNumbers.add(totalPages);

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              IconButton(
                icon: const Icon(Icons.arrow_back),
                onPressed: currentPage > 1
                    ? () => onPageChanged(currentPage - 1)
                    : null,
              ),
              ...pageNumbers.map((page) {
                return _buildPaginationButton(page);
              }),
              IconButton(
                icon: const Icon(Icons.arrow_forward),
                onPressed: currentPage < totalPages
                    ? () => onPageChanged(currentPage + 1)
                    : null,
              ),
              const VerticalDivider(
                color: Colors.black,
                thickness: 1.0,
              ),
              if(isBigScreen)
                Column(
                  children: [
                    const SizedBox(height: 100),
                    Container(
                      color: Colors.transparent,
                      width: 150,
                      height: 150,
                      child: TextField(
                        decoration: InputDecoration(
                          labelText: 'Página',
                          hintText: 'número de página',
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(
                                8.0), // Ajusta el radio según tu preferencia
                          ),
                          contentPadding: EdgeInsets.symmetric(
                              vertical: 12.0, horizontal: 16.0),
                        ),
                        keyboardType: TextInputType.number,
                        onChanged: onCustomPageChanged,
                      )
                    ),
                  ]
                ),
              if(isBigScreen)
                Padding(
                  padding: EdgeInsets.only(left: 4),
                  child: ElevatedButton(
                    onPressed: () => onPageChanged(customPage),
                    child: Text('Ir'),
                  ),
                )
            ]),
        const SizedBox(height: 200),
      ]
    );
  }

  Widget _buildPaginationButton(int page) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        width: 60,
        height: 60,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.0),
          color: currentPage == page ? primaryColor : Colors.grey,
        ),
        child: TextButton(
          onPressed: () {
            onPageChanged(page);
          },
          child: Text(
            page.toString(),
            style: const TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  void onCustomPageChanged(String value) {
    // Validar la entrada del usuario y actualizar el estado
    if (value.isNotEmpty) {
      customPage = int.parse(value);
    } else {
      customPage = currentPage;
    }
  }
}
