import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
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/generated/locale_keys.g.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) {
                    i == 0 ? m.logger.d(m.userChatHistory.length) : "";
                    return ChatBubble(
                      text: m.userChatHistory[i].contant.toString(),
                      isSeen: m.userChatHistory[i].isSeen == true ? true : false,
                      isCurrentUser: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id ? true : false,
                      isDelivered: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id && 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: LocaleKeys.typeheretoreply.tr(),
                      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: SizedBox(
                        width: 100,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            IconButton(
                              icon: const Icon(
                                Icons.attach_file_rounded,
                                size: 27,
                                color: MyColors.lightGreenColor,
                              ),
                              onPressed: () {},
                            ),
                            IconButton(
                              icon: SvgPicture.asset(
                                "assets/icons/chat/chat_send_icon.svg",
                                height: 26,
                                width: 35,
                              ),
                              onPressed: () {
                                m.sendChatMessage(m.message.text, userDetails["targetUser"].id, userDetails["targetUser"].userName);
                              },
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ));
        },
      ),
    );
  }
}
