import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:get/get.dart';

import '../../../../data/api/app_exception.dart';
import '../../../../data/repository/repository.dart';
import '../../../../utils/utils.dart';
import '../../../custom_widgets/widget_utils.dart';
import '../models/task.dart';
import '../models/task_update_request.dart';

part 'task_detail_state.dart';

class TaskDetailCubit extends Cubit<TaskDetailState> {
  final repository = Repository();
  TaskDetailCubit() : super(TaskDetailInitial());

  Future<void> preparedData(int taskId) async {
    try {
      await Future.delayed(const Duration(seconds: 0));
      emit(TaskDetailLoading());
      var task = await repository.getTaskDetail(id: taskId);
      emit(TaskDetailSuccessful(task));
    } catch (e) {
      emit(TaskDetailFailure(AppException.handleError(e)));
    }
  }

  Future<void> updateStatusTask(
    RequestTaskUpdate task,
  ) async {
    print(task.toJson());
    UtilWidget.showLoading();
    await repository.updateTask(
      (success) {
        UtilWidget.hideDialog();
        Get.back(result: 'ok');
        // Utils.showSnackBarSuccess();
      },
      (errorMessage) {
        UtilWidget.hideDialog();
        Utils.showSnackBarError();
      },
      item: task,
    );
  }
}
