import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/your_tenders_view_model.dart';
import 'package:tm_app/views/your_tenders/widgets/tender_card.dart';

import '../../../data/services/model/tender_approvals_response/tender_approvals_response.dart';

class TendersSubmitted extends StatefulWidget {
  const TendersSubmitted({
    required this.approvedTenders,
    required this.isDesktop,
    required this.isMobile,
    super.key,
  });

  final TenderApprovalsResponse approvedTenders;
  final bool isDesktop;
  final bool isMobile;

  @override
  State<TendersSubmitted> createState() => _TendersSubmittedState();
}

class _TendersSubmittedState extends State<TendersSubmitted> {
  late final ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController()..addListener(_onScroll);
  }

  void _onScroll() {
    if (!_scrollController.hasClients) {
      return;
    }
    final max = _scrollController.position.maxScrollExtent;
    final pos = _scrollController.position.pixels;
    const threshold = 200.0;
    if (max - pos <= threshold) {
      final vm = Provider.of<YourTendersViewModel>(context, listen: false);
      vm.loadMoreTenders();
    }
  }

  @override
  void dispose() {
    _scrollController.removeListener(_onScroll);
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<YourTendersViewModel>(
      builder: (context, viewModel, child) {
        if (widget.approvedTenders.data == null) {
          return const Center(child: Text('No submitted projects yet.'));
        }

        final tenders = widget.approvedTenders.data!.tenders ?? [];
        if (tenders.isEmpty) {
          return const Center(child: Text('No submitted projects yet.'));
        }

        final itemCount =
            tenders.length + (viewModel.isLoadingMoreTenders ? 1 : 0);

        return ListView.builder(
          controller: widget.isMobile ? _scrollController : null,
          padding: EdgeInsets.symmetric(
            // horizontal: 24.0.w(),
            vertical: 15.0.h(),
          ),
          itemCount: itemCount,
          itemBuilder: (context, index) {
            if (index < tenders.length) {
              final tender = tenders[index];
              return TenderCard(
                tender: tender.tender!,
                isDesktop: widget.isDesktop,
                status: tender.status,
                submissionMode: tender.submissionMode!,
              );
            } else {
              return Padding(
                padding: EdgeInsets.symmetric(vertical: 20.0.h()),
                child: Center(
                  child: SizedBox(
                    width: 24.0.w(),
                    height: 24.0.h(),
                    child: const CircularProgressIndicator(
                      color: AppColors.secondary50,
                      strokeWidth: 2,
                    ),
                  ),
                ),
              );
            }
          },
        );
      },
    );
  }
}
