import 'dart:async';
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()) {
    // init();
  }

  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();
  }

  @override
  Stream<ExercisePlanCustomAddState> mapEventToState(ExercisePlanCustomAddEvent event) async* {
    try {
      if (event is ExercisePlanCustomAddLoad) {
        yield ExercisePlanCustomAddLoading();
        init();
        yield ExercisePlanCustomAddReady();
      } else if (event is ExercisePlanCustomAddChangeSerie) {
        yield ExercisePlanCustomAddLoading();
        serie = event.quantity;
        exercisePlanRepository.getActualPlanDetail()!.serie = event.quantity.toInt();
        yield ExercisePlanCustomAddReady();
      } else if (event is ExercisePlanCustomAddChangeQuantity) {
        yield ExercisePlanCustomAddLoading();
        quantity = event.quantity;
        exercisePlanRepository.getActualPlanDetail()!.repeats = event.quantity.toInt();
        yield ExercisePlanCustomAddReady();
      } else if (event is ExercisePlanCustomAddChangeQuantityUnit) {
        yield ExercisePlanCustomAddLoading();
        quantityUnit = event.quantity;
        exercisePlanRepository.getActualPlanDetail()!.weightEquation = event.quantity.toStringAsFixed(0);
        yield ExercisePlanCustomAddReady();
      } else if (event is ExercisePlanCustomAddSubmit) {
        yield 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()!));
        yield ExercisePlanCustomAddReady();
      } else if (event is ExercisePlanCustomAddRemove) {
        yield ExercisePlanCustomAddLoading();
        exercisePlanRepository.getActualPlanDetail()!.change = ExercisePlanDetailChange.delete;
        planBloc.add(ExercisePlanRemoveExercise(exercisePlanDetail: exercisePlanRepository.getActualPlanDetail()!));

        yield ExercisePlanCustomAddReady();
      }
    } on Exception catch (e) {
      yield ExercisePlanCustomAddError(message: e.toString());
    }
  }
}
