import 'dart:async';

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/view_models/ad_view_model.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/views/advertisement/components/ads_list_widget.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/common_widgets/filters_list.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

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

  @override
  State<FavouriteAdsView> createState() => _FavouriteAdsViewState();
}

class _FavouriteAdsViewState extends State<FavouriteAdsView> {
  int selectedBrandId = 0;

  @override
  void initState() {
    scheduleMicrotask(() async {
      final adVM = context.read<AdVM>();
      await adVM.getFavouriteAds();
      // Load filter options if not already loaded
      if (adVM.exploreAdsFilterOptions.isEmpty) {
        await adVM.populateAdsFilterList();
      }
    });
    super.initState();
  }

  List<AdDetailsModel> getFilteredAds(AdVM adVM) {
    if (selectedBrandId == 0) {
      return adVM.favouriteAds;
    }
    return adVM.favouriteAds.where((ad) => ad.vehicle?.model?.vehicleBrandID == selectedBrandId).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, AdVM adVM, Widget? child) {
      return Scaffold(
        appBar: CustomAppBar(title: LocaleKeys.myFavouriteAds.tr()),
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Column(
            children: [
              if (adVM.exploreAdsFilterOptions.isNotEmpty) ...[
                16.height,
                FiltersList(
                  showImages: true,
                  filterList: adVM.exploreAdsFilterOptions,
                  onFilterTapped: (index, selectedFilterId) {
                    setState(() {
                      selectedBrandId = selectedFilterId;
                    });
                    // Update filter selection in the list
                    for (var filter in adVM.exploreAdsFilterOptions) {
                      filter.isSelected = false;
                    }
                    adVM.exploreAdsFilterOptions[index].isSelected = true;
                  },
                  needLeftPadding: false,
                ).paddingOnly(left: 21),
              ],
              16.height,
              Expanded(
                child: RefreshIndicator(
                  onRefresh: () async {
                    await adVM.getFavouriteAds();
                  },
                  child: adVM.state == ViewState.busy
                      ? const Center(child: CircularProgressIndicator())
                      : getFilteredAds(adVM).isEmpty
                          ? Center(
                              child: Text(
                                LocaleKeys.noFavouriteAds.tr(),
                                style: const TextStyle(fontSize: 16),
                              ),
                            )
                          : AdsListWidget(
                              isAdsFragment: false,
                              isDraftAds: false,
                              shouldShowAdStatus: false,
                              adsList: getFilteredAds(adVM),
                              hasMoreData: false,
                            ),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }
}
