import 'dart:developer';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/advertisment_models/ad_details_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';

class AdsListWidget extends StatelessWidget {
  final List<AdDetailsModel> adsList;
  final ScrollPhysics? scrollPhysics;
  final bool isAdsFragment;
  final bool shouldShowAdStatus;
  final Function()? onFetchMoreAds;

  const AdsListWidget({
    super.key,
    required this.adsList,
    required this.shouldShowAdStatus,
    this.onFetchMoreAds,
    this.scrollPhysics,
    this.isAdsFragment = false,
  });

  @override
  Widget build(BuildContext context) {
    if (isAdsFragment && adsList.isEmpty) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          LocaleKeys.noAdsShow.tr().toText(fontSize: 16, color: MyColors.lightTextColor, fontWeight: MyFonts.Medium),
        ],
      );
    }
    return NotificationListener<ScrollNotification>(
      onNotification: (ScrollNotification scrollInfo) {
        if (scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent && context.read<AdVM>().hasMoreData) {
          if (onFetchMoreAds != null) {
            onFetchMoreAds!();
          }
        }
        return false;
      },
      child: ListView.separated(
        itemCount: adsList.length,
        shrinkWrap: true,
        physics: scrollPhysics,
        itemBuilder: (BuildContext context, int index) {
          return AdCard(
            adDetails: adsList[index],
            isAdsFragment: isAdsFragment,
            shouldShowAdStatus: shouldShowAdStatus,
          ).onPress(() => navigateWithName(context, AppRoutes.adsDetailView, arguments: adsList[index]));
        },
        separatorBuilder: (BuildContext context, int index) {
          return 12.height;
        },
        padding: const EdgeInsets.symmetric(horizontal: 21),
      ),
    );
  }
}

class AdCard extends StatelessWidget {
  final AdDetailsModel adDetails;
  final bool isAdsFragment;
  final bool shouldShowAdStatus;

  const AdCard({super.key, required this.adDetails, required this.isAdsFragment, required this.shouldShowAdStatus});

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Row(
          children: [
            adDetails.vehicle!.image!.first.imageUrl.buildNetworkImage(
              width: 80,
              height: 80,
              fit: BoxFit.cover,
            ),
            12.width,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            if (isAdsFragment && !context.read<AdVM>().isExploreAdsTapped) ...[
                              Utils.statusContainerChip(text: adDetails.statuslabel!, chipColor: Utils.getChipColorByAdStatus(adDetails.adPostStatus!)),
                            ],
                            (adDetails.vehicle!.vehicleTitle ?? "").toText(
                              fontSize: 16,
                              letterSpacing: -0.64,
                              height: 25 / 16,
                            ),
                            Row(
                              children: [
                                ("${LocaleKeys.model.tr()}:").toText(
                                  fontSize: 12,
                                  color: MyColors.lightTextColor,
                                  letterSpacing: -0.48,
                                  height: 18 / 12,
                                ),
                                2.width,
                                (adDetails.vehicle!.modelyear!.label ?? "").toText(
                                  fontSize: 12,
                                  letterSpacing: -0.48,
                                  height: 18 / 12,
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                ("${LocaleKeys.mileage.tr()}:").toText(
                                  color: MyColors.lightTextColor,
                                  fontSize: 12,
                                  letterSpacing: -0.48,
                                  height: 18 / 12,
                                ),
                                2.width,
                                (adDetails.vehicle!.mileage!.mileageEnd ?? "").toText(
                                  fontSize: 12,
                                  letterSpacing: -0.48,
                                  height: 18 / 12,
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          (adDetails.vehicle!.cityName ?? "").toText(color: MyColors.lightTextColor),
                          adDetails.createdOn != null ? DateTime.parse(adDetails.createdOn!).getTimeAgo().toText(color: MyColors.lightTextColor) : const SizedBox(),
                        ],
                      ),
                    ],
                  ),
                  8.height,
                  Row(
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            (adDetails.vehicle!.demandAmount!.toInt().toString()).toText(fontSize: 19, isBold: true, letterSpacing: -1.16, height: 29 / 19),
                            2.width,
                            LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, letterSpacing: -0.4, height: 16 / 10).paddingOnly(bottom: 3),
                          ],
                        ),
                      ),
                      SvgPicture.asset(
                        MyAssets.arrowRight,
                        height: 9.69,
                        width: 13,
                      ),

                      //   const Icon(Icons.arrow_forward)
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
        if (false)
          Container(
            height: 100,
            alignment: Alignment.center,
            color: MyColors.white.withOpacity(0.9),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                MyAssets.reservedConeIcon.buildSvg(),
                5.width,
                "Reserved".toText(
                  color: MyColors.darkPrimaryColor,
                  fontSize: 17,
                  isBold: true,
                )
              ],
            ),
          )
        else
          const SizedBox(),
      ],
    ).toWhiteContainer(
      width: double.infinity,
      allPading: 12,
    );
  }
}
