

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/modules/cm_module/cm_detail_provider.dart';
import 'package:test_sa/new_views/common_widgets/custom_badge.dart';

import 'chat_page.dart';
import 'chat_provider.dart';

class ChatWidget extends StatefulWidget {
  int moduleId;
  int requestId;
  String? assigneeEmployeeNumber;
  String? myLoginUserID;
  String? contactEmployeeINumber;
  bool isReadOnly;
  bool isShow;
  String title;

  ChatWidget(
      {Key? key,
      this.isReadOnly = false,
      this.title = "Chat",
      required this.moduleId,
      required this.requestId,
      this.assigneeEmployeeNumber,
      this.myLoginUserID,
      this.contactEmployeeINumber,
      this.isShow = false})
      : super(key: key);

  @override
  _ChatWidgetState createState() {
    return _ChatWidgetState();
  }
}

class _ChatWidgetState extends State<ChatWidget> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Provider.of<ChatProvider>(context, listen: false).reset();
      getChatToken();
    });
  }

  void getChatToken() {
    // ChatProvider cProvider = Provider.of<ChatProvider>(context, listen: false);
    // if (cProvider.chatLoginResponse != null && cProvider.referenceID == widget.requestId) return;
    // // String assigneeEmployeeNumber = Provider.of<ServiceRequestDetailProvider>(context, listen: false).currentWorkOrder?.data?.assignedEmployee?.employeeId ?? "";
    // String myEmployeeId = widget.myLoginUserID;
    //
    // String receiver = context.userProvider.isNurse ? widget.assigneeEmployeeNumber : widget.myEmployeeID;

    ChatProvider cProvider = Provider.of<ChatProvider>(context, listen: false);
    if (cProvider.chatLoginResponse != null && cProvider.referenceID == widget.requestId) return;
    String assigneeEmployeeNumber = widget.assigneeEmployeeNumber ?? Provider.of<CMDetailProvider>(context, listen: false).currentWorkOrder?.data?.assignedEmployee?.employeeId ?? "";
    String myEmployeeId = widget.myLoginUserID ?? context.userProvider.user!.username!;

    String receiver = context.userProvider.isNurse
        ? assigneeEmployeeNumber
        : (context.userProvider.isEngineer
            ? (widget.contactEmployeeINumber ?? Provider.of<CMDetailProvider>(context, listen: false).currentWorkOrder!.data!.workOrderContactPerson.first.employeeId!)
            : "");

    cProvider.getUserAutoLoginTokenSilent(widget.moduleId, widget.requestId, widget.title, myEmployeeId, receiver, isMounted: mounted);
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.isShow
        ? Consumer<ChatProvider>(builder: (pContext, chatProvider, _) {
            final int unreadCount = (!chatProvider.chatLoginTokenLoading && (chatProvider.chatParticipantModel?.unreadCount ?? 0) > 0) ? chatProvider.chatParticipantModel!.unreadCount! : 0;
            return CustomBadge2(
              value: unreadCount,
              top: 2,
              right: 2,
              minSize: 18,
              child: IconButton(
                icon: const Icon(Icons.chat_bubble),
                onPressed: () {
                  Navigator.push(
                      context,
                      CupertinoPageRoute(
                          builder: (context) => ChatPage(
                                moduleId: widget.moduleId,
                                requestId: widget.requestId,
                                title: widget.title,
                                readOnly: widget.isReadOnly,
                                assigneeEmployeeNumber: widget.assigneeEmployeeNumber,
                                contactEmployeeINumber: widget.contactEmployeeINumber,
                                myLoginUserID: widget.myLoginUserID,
                              )));
                },
              ).toShimmer(context: context, isShow: chatProvider.chatLoginTokenLoading, radius: 30, height: 30, width: 30),
            );
          })
        : const SizedBox();
  }
}
