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

abstract class NativeMethodChannelService {
  void reopenApp();

  Future<void> restartApp();

  Future<void> restartDevice();

  Future<void> clearAllResources();

  Future<void> smartRestart({bool forceRestart = false, bool cleanupFirst = true});
}

class NativeMethodChannelServiceImp implements NativeMethodChannelService {
  static const MethodChannel _platform = MethodChannel('com.example.hmg_qline/foreground');
  LoggerService loggerService;

  NativeMethodChannelServiceImp({required this.loggerService});

  @override
  void reopenApp() async {
    try {
      loggerService.logInfo("Attempting to reopen app");
      await _platform.invokeMethod('reopenApp');
      loggerService.logInfo("App reopened successfully");
    } 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 {
      loggerService.logInfo("Initiating app restart");
      await _platform.invokeMethod('restartApp');
      loggerService.logInfo("App restart command sent successfully");
    } catch (e) {
      loggerService.logError("Error restarting app: $e");
      loggerService.logToFile(message: "Error restarting app: $e", source: "restartApp -> native_method_handler.dart", type: LogTypeEnum.error);
    }
  }

  @override
  Future<void> restartDevice() async {
    try {
      loggerService.logInfo("Attempting device restart (requires root)");

      final result = await _platform.invokeMethod('restartDevice');
      loggerService.logInfo("Device restart initiated: $result");
    } catch (e) {
      loggerService.logError("Device restart failed: $e");
      loggerService.logToFile(message: "Device restart failed: $e", source: "restartDevice -> native_method_handler.dart", type: LogTypeEnum.error);
    }
  }

  @override
  Future<void> clearAllResources() async {
    try {
      loggerService.logInfo("Clearing all native resources");

      final result = await _platform.invokeMethod('clearAllResources');
      loggerService.logInfo("All resources cleared: $result");
    } catch (e) {
      loggerService.logError("Error clearing resources: $e");
      loggerService.logToFile(message: "Error clearing resources: $e", source: "clearAllResources -> native_method_handler.dart", type: LogTypeEnum.error);
    }
  }

  // Enhanced restart method with multiple fallback options
  @override
  Future<void> smartRestart({bool forceRestart = false, bool cleanupFirst = true}) async {
    try {
      loggerService.logInfo("Starting smart restart - forceRestart: $forceRestart, cleanupFirst: $cleanupFirst");

      if (cleanupFirst) {
        await clearAllResources();
        await Future.delayed(const Duration(seconds: 1));
      }
      try {
        loggerService.logInfo("Initiating app restart");
        await _platform.invokeMethod('restartApp');
        loggerService.logInfo("App restart command sent successfully");
      } catch (e) {
        loggerService.logError("Error restarting app: $e");
        loggerService.logToFile(message: "Error restarting app: $e", source: "restartApp -> native_method_handler.dart", type: LogTypeEnum.error);
      }
    } catch (primaryError) {
      loggerService.logError("Primary restart failed, trying fallback methods: $primaryError");
    }
  }
}
