import 'package:get/get.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';
import 'package:kaer_with_panels/app/routes/app_pages.dart';
import 'swipe_config.dart';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';

class SwipeController extends GetxController {
  final Rxn<SwipeConfig> currentConfig = Rxn<SwipeConfig>();
  final RxInt currentPageIndex = 0.obs;
  final RxBool leftMenuOpen = false.obs;
  final RxBool rightMenuOpen = false.obs;
  void setConfig(SwipeConfig? config) {
    currentConfig.value = config;
  }

  void setPageIndex(int index) {
    currentPageIndex.value = index;
  }

  void closeMenus() {
    leftMenuOpen.value = false;
    rightMenuOpen.value = false;
  }

  void openLeftMenu() {
    KRLogUtil.kr_d('openLeftMenu', tag: 'SwipeController');
    leftMenuOpen.value = true;
    Get.toNamed(Routes.HI_MENU);
  }

  void openRightMenu() {
    KRLogUtil.kr_d('openRightMenu', tag: 'SwipeController');
    rightMenuOpen.value = true;
    Get.toNamed(Routes.KR_SETTING);
  }

  void triggerLeft() {
    final cfg = currentConfig.value;
    if (cfg == null || !cfg.enableLeft) return;
    KRLogUtil.kr_d('call11 onLeft$cfg', tag: 'SwipeController');

    if (cfg.onLeft != null) {
      KRLogUtil.kr_d('call onLeft', tag: 'SwipeController');
      cfg.onLeft!.call();
      return;
    }
    Get.back();
  }

  void triggerRight() {
    final cfg = currentConfig.value;
    if (cfg == null || !cfg.enableRight) return;
    if (cfg.onRight != null) {
      KRLogUtil.kr_d('call onRight', tag: 'SwipeController');
      cfg.onRight!.call();
      return;
    }
    openRightMenu();
  }
}
