import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/chat_room/chat_room.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/profile/direct_list/direct_list_state.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:provider/provider.dart';

class ChatRoomItem extends StatelessWidget {
  final ChatRoom chatRoom;
  final VoidCallback? onBackButtonPressed;

  const ChatRoomItem(
      {Key? key, required this.chatRoom, this.onBackButtonPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(
          Routes.direct,
          arguments: {
            'type': chatRoom.type,
          },
        ).then((value) => {onBackButtonPressed?.call()});

        final state = context.read<DirectListState>();
        int unreadCount = chatRoom.unread;
        chatRoom.unread = 0;
        state.unreadCount -= unreadCount;
        context.read<UserProvider>().unreadMessageCount -= unreadCount;
      },
      child: Container(
        color: Colors.transparent,
        child: Column(
          children: [
            Row(
              children: [
                SvgPicture.asset(
                    chatRoom.unread != 0
                        ? 'lib/assets/icons/sms-notification.svg'
                        : 'lib/assets/icons/sms.svg',
                    height: 24,
                    width: 24,
                    color: chatRoom.unread != 0
                        ? null
                        : const Color.fromARGB(255, 0, 126, 167)),
                const SizedBox(width: 6),
                Expanded(
                  child: DidvanText(
                    chatRoom.type,
                    style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                        color: const Color.fromARGB(255, 102, 102, 102)),
                  ),
                ),
                // if (chatRoom.unread != 0)
                //   DidvanBadge(text: chatRoom.unread.toString()),
              ],
            ),
            const SizedBox(
              height: 10,
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const SizedBox(width: 2),
                    Container(
                      decoration: BoxDecoration(
                          color: chatRoom.lastMessage.readed
                              ? chatRoom.lastMessage.writedByAdmin
                                  ? const Color.fromARGB(255, 230, 243, 250)
                                  : const Color.fromARGB(255, 235, 235, 235)
                              : const Color.fromARGB(255, 255, 200, 215),
                          borderRadius: BorderRadius.circular(8)),
                      child: Padding(
                        padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
                        child: Text(
                          chatRoom.lastMessage.readed
                              ? chatRoom.lastMessage.writedByAdmin
                                  ? 'پاسخ داده شده'
                                  : 'در انتظار پاسخ'
                              : 'خوانده نشده',
                        ),
                      ),
                    ),
                    // Icon(
                    //   chatRoom.lastMessage.readed
                    //       ? DidvanIcons.check_double_light
                    //       : DidvanIcons.check_light,
                    //   size: 16,
                    // ),
                    const SizedBox(width: 12),
                    Expanded(
                      child: Row(
                        children: [
                          Center(
                            child: Column(
                              children: [
                                const SizedBox(
                                  height: 7,
                                ),
                                DidvanText(
                                  DateTime.parse(chatRoom.updatedAt)
                                      .toPersianDateStr(),
                                  style: Theme.of(context).textTheme.bodySmall,
                                  color: Theme.of(context).colorScheme.caption,
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 5,
                ),
                if (chatRoom.lastMessage.text == null)
                  const Icon(
                    DidvanIcons.mic_light,
                    size: 18,
                  ),
                Padding(
                  padding: const EdgeInsets.only(right: 8),
                  child: Row(
                    children: [
                      Text(
                        chatRoom.lastMessage.writedByAdmin
                            ? 'پیام پشتیبانی: '
                            : 'پیام شما: ',
                        style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                              fontWeight: FontWeight.bold,
                              color: const Color.fromARGB(255, 0, 126, 167),
                            ),
                      ),
                      Expanded(
                        child: DidvanText(
                          chatRoom.lastMessage.text ?? 'پیام صوتی',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          textAlign: TextAlign.start,
                          color: const Color.fromARGB(255, 102, 102, 102),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            const DidvanDivider(),
          ],
        ),
      ),
    );
  }
}
