import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:mc_common_app/classes/consts.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/advertisment_models/ad_details_model.dart';
import 'package:mc_common_app/models/chat_models/buyers_chat_for_ads_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/view_models/ad_view_model.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:easy_localization/easy_localization.dart';
import 'package:provider/provider.dart';
import 'package:sizer/sizer.dart';

class AdsBuyerChatsView extends StatelessWidget {
  final List<BuyersChatForAdsModel> buyersListViewArguments;

  const AdsBuyerChatsView({super.key, required this.buyersListViewArguments});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(title: LocaleKeys.chat.tr()),
      body: buyersListViewArguments.isEmpty
          ? Center(child: LocaleKeys.noOffersShow.tr().toText(fontSize: 16, color: MyColors.lightTextColor))
          : ListView.separated(
              itemCount: buyersListViewArguments.length,
              padding: const EdgeInsets.all(16),
              itemBuilder: (context, index) {
                BuyersChatForAdsModel chatForAdsModel = buyersListViewArguments[index];
                return Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: 34,
                          height: 34,
                          alignment: Alignment.center,
                          color: MyColors.darkTextColor,
                          child: ((chatForAdsModel.buyerName ?? "").getInitials()).toText(color: MyColors.white, fontSize: 15),
                        ).toCircle(borderRadius: 100),
                        SizedBox(width: 2.w),
                        Expanded(
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  (chatForAdsModel.buyerName ?? "").toText(fontSize: 16, isBold: true),
                                  if (chatForAdsModel.unReadMessagesCount != null && chatForAdsModel.unReadMessagesCount! > 0) ...[
                                    Center(
                                      child: "${chatForAdsModel.unReadMessagesCount}".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: [
                                  Flexible(child: ("${chatForAdsModel.lastMessage}").toText(color: MyColors.lightTextColor, fontSize: 12)),
                                  SizedBox(width: 5.w),
                                  if (chatForAdsModel.lastMessageDateTime != null) ...[
                                    DateTime.parse(chatForAdsModel.lastMessageDateTime!).getTimeAgo().toText(color: MyColors.lightTextColor),
                                  ],
                                ],
                              ),
                              2.height,
                              const Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Icon(
                                    Icons.arrow_forward,
                                    color: MyColors.darkIconColor,
                                    size: 18,
                                  ),
                                ],
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  ],
                ).onPress(() async {
                  ChatViewArgumentsForAd chatViewArgumentsForAd = ChatViewArgumentsForAd(receiverUserID: chatForAdsModel.buyerUserID, adsID: chatForAdsModel.adsID);

                  ChatViewArguments chatViewArguments = ChatViewArguments(chatTypeEnum: ChatTypeEnum.ads, chatViewArgumentsForAd: chatViewArgumentsForAd);

                  final chatVM = context.read<ChatVM>();
                  await chatVM
                      .getUsersChatMessagesForAd(context: context, isForBuyer: false, adsChatBuyerId: chatForAdsModel.id, senderName: chatForAdsModel.buyerName)
                      .whenComplete(() => navigateWithName(context, AppRoutes.chatView, arguments: chatViewArguments));
                }).toContainer(isShadowEnabled: true);
              },
              separatorBuilder: (context, index) => 16.height,
            ),
    );
  }
}
