import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/requests_models/request_model.dart';
import 'package:mc_common_app/utils/dialogs_and_bottomsheets.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/view_models/requests_view_model.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/views/requests/widget/request_item.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/common_widgets/categories_list.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

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

  @override
  State<ProviderAcceptedRequestsView> createState() => _ProviderAcceptedRequestsViewState();
}

class _ProviderAcceptedRequestsViewState extends State<ProviderAcceptedRequestsView> {
  @override
  void initState() {
    scheduleMicrotask(() async => context.read<RequestsVM>().applyFilterOnProvidersAcceptedRequests(requestsTypeEnum: RequestsTypeEnum.specialCarRequest));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(builder: (BuildContext context, RequestsVM requestsVM, Widget? child) {
      return Scaffold(
        appBar: CustomAppBar(title: LocaleKeys.acceptedRequests.tr()),
        body: Container(
          color: MyColors.backgroundColor,
          width: double.infinity,
          height: double.infinity,
          child: Column(
            children: [
              16.height,
              FiltersList(
                filterList: requestsVM.acceptedRequestsTypeFilterOptions,
                onFilterTapped: (index, selectedFilterId) {
                  requestsVM.applyFilterOnProvidersAcceptedRequests(requestsTypeEnum: selectedFilterId.toRequestTypeEnum());
                },
              ),
              8.height,
              Expanded(
                  child: RefreshIndicator(
                onRefresh: () async => await requestsVM.getProvidersAcceptedRequests(),
                child: requestsVM.state == ViewState.busy
                    ? const Center(child: CircularProgressIndicator())
                    : requestsVM.providersAcceptedRequestsList.isEmpty
                        ? Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              LocaleKeys.noRequeststoShow.tr().toText(fontSize: 16, color: MyColors.lightTextColor),
                              if (requestsVM.requestsFiltersCounter > 0) ...[
                                8.height,
                                InkWell(
                                  onTap: () async {
                                    requestsVM.clearRequestsFilters();
                                    await requestsVM.getRequestsBasedOnFilters();
                                  },
                                  child: LocaleKeys.clearFilters.tr().toText(
                                        fontSize: 14,
                                        isBold: true,
                                        color: MyColors.darkPrimaryColor,
                                      ),
                                ),
                              ],
                            ],
                          )
                        : ListView.separated(
                            itemBuilder: (context, index) {
                              return RequestItem(
                                request: requestsVM.providersAcceptedRequestsList[index],
                                onTap: () async {
                                  RequestModel request = requestsVM.myFilteredRequests[index];
                                  requestsVM.updateCurrentSelectedRequest(request);
                                  if (request.requestStatus == RequestStatusEnum.pending ||
                                      request.requestStatus == RequestStatusEnum.cancelled ||
                                      request.requestStatus == RequestStatusEnum.expired) {
                                    return;
                                  }
                                  if (AppState().currentAppType == AppType.provider) {
                                    RequestDetailPageArguments requestDetailPageArguments = RequestDetailPageArguments(requestIndex: index, requestModel: request);
                                    navigateWithName(context, AppRoutes.requestsDetailPage, arguments: requestDetailPageArguments);
                                  } else {
                                    navigateWithName(context, AppRoutes.offersListPage, arguments: request.id);
                                  }
                                },
                              );
                            },
                            separatorBuilder: (context, index) {
                              return 16.height;
                            },
                            itemCount: requestsVM.providersAcceptedRequestsList.length,
                            padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16, top: 8),
                          ),
              ))
            ],
          ),
        ),
      );
    });
  }
}
