import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:sefer_driver/constant/style.dart';
import 'package:sefer_driver/views/widgets/elevated_btn.dart';
import 'package:sefer_driver/views/widgets/mydialoug.dart';

import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../main.dart';
import '../functions/crud.dart';

class NotificationCaptainController extends GetxController {
  bool isLoading = false;
  Map notificationData = {};

  getNotifications() async {
    isLoading = true;
    update();
    var res = await CRUD().get(
        link: AppLink.getNotificationCaptain,
        payload: {'driverID': box.read(BoxName.driverID)});
    if (res == "failure") {
      // MyDialog().getDialog('There is no notification yet'.tr, '', () {
      //   Get.back();
      //   Get.back();
      // });
      Get.dialog(
        CupertinoAlertDialog(
          title: Column(
            children: [
              const Icon(
                CupertinoIcons.bell_slash_fill,
                color: CupertinoColors.systemGrey,
                size: 40,
              ),
              const SizedBox(height: 12),
              Text(
                'There is no notification yet'.tr,
                style: const TextStyle(
                  fontSize: 17,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
          actions: [
            CupertinoDialogAction(
              onPressed: () {
                Get.back();
                Get.back();
              },
              child: Text('Back'.tr),
            ),
          ],
        ),
        barrierDismissible: true,
        transitionCurve: Curves.easeOutBack,
        transitionDuration: const Duration(milliseconds: 200),
      );
    }
    notificationData = jsonDecode(res);
    // sql.insertData(notificationData['message'], TableName.captainNotification);

    isLoading = false;
    update();
  }

  updateNotification(String id) async {
    await CRUD().post(
      link: AppLink.updateNotificationCaptain,
      payload: {'isShown': 'true', 'id': id},
    );
  }

  addNotificationCaptain(String driverId, title, body, isPin) async {
    await CRUD().post(link: AppLink.addNotificationCaptain, payload: {
      'driverID': driverId,
      'title': title,
      'body': body,
      'isPin': isPin
    });
  }

  @override
  void onInit() {
    getNotifications();
    super.onInit();
  }
}
