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/notification_controller.dart';
import '../../models/NotificationListResponse.dart';
import '../../widgets/comman_background.dart';
import '../../widgets/common_appbar.dart';
import '../../widgets/common_elevated_button.dart';

class NotificationScreen extends StatelessWidget {
  final NotificationController controller = Get.put(NotificationController());

  NotificationScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return CommonBackground(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: CommonAppBar(
          title: const Text('Notification',
              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(notificationList: controller.notificationsList)),
      ),
    );
  }
}

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<Notifications> notificationList;

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

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

    for (var notification in notificationList) {
      String date = DateFormat("dd MMM yyyy").format(DateTime.parse(notification.createdAt ?? ''));
      if (!groupedNotifications.containsKey(date)) {
        groupedNotifications[date] = [];
      }
      groupedNotifications[date]!.add(notification);
    }

    return ListView.builder(
      padding: const EdgeInsets.only(top: 15),
      itemCount: groupedNotifications.length,
      itemBuilder: (context, index) {
        String date = groupedNotifications.keys.elementAt(index);
        List<Notifications> notificationsForDate = groupedNotifications[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),
                ),
              ),
              ...notificationsForDate.map((item) => Padding(
                padding: const EdgeInsets.only(bottom: 10),
                child: ExpansionTile(
                  collapsedBackgroundColor: Colors.white,
                  backgroundColor: Colors.white,
                  trailing: const SizedBox.shrink(),
                  title: Text(
                    item.title ?? '',
                    style: const TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
                  ),
                  subtitle: Text(item.msg ?? ''),
                ),
              )),
            ],
          ),
        );
      },
    );
  }
}