import 'package:cheminova/widgets/my_drawer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import '../../controller/announcement_controller.dart';
import '../../models/announcement_response.dart';
import '../../widgets/comman_background.dart';
import '../../widgets/common_appbar.dart';
import '../../widgets/common_elevated_button.dart';

class AnnouncementScreen extends StatelessWidget {
  final AnnouncementController controller = Get.put(AnnouncementController());

  AnnouncementScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return CommonBackground(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: CommonAppBar(
          title: const Text('Announcements',
              style: TextStyle(
                  fontSize: 20,
                  color: Colors.black,
                  fontWeight: FontWeight.w400,
                  fontFamily: 'Anek')),
          backgroundColor: Colors.transparent,
          elevation: 0,
          actions: [
            IconButton(
                onPressed: () => Navigator.pop(context),
                icon: SvgPicture.asset('assets/svg/back_arrow.svg'),
                padding: const EdgeInsets.only(right: 20))
          ],
        ),
        drawer: const MyDrawer(),
        body: Obx(() => controller.isLoading.value
            ? const Center(child: CircularProgressIndicator())
            : MyListView(announcementList: controller.announcementsList)),
      ),
    );
  }
}

Widget buildProductButton(String productName) {
  return Padding(
    padding: const EdgeInsets.only(bottom: 15),
    child: CommonElevatedButton(
      borderRadius: 30,
      width: double.infinity,
      height: kToolbarHeight - 10,
      text: productName,
      backgroundColor: const Color(0xff004791),
      onPressed: () {
        debugPrint('$productName pressed');
      },
    ),
  );
}

class MyListView extends StatelessWidget {
  final List<AnnouncementResponse> announcementList;

  const MyListView({super.key, required this.announcementList});

  @override
  Widget build(BuildContext context) {
    Map<String, List<AnnouncementResponse>> groupedAnnouncements = {};

    for (var announcement in announcementList) {
      String date = DateFormat("dd MMM yyyy").format(announcement.createdAt ?? DateTime.now());
      if (!groupedAnnouncements.containsKey(date)) {
        groupedAnnouncements[date] = [];
      }
      groupedAnnouncements[date]!.add(announcement);
    }

    return ListView.builder(
      padding: const EdgeInsets.only(top: 15),
      itemCount: groupedAnnouncements.length,
      itemBuilder: (context, index) {
        String date = groupedAnnouncements.keys.elementAt(index);
        List<AnnouncementResponse> announcementsForDate = groupedAnnouncements[date]!;

        return Padding(
          padding: const EdgeInsets.only(bottom: 10, left: 10, right: 10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.only(bottom: 8.0),
                child: Text(
                  date,
                  style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
              ),
              ...announcementsForDate.map((item) => Padding(
                padding: const EdgeInsets.only(bottom: 10),
                child: ExpansionTile(
                  collapsedBackgroundColor: Colors.white,
                  backgroundColor: Colors.white,
                  trailing: const SizedBox.shrink(),
                  title: const Text(
                    'New Announcement',
                    style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
                  ),
                  subtitle: Text(item.message ?? ''),
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text('Sent to: ${item.sentTo?.join(", ")}'),
                          const SizedBox(height: 8),
                          Text('Unique ID: ${item.uniqueId}'),
                        ],
                      ),
                    ),
                  ],
                ),
              )),
            ],
          ),
        );
      },
    );
  }
}