import 'dart:convert';

import 'package:get/get.dart';
import 'package:Intaleq/controller/firebase/firbase_messge.dart';

import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../main.dart';
import '../../views/widgets/mydialoug.dart';
import '../firebase/notification_service.dart';
import '../functions/crud.dart';

class PassengerNotificationController extends GetxController {
  bool isloading = false;
  Map notificationData = {};

  getNotifications() async {
    isloading = true;
    update();
    var res = await CRUD().get(
        link: AppLink.getNotificationPassenger,
        payload: {'passenger_id': box.read(BoxName.passengerID)});
    if (res == "failure") {
      MyDialog().getDialog('There is no notification yet'.tr, '', () {
        Get.back();
        Get.back();
      });
    } else {
      notificationData = jsonDecode(res);
      isloading = false;
      update();
    }

    // sql.insertData(notificationData['message'], TableName.captainNotification);
  }

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

  addNotificationToPassenger(String title, body) async {
    var res = CRUD().post(link: AppLink.addNotificationPassenger, payload: {
      'title': title,
      'body': body,
    });
    // Get.find<FirebaseMessagesController>().sendNotificationToPassengerToken(
    //   title,
    //   body,
    //   'token',
    //   [],
    //   'iphone_ringtone',
    // );
    await NotificationService.sendNotification(
      category: title,
      target: 'token'.toString(),
      title: title,
      body: body.tr,
      isTopic: false, // Important: this is a token
      tone: 'cancel',
      driverList: [],
    );
  }

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