import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../configs/constants.dart';
import '../preference/data_preference.dart';
import '../resources/base_color.dart';
import '../shared/router_gage.dart';
import '../widgets/alert/custom_alert_dialog.dart';
import '../widgets/alert/data_alert_model.dart';

class AppNavigator {
  static final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
  static bool _authDialogShown = false;
  static BuildContext? get _ctx => key.currentContext;

  static Future<void> showAuthAlertAndGoLogin(String message) async {
    if (_authDialogShown || _ctx == null) return;
    _authDialogShown = true;
    final dataAlert = DataAlertModel(
      title: "Thông Báo",
      description: message.isNotEmpty ? message : 'Phiên đăng nhập đã hết hạn. Vui lòng đăng nhập lại.',
      localHeaderImage: "assets/images/ic_pipi_03.png",
      buttons: [
        AlertButton(
          text: "Đã hiểu",
          onPressed: () {
            final phone = DataPreference.instance.phoneNumberUsedForLoginScreen;
            if (phone.isNotEmpty) {
              Get.offAllNamed(loginScreen, arguments: {'phone': phone});
            } else {
              DataPreference.instance.clearData();
              Get.offAllNamed(onboardingScreen);
            }
            _authDialogShown = false;
          },
          bgColor: BaseColor.primary500,
          textColor: Colors.white,
        ),
      ],
    );
    Get.dialog(CustomAlertDialog(alertData: dataAlert, showCloseButton: false), barrierDismissible: false);
  }

  static Future<void> showAlert(String message) async {
    if (_authDialogShown || _ctx == null) return;
    _authDialogShown = true;
    final dataAlert = DataAlertModel(
      title: "Thông Báo",
      description: message.isNotEmpty ? message : ErrorCodes.serverErrorMessage,
      localHeaderImage: "assets/images/ic_pipi_03.png",
      buttons: [
        AlertButton(
          text: "Đã hiểu",
          onPressed: () {
            _authDialogShown = false;
          },
          bgColor: BaseColor.primary500,
          textColor: Colors.white,
        ),
      ],
    );
    Get.dialog(CustomAlertDialog(alertData: dataAlert, showCloseButton: false), barrierDismissible: false);
  }
}
