import 'dart:async';

import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:flutter/material.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:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

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

  @override
  State<MyDraftAdsView> createState() => _MyDraftAdsViewState();
}

class _MyDraftAdsViewState extends State<MyDraftAdsView> {
  @override
  void initState() {
    scheduleMicrotask(() async => context.read<AdVM>().getMyDraftAds());
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, AdVM adVM, Widget? child) {
      return Scaffold(
        appBar: CustomAppBar(title: LocaleKeys.myDraftAds.tr()),
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Column(
            children: [
              Expanded(
                child: RefreshIndicator(
                  onRefresh: () async {
                    await adVM.getMyDraftAds();
                  },
                  child: adVM.state == ViewState.busy
                      ? const Center(child: CircularProgressIndicator())
                      : AdsListWidget(
                          isAdsFragment: true,
                          isDraftAds: true,
                          shouldShowAdStatus: false,
                          adsList: adVM.myDraftAds,
                          hasMoreData: adVM.hasMoreDataForMyDraftsAds,
                          onFetchMoreAds: () async {
                            await adVM.fetchMoreDraftAds();
                          },
                        ),
                ),
              ),
              if (adVM.isLoadingMore) ...[
                const Center(
                    child: Padding(
                  padding: EdgeInsets.all(8.0),
                  child: CircularProgressIndicator(),
                ))
              ]
            ],
          ),
        ),
      );
    });
  }
}
