import 'package:community_material_icon/community_material_icon.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:prosappco/src/authentication/authentication_repository.dart';
import 'package:prosappco/src/components/photo_view.dart';
import 'package:prosappco/src/components/pop_appbar.dart';
import 'package:prosappco/src/models/chat_model.dart';
import 'package:prosappco/src/models/event_model.dart';
import 'package:prosappco/src/screens/chat.dart';

class MessagesUserScreen extends StatefulWidget {
  const MessagesUserScreen({super.key});

  @override
  State<MessagesUserScreen> createState() => _MessagesUserScreenState();
}

class _MessagesUserScreenState extends State<MessagesUserScreen> {
  final uid = AuthenticationRepository.instance.getCurrentUserUid();
  List<ChatModel> list = [];
  @override
  void initState() {
    super.initState();

    ChatModel.getChatsByUserId(uid!).then(
      (List<ChatModel> s) => setState(() {
        list = s;
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PopAppbar(
          onPressed: () {
            Navigator.pop(context);
          },
          label: 'Mensajes'),
      body: Column(
        children: [
          const Padding(
            padding: EdgeInsets.all(10),
            child: TextField(
              // controller: searchController,
              decoration: InputDecoration(
                hintText: 'Escriba un nombre',
                prefixIcon: Icon(CommunityMaterialIcons.stethoscope),
              ),
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: list.length,
              itemBuilder: (BuildContext context, int index) {
                if (list[index].messages.isNotEmpty) {
                  final pro = list[index].professional;
                  final lastMsg = list[index].messages.last;
                  return ListTile(
                    title: Text(
                      (pro?.name ?? ''),
                    ),
                    subtitle: Text(
                      '" ${lastMsg.content} "',
                      style: const TextStyle(fontStyle: FontStyle.italic),
                    ),
                    leading: ReferencePhoto(
                      ref: pro?.photo,
                      size: 55,
                      sizeCircle: 60,
                    ),
                    trailing: Column(
                      children: [
                        SizedBox(height: 8),
                        const Icon(
                          Icons.keyboard_arrow_right,
                          color: Colors.black,
                        ),
                        Text(
                          lastMsg.timestamp.day >= DateTime.now().day
                              ? DateFormat('h:mm a').format(lastMsg.timestamp)
                              : DateFormat('dd/MM/yyyy', 'es')
                                  .format(lastMsg.timestamp),
                          style:
                              const TextStyle(color: Colors.grey, fontSize: 12),
                        )
                      ],
                    ),
                    onTap: () {
                      Event.getEventById(list[index].id).then((value) {
                        Navigator.push(
                          context,
                          CupertinoPageRoute(
                            builder: (BuildContext context) {
                              return ChatScreen(eventoId: list[index].id);
                            },
                          ),
                        );
                      });
                    },
                  );
                } else {
                  return const SizedBox();
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}
