import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_app/core/widgets/custom_error_screen.dart';
import 'package:niogu_app/core/enums/campaign_type.dart';
import 'package:niogu_app/core/widgets/custom_empty_screen.dart';
import 'package:niogu_app/features/online_store/domain/entities/online_store.dart';
import 'package:niogu_app/features/online_store/presentation/providers/online_store_provider.dart';
import 'package:niogu_app/core/widgets/custom_selectable_item.dart';
import 'package:sizer/sizer.dart';

class CategoryCampaignScreen extends ConsumerStatefulWidget {
  const CategoryCampaignScreen({super.key});

  @override
  ConsumerState<CategoryCampaignScreen> createState() =>
      _CategoryCampaignScreenState();
}

class _CategoryCampaignScreenState extends ConsumerState<CategoryCampaignScreen>
    with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    final filteredCategoryState = ref.watch(
      filteredCategoryCampaignStreamProvider,
    );

    final categoryEmptyState = ref.watch(categoryCampaignEmptyProvider);

    final selectedCampaignItem = ref.watch(selectedCampaignItemProvider);

    return filteredCategoryState.when(
      data: (categories) {
        switch (categoryEmptyState) {
          case CategoryCampaignEmpty.loading:
            return const SizedBox();
          case CategoryCampaignEmpty.empty_database:
            return CustomEmptyScreen(
              title: "Tidak Ada Kategori",
              body: "Kamu belum memiliki kategori",
            );
          case CategoryCampaignEmpty.empty_search_result:
            return const CustomEmptyScreen(body: "Kategori Tidak Ditemukan");
          case CategoryCampaignEmpty.has_data:
            return ListView.builder(
              padding: EdgeInsets.all(4.w),
              itemCount: categories.length,
              itemBuilder: (context, index) {
                final category = categories[index];

                return CustomSelectableItem(
                  id: category.id,
                  title: category.name,
                  subtitle: "Total ${category.productUseAmount} produk",
                  imagePath: category.imagePath,
                  isSelected: selectedCampaignItem?.id == category.id,
                  onTap: () {
                    ref
                        .read(selectedCampaignItemProvider.notifier)
                        .state = SelectedItemCampaign(
                      id: category.id,
                      name: category.name,
                      type: CampaignType.category,
                    );
                  },
                );
              },
            );
        }
      },
      error: (error, stackTrace) =>
          CustomErrorScreen(message: "Ups, terjadi kesalahan", onRefresh: () {}),
      loading: () => const SizedBox(),
    );
  }
}
