import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:smart_hotel_app/views/home/inspection/cubit/inspection_state.dart';
import 'package:smart_hotel_app/views/home/inspection/widget/device_list_item.dart';

class DeviceList extends StatelessWidget {
  final PagingState<int, InspectionDevice> pagingState;
  final VoidCallback fetchNextPage;
  final void Function(InspectionDevice item, BuildContext context) onTap;

  const DeviceList({
    super.key,
    required this.pagingState,
    required this.fetchNextPage,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return SliverPadding(
      padding: EdgeInsets.symmetric(horizontal: 28.w),
      sliver: PagedSliverList<int, InspectionDevice>(
        state: pagingState,
        fetchNextPage: fetchNextPage,
        builderDelegate: PagedChildBuilderDelegate<InspectionDevice>(
          firstPageErrorIndicatorBuilder: (_) => Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '加载失败: ${pagingState.error}',
                  style: TextStyle(
                    fontSize: 28.sp,
                    color: const Color.fromRGBO(255, 100, 101, 1),
                  ),
                  textAlign: TextAlign.center,
                ),
                SizedBox(height: 24.h),
                ElevatedButton(
                  onPressed: fetchNextPage,
                  child:
                      Text('重新加载', style: TextStyle(fontSize: 28.sp)),
                ),
              ],
            ),
          ),
          noItemsFoundIndicatorBuilder: (_) => Padding(
            padding: EdgeInsets.only(top: 60.h),
            child: Text(
              '暂无设备数据',
              style: TextStyle(
                fontSize: 28.sp,
                color: const Color.fromRGBO(100, 116, 139, 1),
              ),
            ),
          ),
          newPageErrorIndicatorBuilder: (_) => Center(
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 20.h),
              child: Column(
                children: [
                  Text(
                    '加载失败: ${pagingState.error}',
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(255, 100, 101, 1),
                    ),
                  ),
                  SizedBox(height: 12.h),
                  TextButton(
                    onPressed: fetchNextPage,
                    child: Text('重试', style: TextStyle(fontSize: 24.sp)),
                  ),
                ],
              ),
            ),
          ),
          itemBuilder: (_, item, __) {
            return DeviceListItem(
              device: item,
              onTap: () => onTap(item, context),
            );
          },
        ),
      ),
    );
  }
}