// ignore_for_file: use_build_context_synchronously

import 'dart:developer';

import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/view_models/chat_view_model.dart';
import 'package:flutter/material.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/models/requests_models/request_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/requests_view_model.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

class RequestItem extends StatelessWidget {
  final RequestModel request;
  final AppType appType;
  final int requestIndex;

  const RequestItem({super.key, required this.request, required this.appType, required this.requestIndex});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Utils.statusContainerChip(text: request.requestStatusName, chipColor: Utils.getChipColorByRequestStatus(request.requestStatus)),
                  6.height,
                  "${request.brand} ${request.model}".toText(fontSize: 16, isBold: true),
                  showItem("${LocaleKeys.model.tr()}:", "${request.year}"),
                  showItem("${LocaleKeys.customerName.tr()}:", request.customerName),
                ],
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                if (request.offerCount > 0 && appType == AppType.customer) ...[
                  Center(
                    child: "${request.offerCount}".toText(
                      color: Colors.white,
                      isBold: true,
                      fontSize: 12,
                    ),
                  ).toContainer(
                    backgroundColor: MyColors.redColor,
                    borderRadius: 100,
                    paddingAll: 1,
                    width: 25,
                    height: 25,
                  ),
                ],
                2.height,
                request.cityName.toText(
                  color: MyColors.lightTextColor,
                ),
                if (request.createdOn != null) ...[
                  DateTime.parse(request.createdOn!).getTimeAgo().toText(
                        color: MyColors.lightTextColor,
                      ),
                ],
              ],
            )
          ],
        ),
        showItem("${LocaleKeys.description.tr()}:", request.description),
        showItem("Price Range: ", ""),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                request.price.toInt().toString().toText(
                      fontSize: 20,
                      color: Colors.black,
                      isBold: true,
                    ),
                2.width,
                LocaleKeys.sar
                    .tr()
                    .toText(
                      color: MyColors.lightTextColor,
                      fontSize: 10,
                    )
                    .paddingOnly(
                      bottom: 3,
                    ),
              ],
            ),
            request.requestStatus == RequestStatus.submitted
                ? const Icon(
                    Icons.arrow_forward,
                    color: MyColors.darkIconColor,
                    size: 18,
                  )
                : const SizedBox()
          ],
        ),
      ],
    ).toContainer(isShadowEnabled: true).onPress(() async {
      RequestsVM requestsVM = context.read<RequestsVM>();
      requestsVM.updateCurrentSelectedRequest(request);
      if (request.requestStatus == RequestStatus.pending) {
        return;
      }
      if (appType == AppType.provider) {
        RequestDetailPageArguments requestDetailPageArguments = RequestDetailPageArguments(requestIndex: requestIndex, requestModel: request);
        navigateWithName(context, AppRoutes.requestsDetailPage, arguments: requestDetailPageArguments);
      } else {
        ChatVM chatVM = context.read<ChatVM>();
        await chatVM.getOffersFromProvidersByRequest(requestId: request.id, context: context);
        OfferListPageArguments offerListPageArguments = OfferListPageArguments(serviceProviderOffers: chatVM.serviceProviderOffersList, requestId: request.id);
        navigateWithName(context, AppRoutes.offersListPage, arguments: offerListPageArguments);
      }
    });
  }

  Widget showItem(String title, String value) {
    return Row(
      children: [
        title.toText(
          color: MyColors.lightTextColor,
        ),
        2.width,
        value.toText(
          isBold: true,
        ),
      ],
    );
  }
}
