import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/modules/asset_inventory_module/provider/asset_inventory_provider.dart';
import 'package:test_sa/views/widgets/loaders/lazy_loading.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

import 'asset_detail_card_view.dart';

class AssetInventoryScanAssetView extends StatefulWidget {
  int sessionId;

  AssetInventoryScanAssetView({Key? key, required this.sessionId}) : super(key: key);

  @override
  State<AssetInventoryScanAssetView> createState() => _AssetInventoryScanAssetViewState();
}

class _AssetInventoryScanAssetViewState extends State<AssetInventoryScanAssetView> {
  late AssetInventoryProvider assetInventoryProvider;

  @override
  void initState() {
    assetInventoryProvider = Provider.of<AssetInventoryProvider>(context, listen: false);
    super.initState();
  }

  Future<void> getAssetList({bool loadMore = false}) async {
    await assetInventoryProvider.getAssetsInSession(
      sessionId: widget.sessionId,
      loadMore: loadMore,
    );
  }

  @override
  @override
  Widget build(BuildContext context) {
    return Consumer<AssetInventoryProvider>(
      builder: (context, provider, _) {
        if (provider.isLoading && provider.assetInventoryResponse == null) {
          return const Center(child: CircularProgressIndicator());
        }
        final assets = provider.assetInventoryResponse?.assetList ?? [];
        if (assets.isEmpty) {
          return const Center(child: NoDataFound());
        }
        return NotificationListener<ScrollNotification>(
          onNotification: (scrollInfo) {
            if (!provider.isNextPageLoading && provider.nextPage && scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {
              getAssetList(loadMore: true);
            }
            return false;
          },
          child: ListView.separated(
            padding: const EdgeInsets.all(16),
            itemCount: assets.length + (provider.isNextPageLoading ? 1 : 0),
            itemBuilder: (context, index) {
              if (index == assets.length) {
                return const Padding(
                  padding: EdgeInsets.symmetric(vertical: 16),
                  child: Center(child: CircularProgressIndicator()),
                );
              }
              return AssetDetailCardView(
                assetInventoryModel: assets[index],
                canDelete: (provider.modelPermission?.canEdit ?? false),
                onDeletePress: () async {
                  await provider.deleteAssetISession(id: assets[index].id ?? 0).then((success) async {
                    if (success) {
                      await provider.getAssetsInSession(sessionId: widget.sessionId);
                    }
                  });
                },
              );
            },
            separatorBuilder: (context, index) => 12.height,
          ),
        );
      },
    );
  }

// Widget build(BuildContext context) {
//   return Consumer<AssetInventoryProvider>(
//     builder: (context, provider, _) {
//       if (provider.isLoading && provider.assetInventoryResponse == null) {
//         //TODO need use existing loader if found..
//         return const Center(child: CircularProgressIndicator());
//       }
//       final assets = provider.assetInventoryResponse?.assetList ?? [];
//       if (assets.isEmpty) {
//         return const Center(child: NoDataFound());
//       }
//       return LazyLoading(
//         nextPage: provider.nextPage,
//         onLazyLoad: () async {
//           log('Loading next page...');
//           await getAssetList(loadMore: true);
//         },
//         child: ListView.separated(
//           padding: const EdgeInsets.all(16),
//           itemBuilder: (context, index) {
//             if (index == assets.length) {
//               // bottom loader
//               return const Padding(
//                 padding: EdgeInsets.symmetric(vertical: 16),
//                 child: Center(child: CircularProgressIndicator()),
//               );
//             }
//             return AssetDetailCardView(
//               assetInventoryModel: assets[index],
//               onDeletePress: () async {
//                 await provider.deleteAssetISession(id: assets[index].id ?? 0).then((success) async {
//                   if (success) {
//                     await provider.getAssetsInSession(sessionId: widget.sessionId);
//                   }
//                 });
//               },
//             );
//           },
//           separatorBuilder: (context, index) => 12.height,
//           itemCount: assets.length + (provider.nextPage ? 1 : 0),
//         ),
//       );
//     },
//   );
// }
}
