import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/constants/assets.dart';
import 'package:tm_app/core/routes/app_routes.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/home_view_model.dart';
import 'package:tm_app/views/shared/desktop_navigation_widget.dart';

import '../strings/home_strings.dart';
import '../widgets.dart';

class DesktopHomePage extends StatelessWidget {
  const DesktopHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          const DesktopNavigationWidget(
            currentIndex: 0, // Home index
          ),
          Expanded(
            child: Consumer<HomeViewModel>(
              builder: (context, homeViewModel, child) {
                if (homeViewModel.isLoading) {
                  return const Center(
                    child: CircularProgressIndicator(
                      color: AppColors.secondary50,
                    ),
                  );
                }

                if (homeViewModel.errorMessage != null) {
                  return Center(child: Text(homeViewModel.errorMessage!));
                }

                if (homeViewModel.tenderApprovalsStateResponse != null &&
                    homeViewModel.data != null &&
                    homeViewModel.feedbackStats != null) {
                  return SafeArea(
                    child: SingleChildScrollView(
                      child: SizedBox(
                        width: 780,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            const SizedBox(height: 55),
                            // SizedBox(width: 780, child: NotificationCard()),
                            const SizedBox(height: 40.0),
                            _progressBarsRow(homeViewModel),
                            SizedBox(height: 32.0.h()),
                            Padding(
                              padding: EdgeInsets.symmetric(
                                horizontal: 15.0.w(),
                              ),
                              child: _firstTenderCardsRow(
                                context,
                                homeViewModel,
                              ),
                            ),
                            SizedBox(height: 32.0.h()),
                            _yourTenderText(homeViewModel),
                            SizedBox(height: 8.0.h()),
                            _bottomListView(homeViewModel),
                          ],
                        ),
                      ),
                    ),
                  );
                }
                return const Center(
                  child: CircularProgressIndicator(
                    color: AppColors.secondary50,
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _progressBarsRow(HomeViewModel homeViewModel) {
    return Row(
      // mainAxisAlignment: MainAxisAlignment.spaceBetween,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ProgressBarColumn(
          text: HomeStrings.partnership,
          value: homeViewModel.partnershipPercent,
          amount: homeViewModel.partnershipPercent.toString(),
          circularProgressIndicatorWidth: 176,
          circularProgressIndicatorHeight: 176,
          strokeWidth: 9,
        ),
        const SizedBox(width: 106),
        ProgressBarColumn(
          text: HomeStrings.selfApply,
          value: homeViewModel.selfApplyPercent,
          amount: homeViewModel.selfApplyPercent.toString(),
          circularProgressIndicatorWidth: 176,
          circularProgressIndicatorHeight: 176,
          strokeWidth: 9,
        ),
      ],
    );
  }

  Widget _firstTenderCardsRow(
    BuildContext context,
    HomeViewModel homeViewModel,
  ) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          child: TenderCard(
            backgroundColor: AppColors.primary20,
            iconPath: AssetsManager.arrows,
            title: HomeStrings.tenderSubmitted,
            amount:
                homeViewModel
                    .tenderApprovalsStateResponse!
                    .data!
                    .submittedTenders
                    .toString(),

            textColor: AppColors.mainBlue,
            enableTap: true,
            width: 178,
            height: 148,
            onTap: () {
              const YourTendersRouteData().push(context);
            },
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: TenderCard(
            backgroundColor: AppColors.primary10,
            iconPath: AssetsManager.approve,
            title: HomeStrings.approvedTenders,
            amount: '0',
            textColor: AppColors.jellyBean,
            enableTap: true,
            width: 178,
            height: 148,
            onTap: () {
              const YourTendersRouteData().push(context);
            },
          ),
        ),
        const SizedBox(width: 10),
        TenderCard(
          backgroundColor: AppColors.orange10,
          iconPath: AssetsManager.shield,
          title: HomeStrings.tenderValue,
          amount: '0',
          textColor: AppColors.orange,
          enableTap: true,
          width: 178,
          height: 148,
          onTap: () {},
        ),
        const SizedBox(width: 10),
        TenderCard(
          backgroundColor: AppColors.grey10,
          iconPath: AssetsManager.thumbLike,
          title: HomeStrings.likedTenders,
          amount: homeViewModel.feedbackStats?.totalLikes.toString() ?? '0',
          textColor: AppColors.grey50,
          enableTap: (homeViewModel.feedbackStats?.totalLikes ?? 0) != 0,
          width: 178,
          height: 148,
          onTap: () {
            const LikedTendersRouteData().push(context);
          },
        ),
      ],
    );
  }

  Widget _yourTenderText(HomeViewModel homeViewModel) {
    final isYourTenders =
        homeViewModel.data?.data?.tenders!.isNotEmpty ?? false;
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 740),
      child: Align(
        alignment: AlignmentDirectional.centerStart,
        child: Text(
          isYourTenders
              ? HomeStrings.yourTenders
              : HomeStrings.recommendedTenders,
          style: TextStyle(
            fontSize: 18.0.sp(),
            fontWeight: FontWeight.w700,
            color: AppColors.grey80,
          ),
        ),
      ),
    );
  }

  Widget _bottomListView(HomeViewModel homeViewModel) {
    return SizedBox(
      width: 740,
      height: 387.0,
      child: NotificationListener<ScrollNotification>(
        onNotification: (ScrollNotification scrollInfo) {
          if (scrollInfo.metrics.pixels >=
              scrollInfo.metrics.maxScrollExtent - 200) {
            if (!homeViewModel.isLoadingMore && homeViewModel.hasMore) {
              if (!homeViewModel.isRecommendedMode) {
                homeViewModel.getYourTenders();
              } else {
                homeViewModel.getHomeRecommendTenders();
              }
            }
          }
          return false;
        },
        child: ListView.builder(
          padding: const EdgeInsets.only(bottom: 20),
          itemCount:
              homeViewModel.tenders.length +
              (homeViewModel.isLoadingMore ? 1 : 0),
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            if (index < homeViewModel.tenders.length) {
              return TendersListItem(tender: homeViewModel.tenders[index]);
            } else {
              return const Center(
                child: Padding(
                  padding: EdgeInsets.all(16.0),
                  child: CircularProgressIndicator(
                    color: AppColors.secondary50,
                  ),
                ),
              );
            }
          },
        ),
      ),
    );
  }
}
