import 'package:aitrainer_app/bloc/exercise_plan/exercise_plan_bloc.dart';
import 'package:aitrainer_app/model/workout_menu_tree.dart';
import 'package:aitrainer_app/repository/exercise_plan_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';

part 'exercise_plan_custom_add_event.dart';
part 'exercise_plan_custom_add_state.dart';

class ExercisePlanDetailChange {
  static const String add = "add";
  static const String delete = "delete";
  static const String update = "update";
  static const String deleted = "deleted";
}

class ExercisePlanCustomAddBloc extends Bloc<ExercisePlanCustomAddEvent, ExercisePlanCustomAddState> {
  final ExercisePlanRepository exercisePlanRepository;
  final ExercisePlanBloc planBloc;
  final WorkoutMenuTree workoutMenuTree;

  late double quantity;
  double? serie;
  late double quantityUnit;

  @override
  ExercisePlanCustomAddBloc({required this.exercisePlanRepository, required this.planBloc, required this.workoutMenuTree})
      : super(ExercisePlanCustomAddInitial()) {
    on<ExercisePlanCustomAddLoad>(_onLoad);
    on<ExercisePlanCustomAddChangeSerie>(_onChangeSerie);
    on<ExercisePlanCustomAddChangeQuantity>(_onChangeQuantity);
    on<ExercisePlanCustomAddChangeQuantityUnit>(_onChangeQuantityUnit);
    on<ExercisePlanCustomAddSubmit>(_onSubmit);
    on<ExercisePlanCustomAddRemove>(_onRemove);
  }

  void init() {
    exercisePlanRepository.setActualPlanDetailByExerciseType(workoutMenuTree.exerciseType!);
    quantity = exercisePlanRepository.getActualPlanDetail()!.repeats != null ? exercisePlanRepository.getActualPlanDetail()!.repeats!.toDouble() : 12;
    serie = exercisePlanRepository.getActualPlanDetail()!.serie != null ? exercisePlanRepository.getActualPlanDetail()!.serie!.toDouble() : 3;
    quantityUnit = exercisePlanRepository.getActualPlanDetail()!.weightEquation != null
        ? double.parse(exercisePlanRepository.getActualPlanDetail()!.weightEquation!)
        : 30;

    exercisePlanRepository.getActualPlanDetail()!.weightEquation = quantityUnit.toString();
    exercisePlanRepository.getActualPlanDetail()!.serie = serie!.toInt();
    exercisePlanRepository.getActualPlanDetail()!.repeats = quantity.toInt();
  }

  void _onLoad(ExercisePlanCustomAddLoad event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    init();
    emit(ExercisePlanCustomAddReady());
  }

  void _onChangeSerie(ExercisePlanCustomAddChangeSerie event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    serie = event.quantity;
    exercisePlanRepository.getActualPlanDetail()!.serie = event.quantity.toInt();
    emit(ExercisePlanCustomAddReady());
  }

  void _onChangeQuantity(ExercisePlanCustomAddChangeQuantity event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    quantity = event.quantity;
    exercisePlanRepository.getActualPlanDetail()!.repeats = event.quantity.toInt();
    emit(ExercisePlanCustomAddReady());
  }

  void _onChangeQuantityUnit(ExercisePlanCustomAddChangeQuantityUnit event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    quantityUnit = event.quantity;
    exercisePlanRepository.getActualPlanDetail()!.weightEquation = event.quantity.toStringAsFixed(0);
    emit(ExercisePlanCustomAddReady());
  }

  void _onSubmit(ExercisePlanCustomAddSubmit event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    if (exercisePlanRepository.exercisePlanDetails[exercisePlanRepository.getActualPlanDetail()] == null) {
      exercisePlanRepository.getActualPlanDetail()!.change = ExercisePlanDetailChange.add;
    } else {
      exercisePlanRepository.getActualPlanDetail()!.change = ExercisePlanDetailChange.update;
    }
    exercisePlanRepository.addDetailToPlan();
    planBloc.add(ExercisePlanAddExercise(exercisePlanDetail: exercisePlanRepository.getActualPlanDetail()!));
    emit(ExercisePlanCustomAddReady());
  }

  void _onRemove(ExercisePlanCustomAddRemove event, Emitter<ExercisePlanCustomAddState> emit) {
    emit(ExercisePlanCustomAddLoading());
    exercisePlanRepository.getActualPlanDetail()!.change = ExercisePlanDetailChange.delete;
    planBloc.add(ExercisePlanRemoveExercise(exercisePlanDetail: exercisePlanRepository.getActualPlanDetail()!));
    emit(ExercisePlanCustomAddReady());
  }
}
