import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:farm_tpf/custom_model/action_form/ActionUIForm.dart';
import 'package:farm_tpf/custom_model/action_form/RequestActivity.dart';
import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:meta/meta.dart';

part 'action_ui_state.dart';

class ActionUiCubit extends Cubit<ActionUiState> {
  final Repository repository;
  ActionUiCubit({required this.repository}) : super(ActionUiInitial());

  Future<void> getActionUIForm({
    required bool isEdit,
    required int actionId,
    required String actionType,
    required int activityId,
  }) async {
    try {
      emit(ActionUiLoading());
      var actionDetail = RequestActivity();
      var actionUIForm = await repository.getActionUIForm(idAction: actionId);
      if (isEdit) {
        try {
          actionDetail = await repository.detailCommonAction(activityId: activityId, activityType: actionType);
          print(actionDetail);
        } catch (e) {
          print(e);
        }
      }
      emit(ActionUiSuccess(actionUIForm: actionUIForm, activityDetail: actionDetail));
    } on DioError catch (e) {
      emit(ActionUiFailure(errorString: AppException.handleError(e)));
    }
  }
}
