import 'dart:convert';

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

import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../constant/style.dart';
import '../../main.dart';
import '../../views/widgets/elevated_btn.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") {
      Get.defaultDialog(
          title: 'There is no notification yet'.tr,
          titleStyle: AppStyle.title,
          middleText: '',
          confirm: MyElevatedButton(
              title: 'Back',
              onPressed: () {
                Get.back();
                Get.back();
              }));
    }
    notificationData = jsonDecode(res);
    // sql.insertData(notificationData['message'], TableName.captainNotification);

    isloading = false;
    update();
  }

  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,
    });
    // FirebaseMessagesController()
    //     .sendNotificationToPassengerToken(title, body, 'token', [], 'ding.wav');
    NotificationService.sendNotification(
      target: 'token'.toString(),
      title: title.tr,
      body: body,
      isTopic: false, // Important: this is a token
      tone: 'ding',
      driverList: [], category: title,
    );
  }

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