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

class ListController extends GetxController {
  final filamentList = <FilamentModel>[].obs;
  final isLoadingFilament = false.obs;

  @override
  void onInit() {
    loadListFillament();
    super.onInit();
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  Future<void> loadListFillament() async {
    isLoadingFilament(true);
    if (filamentList.isNotEmpty) {
      filamentList.clear();
    }
    filamentList(FilamentRepository.to.getAllFilaments());
    isLoadingFilament(false);
    update();
  }

  void addNewFilament() async {
    await Get.toNamed(EditPage.namedRoute);
    loadListFillament();
  }

  void viewFilamentDetails(FilamentModel filament) {
    Get.toNamed(DetailsPage.namedRoute, arguments: {'filament': filament});
  }

  void editFilament(FilamentModel filament) async {
    await Get.toNamed(EditPage.namedRoute, arguments: {'filament': filament});
    loadListFillament();
  }

  void removeFilament(FilamentModel filament) {
    FilamentRepository.to.deleteFilament(filament.id);
    filamentList.removeWhere((item) => item.id == filament.id);
    filamentList.refresh();
    update();
  }
}
