import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/view_models/your_tenders_view_model.dart';
import 'package:tm_app/views/shared/tender_app_bar.dart';
import 'package:tm_app/views/your_tenders/strings/your_tenders_strings.dart';
import 'package:tm_app/views/your_tenders/widgets/filter_button.dart';
import 'package:tm_app/views/your_tenders/widgets/liked_disliked_tenders_list.dart';
import 'package:tm_app/views/your_tenders/widgets/tenders_submitted.dart';

import '../../../core/constants/tender_feedback.dart';

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

  @override
  State<YourTendersMobilePage> createState() => _YourTendersMobilePageState();
}

class _YourTendersMobilePageState extends State<YourTendersMobilePage> {
  late final YourTendersViewModel viewModel;
  @override
  void initState() {
    viewModel = context.read<YourTendersViewModel>();
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.clearFilter();
    });
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: appBar(context: context, title: YourTendersStrings.yourTenders),
      body: SafeArea(
        child: Column(
          children: [
            const FilterButton(platformType: PlatformType.mobile),
            Expanded(
              child: Consumer<YourTendersViewModel>(
                builder: (context, viewModel, child) {
                  if (viewModel.isLoading &&
                      (viewModel.data?.data?.tenders ?? []).isEmpty &&
                      (viewModel.likedTendersData?.data?.feedback ?? [])
                          .isEmpty) {
                    return const Center(
                      child: CircularProgressIndicator(
                        color: AppColors.secondary50,
                      ),
                    );
                  }
        
                  if (viewModel.errorMessage != null) {
                    return Center(child: Text(viewModel.errorMessage!));
                  }
        
                  return Column(
                    children: [
                      if (viewModel.selectedStatus ==
                              YourTendersStrings.likeTenders &&
                          viewModel.likedTendersData != null)
                        Expanded(
                          child: LikedDislikedTendersList(
                            data: viewModel.likedTendersData!,
                            status: TenderFeedback.liked.value,
                            isDesktop: false,
                            isMobile: true,
                          ),
                        )
                      else if (viewModel.selectedStatus ==
                              YourTendersStrings.dislikeTenders &&
                          viewModel.likedTendersData != null)
                        Expanded(
                          child: LikedDislikedTendersList(
                            data: viewModel.likedTendersData!,
                            status: TenderFeedback.disliked.value,
                            isDesktop: false,
                            isMobile: true,
                          ),
                        )
                      else if (viewModel.data != null)
                        Expanded(
                          child: TendersSubmitted(
                            approvedTenders: viewModel.data!,
                            isDesktop: false,
                            isMobile: true,
                          ),
                        )
                      else
                        const Expanded(
                          child: Center(
                            child: Text(YourTendersStrings.noTenders),
                          ),
                        ),
                    ],
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
