import 'package:get/get.dart';
import '../model/filament_model.dart';
import '../helpers/filament_repository.dart';
import '../pages/edit_view.dart';

class DetailsController extends GetxController {
  Rx<FilamentModel?> filament = Rx<FilamentModel?>(null);

  @override
  void onInit() {
    if (Get.arguments != null && Get.arguments['filament'] != null) {
      filament.value = Get.arguments['filament'];
    }
    super.onInit();
  }

  void onEditPressed() {
    // Implement edit functionality here
    Get.toNamed(EditPage.namedRoute, arguments: {'filament': filament.value});
  }

  void deleteFilament() {
    if (filament.value != null) {
      FilamentRepository.to.deleteFilament(filament.value!.id);
      Get.back();
      Get.snackbar(
        'Gelöscht',
        'Filament wurde entfernt',
        snackPosition: SnackPosition.BOTTOM,
      );
    }
  }

  double get remainingWeight {
    if (filament.value == null) return 0;
    return filament.value!.weight - filament.value!.weightUsed;
  }

  double get usagePercentage {
    if (filament.value == null || filament.value!.weight == 0) return 0;
    return (filament.value!.weightUsed / filament.value!.weight) * 100;
  }

  @override
  void onClose() {}
}
