import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_rider/constant/colors.dart';
import 'package:siro_rider/constant/style.dart';

import '../../controller/notification/passenger_notification_controller.dart';
import '../widgets/my_scafold.dart';
import '../widgets/mycircular.dart';

class NotificationPage extends StatelessWidget {
  const NotificationPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(PassengerNotificationController());
    return MyScafolld(
      isleading: true,
      title: 'Notifications'.tr,
      body: [
        GetBuilder<PassengerNotificationController>(
          builder: (notificationCaptainController) => notificationCaptainController
                  .isloading
              ? const MyCircularProgressIndicator() // iOS-style loading indicator
              : SafeArea(
                  child: ListView.builder(
                    itemCount: notificationCaptainController
                        .notificationData['message'].length,
                    itemBuilder: (BuildContext context, int index) {
                      if (notificationCaptainController
                              .notificationData['message'] ==
                          "No notification data found") {
                        Get.defaultDialog(
                          title: 'No Notifications'.tr,
                          content: Text(
                            'No notification data found.'.tr,
                          ),
                        );
                      }
                      var res = notificationCaptainController
                          .notificationData['message'][index];
                      return Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 8, vertical: 4),
                        child: CupertinoListTile(
                          backgroundColor: res['isShown'] == 'true'
                              ? AppColor.secondaryColor.withOpacity(.2)
                              : AppColor.secondaryColor.withOpacity(.8),
                          leading: res['isShown'] == 'true'
                              ? const Icon(CupertinoIcons.bell_slash_fill)
                              : const Icon(CupertinoIcons.bell_fill),
                          title: Text(
                            res['title'],
                            style: AppStyle.title.copyWith(
                              color: CupertinoColors.black,
                            ),
                          ),
                          subtitle: Text(
                            res['body'],
                            style: AppStyle.subtitle.copyWith(
                              color: CupertinoColors.systemGrey,
                            ),
                          ),
                          onTap: () {
                            showCupertinoDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return CupertinoAlertDialog(
                                  title: Text(
                                    res['title'],
                                    style: AppStyle.title,
                                  ),
                                  content: Text(
                                    res['body'],
                                    style: AppStyle.subtitle,
                                  ),
                                  actions: [
                                    CupertinoDialogAction(
                                      child: const Text('Ok'),
                                      onPressed: () {
                                        notificationCaptainController
                                            .updateNotification(
                                                res['id'].toString());
                                        Get.back();
                                      },
                                    ),
                                  ],
                                );
                              },
                            );
                          },
                        ),
                      );
                    },
                  ),
                ),
        )
      ],
    );
  }
}
