import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/controller/notification/notification_captain_controller.dart';
import 'package:flutter/cupertino.dart';

class NotificationCaptain extends StatelessWidget {
  const NotificationCaptain({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(NotificationCaptainController());

    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Notifications'.tr),
        leading: CupertinoNavigationBarBackButton(
          onPressed: () => Get.back(),
        ),
      ),
      child: SafeArea(
        child: GetBuilder<NotificationCaptainController>(
          builder: (notificationCaptainController) =>
              notificationCaptainController.isLoading
                  ? const Center(child: CupertinoActivityIndicator())
                  : ListView.builder(
                      itemCount: notificationCaptainController
                          .notificationData['message'].length,
                      itemBuilder: (BuildContext context, int index) {
                        if (notificationCaptainController
                                .notificationData['message'] ==
                            "No notification data found") {
                          _showCupertinoDialog(context, 'No Notifications',
                              'There are no notifications at this time.');
                          return const SizedBox.shrink();
                        }
                        var res = notificationCaptainController
                            .notificationData['message'][index];
                        return CupertinoListTile(
                          leading: const Icon(CupertinoIcons.bell_fill),
                          title: Text(
                            res['title'],
                            style:
                                CupertinoTheme.of(context).textTheme.textStyle,
                          ),
                          subtitle: Text(
                            res['body'],
                            style: CupertinoTheme.of(context)
                                .textTheme
                                .tabLabelTextStyle,
                          ),
                          onTap: () {
                            _showCupertinoDialog(
                              context,
                              res['title'],
                              res['body'],
                              onConfirm: () {
                                notificationCaptainController
                                    .updateNotification(res['id'].toString());
                                Navigator.of(context).pop();
                              },
                            );
                          },
                        );
                      },
                    ),
        ),
      ),
    );
  }

  void _showCupertinoDialog(BuildContext context, String title, String content,
      {VoidCallback? onConfirm}) {
    showCupertinoDialog(
      context: context,
      builder: (BuildContext context) => CupertinoAlertDialog(
        title: Text(title),
        content: Text(content),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            child: const Text('OK'),
            onPressed: onConfirm ?? () => Navigator.of(context).pop(),
          ),
        ],
      ),
    );
  }
}
