import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/api/chat/chat_provider_model.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/ui/chat/chat_bubble.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart';
import 'package:provider/provider.dart';

class ChatDetailScreen extends StatelessWidget {
  dynamic userDetails;
  late ChatProviderModel data;

  ChatDetailScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    userDetails = ModalRoute.of(context)!.settings.arguments;
    data = Provider.of<ChatProviderModel>(context, listen: false);
    data.getSingleUserChatHistory(senderUID: AppState().chatDetails!.response!.id.toString(), receiverUID: userDetails["targetUser"].id, pagination: "0");
    Timer(const Duration(seconds: 1), () => data.scrollDown());
    return Scaffold(
      backgroundColor: const Color(0xFFF8F8F8),
      appBar: AppBarWidget(context, title: userDetails["targetUser"].userName, showHomeButton: false, image: userDetails["targetUser"].image),
      body: Consumer<ChatProviderModel>(
        builder: (BuildContext context, ChatProviderModel m, Widget? child) {
          return (m.isLoading
              ? ChatHomeShimmer()
              : Column(
                  children: <Widget>[
                    Expanded(
                      child: ListView.builder(
                        controller: m.scrollController,
                        shrinkWrap: true,
                        itemCount: m.userChatHistory.length,
                        padding: const EdgeInsets.symmetric(vertical: 10),
                        itemBuilder: (BuildContext context, int i) {
                          return ChatBubble(
                            text: m.userChatHistory[i].contant.toString(),
                            isSeen: m.userChatHistory[i].isSeen == true ? true : false,
                            isCurrentUser: m.userChatHistory[i].currentUserId == 42062 ? true : false,
                            isDelivered: m.userChatHistory[i].currentUserId == 42062 && m.userChatHistory[i].isDelivered == true ? true : false,
                            dateTime: m.userChatHistory[i].createdDate.toString(),
                          );
                        },
                      ),
                    ),
                    Card(
                      margin: EdgeInsets.zero,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 10),
                        child: TextField(
                          controller: m.message,
                          decoration: InputDecoration(
                            hintText: 'Type here to reply',
                            hintStyle: const TextStyle(color: MyColors.grey98Color),
                            border: InputBorder.none,
                            focusedBorder: InputBorder.none,
                            enabledBorder: InputBorder.none,
                            errorBorder: InputBorder.none,
                            disabledBorder: InputBorder.none,
                            contentPadding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                            suffixIcon: IconButton(
                              icon: SvgPicture.asset(
                                "assets/icons/chat/chat_send_icon.svg",
                                height: 26,
                                width: 35,
                              ),
                              onPressed: () {
                              //  m.logger.d(userDetails);
                               m.sendChatMessage(m.message.text, userDetails["targetUser"].id, userDetails["targetUser"].userName);
                              },
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ));
        },
      ),
    );
  }
}
