import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:hallo_doctor_doctor_app/app/modules/appointment/controllers/appointment_controller.dart';
import 'package:hallo_doctor_doctor_app/app/modules/order/controllers/order_controller.dart';
import 'package:hallo_doctor_doctor_app/app/services/local_notification_service.dart';

import '../../../services/notification_service.dart';
import '../../../services/user_service.dart';

class DashboardController extends GetxController {
  final _selectedIndex = 0.obs;
  get selectedIndex => _selectedIndex.value;
  set selectedIndex(index) => _selectedIndex.value = index;
  NotificationService notificationService = Get.find<NotificationService>();
  LocalNotificationService localNotificationService =
      Get.find<LocalNotificationService>();

  @override
  void onInit() async {
    super.onInit();
    try {
      EasyLoading.show();
      await UserService().getUserModel();
      await localNotificationService.initNotification();
      await localNotificationService.requestPermission();
      String? token = await localNotificationService.getNotificationToken();
      if (token != null) {
        await UserService().updateUserToken(token);
      }
      localNotificationService.listenNotification();
      EasyLoading.dismiss();
    } catch (e) {
      EasyLoading.dismiss();
      print('Error initializing notifications: $e');
    }
  }

  @override
  void onClose() {}

  void initTabAppointment() {
    Get.find<AppointmentController>().initDoctorSchedule();
  }

  void initTabOrder() {
    Get.find<OrderController>().initOrderedTimeSlot();
  }

  void activateTabAppointment() {
    initTabAppointment();
    updateTabSelection(1);
  }

  void activateTabOrder() {
    initTabOrder();
    updateTabSelection(2);
  }

  void updateTabSelection(int index) {
    selectedIndex = index;
  }
}
