import 'package:flutter/material.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/app_empty.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/layout/app_screen.dart';
import 'package:frontend_eccp_mobile/modules/berkas/data/models/berkas_model.dart';
import 'package:frontend_eccp_mobile/modules/berkas/presentation/widgets/Berkas_card.dart';
import 'package:frontend_eccp_mobile/modules/berkas/presentation/widgets/Berkas_section_header.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class BerkasPage extends StatefulWidget {
  const BerkasPage({super.key});

  @override
  State<BerkasPage> createState() => _BerkasPageState();
}

class _BerkasPageState extends State<BerkasPage> {
  final RefreshController _refreshController = RefreshController();

  Future<void> _onRefresh() async {
    await Future<void>.delayed(const Duration(seconds: 1));
    _refreshController.refreshCompleted();
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AppScreen(
      title: 'Berkas',
      backgroundColor: AppColors.neutral50,
      scrollableBody: false,
      useShadowAppBar: false,
      isSliver: true,
      refreshController: _refreshController,
      enablePullDown: true,
      onRefresh: _onRefresh,
      body: const SizedBox.shrink(),
      slivers: [
        if (todayBerkas.isNotEmpty)
          _buildSection(
            title: 'Terbaru',
            items: todayBerkas,
          ),

        if (earlierBerkas.isNotEmpty)
          _buildSection(
            title: 'Lebih Lama',
            items: earlierBerkas,
          ),

        if (todayBerkas.isEmpty && earlierBerkas.isEmpty)
          SliverToBoxAdapter(
            child: SizedBox(
              height: MediaQuery.of(context).size.height * 0.7,
              child: Center(
                child: empty(),
              ),
            ),
          ),

        const SliverToBoxAdapter(child: AppSpacing.h24),
      ],
    );
  }

  SliverMainAxisGroup _buildSection({
    required String title,
    required List<BerkasModel> items,
  }) {
    return SliverMainAxisGroup(
      slivers: [
        SliverPersistentHeader(
          pinned: true,
          delegate: BerkasSectionHeaderDelegate(title: title),
        ),

        SliverPadding(
          padding: const EdgeInsets.symmetric(
            horizontal: AppSizes.s16,
          ),
          sliver: SliverToBoxAdapter(
            child: Center(
              child: ConstrainedBox(
                constraints: const BoxConstraints(maxWidth: 900),
                child: Column(
                  children: List.generate(items.length, (index) {
                    final item = items[index];

                    return Padding(
                      padding: AppPadding.t8,
                      child: BerkasCard(item: item),
                    );
                  }),
                ),
              ),
            ),
          ),
        ),

        const SliverToBoxAdapter(child: AppSpacing.h16),
      ],
    );
  }
}

final todayBerkas = <BerkasModel>[
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
  BerkasModel(
    title: 'Rosaldo',
    description: '2908388',
  ),
];

final earlierBerkas = <BerkasModel>[
  BerkasModel(
    title: 'Rosaldo 2',
    description: '2908389',
  ),
  BerkasModel(
    title: 'Rosaldo 3',
    description: '2928389',
  ),
  BerkasModel(
    title: 'Rosaldo 4',
    description: '2900389',
  ),
  BerkasModel(
    title: 'Rosaldo 5',
    description: '3908389',
  ),
  BerkasModel(
    title: 'Rosaldo 2',
    description: '2908389',
  ),
  BerkasModel(
    title: 'Rosaldo 3',
    description: '2928389',
  ),
  BerkasModel(
    title: 'Rosaldo 4',
    description: '2900389',
  ),
  BerkasModel(
    title: 'Rosaldo 5',
    description: '3908389',
  ),
  BerkasModel(
    title: 'Rosaldo 2',
    description: '2908389',
  ),
  BerkasModel(
    title: 'Rosaldo 3',
    description: '2928389',
  ),
  BerkasModel(
    title: 'Rosaldo 4',
    description: '2900389',
  ),
  BerkasModel(
    title: 'Rosaldo 5',
    description: '3908389',
  ),
];
