import 'package:flutter/services.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/services/logger_service.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:restart_app/restart_app.dart';

abstract class NativeMethodChannelService {
  void reopenApp();

  Future<void> restartApp();
}

class NativeMethodChannelServiceImp implements NativeMethodChannelService {
  MethodChannel platform;
  LoggerService loggerService;

  NativeMethodChannelServiceImp({required this.platform, required this.loggerService});

  @override
  void reopenApp() async {
    try {
      await platform.invokeMethod(AppStrings.openAppNativeFunctionName);
    } catch (e) {
      loggerService.logError("Error launching app: $e");
      loggerService.logToFile(message: "Error launching app: $e", source: "reopenApp -> native_method_handler.dart ", type: LogTypeEnum.error);
    }
  }

  @override
  Future<void> restartApp() async {
    try {
      await Restart.restartApp();
    } catch (e) {
      loggerService.logError("Error restarting App : $e");
      loggerService.logToFile(message: "Error restarting app: $e", source: "restartApp -> native_method_handler.dart ", type: LogTypeEnum.error);
    }
  }
}
