import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/app_state.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/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/requests_models/provider_offers_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/chat_view_model.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

class GeneralChatListPage extends StatelessWidget {
  final OfferListPageArguments generalChatListPageArguments;

  const GeneralChatListPage({super.key, required this.generalChatListPageArguments});

  @override
  Widget build(BuildContext context) {
    final List<ServiceProvidersOffers> serviceProviderOffers = generalChatListPageArguments.serviceProviderOffers;
    return Scaffold(
      appBar: CustomAppBar(title: LocaleKeys.offers.tr()),
      body: serviceProviderOffers.isEmpty
          ? Center(child: LocaleKeys.noOffersShow.tr().toText(fontSize: 16, color: MyColors.lightTextColor))
          : ListView.separated(
              itemCount: serviceProviderOffers.length,
              padding: const EdgeInsets.all(16),
              itemBuilder: (context, index) {
                ServiceProvidersOffers offersModel = serviceProviderOffers[index];
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Utils.statusContainerChip(text: "Offer ${Utils.getNameByRequestOfferStatusEnum(offersModel.requestOfferStatusEnum!)}", chipColor: Utils.getChipColorByRequestOfferStatusEnum(offersModel.requestOfferStatusEnum!)),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        (offersModel.name ?? "").toText(fontSize: 16, isBold: true),
                        Center(
                          child: "${offersModel.offerCount}".toText(
                            color: Colors.white,
                            isBold: true,
                            fontSize: 10,
                          ),
                        ).toContainer(
                          backgroundColor: MyColors.redColor,
                          borderRadius: 100,
                          paddingAll: 1,
                          width: 22,
                          height: 22,
                        ),
                      ],
                    ),
                    8.height,
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            "${offersModel.companyName}".toText(color: MyColors.lightTextColor, fontSize: 14),
                            if (offersModel.createdOn != null && offersModel.createdOn!.isNotEmpty) ...[
                              " | ${DateTime.parse(offersModel.createdOn!).getTimeAgo()}".toText(color: MyColors.lightTextColor, fontSize: 14),
                            ],
                            // "  |  1 hour ago".toText(
                            //   color: MyColors.lightTextColor,
                            //   fontSize: 12,
                            // ),
                          ],
                        ),
                        const Icon(Icons.arrow_forward, color: MyColors.darkIconColor, size: 18),
                      ],
                    ),
                  ],
                ).onPress(() async {
                  ChatViewArgumentsForRequest chatViewArgumentsForRequest = ChatViewArgumentsForRequest(
                    chatTypeEnum: ChatTypeEnum.requestOffer,
                    receiverId: "${offersModel.providerUserId}",
                    senderId: AppState().getUser.data!.userInfo!.userId.toString(),
                    requestId: generalChatListPageArguments.requestId,
                    providerIndex: index,
                    requestIndex: -1, // This will be only send in case of provider
                  );

                  ChatViewArguments chatViewArguments = ChatViewArguments(chatTypeEnum: ChatTypeEnum.requestOffer, chatViewArgumentsForRequest: chatViewArgumentsForRequest);

                  final chatVM = context.read<ChatVM>();

                  await chatVM
                      .getRequestsChatMessagesForCustomer(
                        context: context,
                        providerId: offersModel.providerId ?? 0,
                        requestOfferId: 0,
                        requestId: generalChatListPageArguments.requestId ?? 0,
                        providerOfferIndex: index,
                      )
                      .whenComplete(
                        () => navigateWithName(context, AppRoutes.chatView, arguments: chatViewArguments),
                      );
                }).toContainer(isShadowEnabled: true);
              },
              separatorBuilder: (context, index) => 16.height,
            ),
    );
  }
}
