import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/notification/firebase_notification_manger.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/modules/cx_module/chat/chat_provider.dart';
import 'package:test_sa/modules/cx_module/chat/model/unread_message_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

class UnReadChatList extends StatelessWidget {
  UnReadChatList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: const DefaultAppBar(title: "Unread Messages"),
        body: FutureBuilder<List<UnReadMessage>>(
            future: Provider.of<ChatProvider>(context, listen: false).getUnReadMessages(context.userProvider.user!.employeeId ?? context.userProvider.user!.username!),
            builder: (BuildContext context, AsyncSnapshot<List<UnReadMessage>> snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) return CircularProgressIndicator(color: AppColor.loadingColor(context)).center;
              if (snapshot.data?.isEmpty ?? true) return const NoDataFound().center;

              List<UnReadMessage> messages = snapshot.data!;

              return SingleChildScrollView(
                padding: const EdgeInsets.all(16),
                child: ListView.separated(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  padding: const EdgeInsets.all(0),
                  itemCount: messages.length,
                  separatorBuilder: (context, itemIndex) => const Divider().defaultStyle(context),
                  itemBuilder: (context, index) {
                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              getMessage(messages[index]),
                              style: AppTextStyles.bodyText2.copyWith(
                                color: context.isDark ? AppColor.neutral30 : AppColor.neutral50,
                              ),
                            ).expanded,
                            8.width,
                            Text(
                              messages[index].createdAt?.unReadChatMsgDate ?? "",
                              textAlign: TextAlign.right,
                              style: AppTextStyles.tinyFont.copyWith(
                                color: context.isDark ? AppColor.neutral20 : AppColor.neutral50,
                              ),
                            ),
                          ],
                        ),
                        // Text(
                        //   messages[index].content ?? "",
                        //   style: AppTextStyles.bodyText2.copyWith(color: context.isDark ? AppColor.neutral10 : const Color(0xFF757575)),
                        // ),
                      ],
                    ).onPress(() {
                      FirebaseNotificationManger.handleMessage(context, {
                        "transactionType": "17",
                        "requestType": "chat",
                        "moduleId": messages[index].moduleCode,
                        "requestNumber": messages[index].referenceId,
                      });
                    });
                  },
                ).toShadowContainer(context),
              );
            }));
  }

  String getMessage(UnReadMessage unreadMsg) => "New Message from ${unreadMsg.senderUserName ?? ""} on ${getModuleName(unreadMsg.moduleCode ?? "0")} #${unreadMsg.referenceId ?? ""}";

  String getModuleName(String moduleCode) {
    switch (moduleCode) {
      case "1":
        return "CM";
      case "2":
        return "Gas Refill";
      case "3":
        return "Asset Transfer";
      case "6":
        return "Task";
      default:
        return "";
    }
  }
}
