import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/widgets/custom_error_screen.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/core/enums/campaign_type.dart';
import 'package:niogu_app/core/utils/extension_format.dart';
import 'package:niogu_app/core/enums/stock_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 ProductCampaignScreen extends ConsumerStatefulWidget {
  const ProductCampaignScreen({super.key});

  @override
  ConsumerState<ProductCampaignScreen> createState() =>
      _ProductCampaignScreenState();
}

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

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

    final filteredProductState = ref.watch(
      filteredProductCampaignStreamProvider,
    );

    final productEmptyState = ref.watch(productCampaignEmptyProvider);

    final selectedCampaignItem = ref.watch(selectedCampaignItemProvider);

    return filteredProductState.when(
      data: (products) {
        switch (productEmptyState) {
          case ProductCampaignEmpty.loading:
            return const SizedBox();
          case ProductCampaignEmpty.empty_database:
            return CustomEmptyScreen(
              title: "Tidak Ada Produk",
              body: "Kamu belum memiliki produk",
              textButton: "Buat Produk Pertamamu",
              onPressed: () => context.pushNamed(AppRoute.addProductScreen),
            );
          case ProductCampaignEmpty.empty_search_result:
            return const CustomEmptyScreen(body: "Produk Tidak Ditemukan");
          case ProductCampaignEmpty.has_data:
            return ListView.builder(
              padding: EdgeInsets.all(4.w),
              itemCount: products.length,
              itemBuilder: (context, index) {
                final product = products[index];

                final stockType = product.stockType;

                final stock = stockType == StockType.unlimited
                    ? "Stok Selalu Ada"
                    : "Sisa Stok : ${product.remainingStock.toStringWithoutTrailingZero()}";

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