// ignore_for_file: use_build_context_synchronously

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/app_state.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/requests_models/request_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';

class RequestItem extends StatelessWidget {
  final RequestModel request;
  final bool shouldShowStatuses;
  final Function() onTap;

  const RequestItem({super.key, required this.request, this.shouldShowStatuses = true, required this.onTap});

  @override
  Widget build(BuildContext context) {
    String imageUrl = "";
    if (request.requestImages != null && request.requestImages!.isNotEmpty) {
      imageUrl = request.requestImages!.first.imageUrl ?? "";
    }
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            imageUrl.buildNetworkImage(
              width: 80,
              height: 80,
              fit: BoxFit.cover,
            ),
            12.width,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  if (shouldShowStatuses) ...[
                    Utils.statusContainerChip(
                        text: request.requestStatusName.toLowerCase() == "InProgress".toLowerCase() ? "In Progress" : request.requestStatusName,
                        chipColor: Utils.getChipColorByRequestStatus(request.requestStatus)),
                  ],
                  6.height,
                  "${request.brand} ${request.model} | ${request.id}".toText(fontSize: 16, letterSpacing: -0.64),
                  showItem("${LocaleKeys.year.tr()}:", "${request.year}"),
                  if (request.customerName.isNotEmpty) ...[
                    showItem("${LocaleKeys.customerName.tr()}:", request.customerName),
                  ],
                  showItem("${LocaleKeys.description.tr()}:", request.description),
                ],
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                if (request.offerCount > 0 && AppState().currentAppType == AppType.customer && request.requestStatus == RequestStatusEnum.submitted) ...[
                  Center(
                    child: "${request.offerCount}".toText(
                      color: Colors.white,
                      fontSize: 10,
                    ),
                  ).toContainer(
                    backgroundColor: MyColors.redColor,
                    borderRadius: 100,
                    paddingAll: 1,
                    width: 20,
                    height: 20,
                  ),
                ],
                2.height,
                request.cityName.toText(color: MyColors.lightTextColor, letterSpacing: -0.4, fontSize: 10),
                if (request.createdOn != null) ...[
                  DateTime.parse(request.createdOn!).getTimeAgo().toText(color: MyColors.lightTextColor, letterSpacing: -0.4, fontSize: 10),
                ],
              ],
            )
          ],
        ),
        // showItem("${LocaleKeys.priceRange.tr()}: ", ""),
        // Row(
        //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
        //   crossAxisAlignment: CrossAxisAlignment.end,
        //   children: [
        //     Row(
        //       crossAxisAlignment: CrossAxisAlignment.end,
        //       children: [
        //         request.price.toInt().toString().toText(fontSize: 19, color: MyColors.darkTextColor, isBold: true, letterSpacing: -1.16),
        //         2.width,
        //         LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, letterSpacing: -0.4).paddingOnly(bottom: 3),
        //       ],
        //     ),
        //     request.requestStatus == RequestStatusEnum.submitted
        //         ? SvgPicture.asset(
        //             MyAssets.arrowRight,
        //             height: 9.69,
        //             width: 13,
        //           )
        //         : const SizedBox()
        //   ],
        // ),
      ],
    ).toContainer(isShadowEnabled: true).onPress(() async {
      onTap();
    });
  }

  Widget showItem(String title, String value) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        title.toText(color: MyColors.lightTextColor, letterSpacing: -0.48),
        3.width,
        Flexible(child: value.toText(isBold: true, overflow: TextOverflow.ellipsis)),
      ],
    );
  }
}
