import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/sport.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/repository/mautic_repository.dart';
import 'package:aitrainer_app/util/app_language.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';

part 'customer_change_event.dart';
part 'customer_change_state.dart';

class CustomerChangeBloc extends Bloc<CustomerChangeEvent, CustomerChangeState> {
  final CustomerRepository customerRepository;
  bool visiblePassword = false;
  int? year = 1990;
  double weight = 60;
  double height = 170;
  CustomerChangeBloc({required this.customerRepository}) : super(CustomerChangeInitial()) {
    this._load();
    on<CustomerLoad>(_onLoad);
    on<CustomerGoalChange>(_onGoalChange);
    on<CustomerChangePasswordObscure>(_onChangePasswordObscure);
    on<CustomerFitnessChange>(_onFitnessChange);
    on<CustomerBirthYearChange>(_onBirthyearChange);
    on<CustomerWeightChange>(_onWeightChange);
    on<CustomerHeightChange>(_onHeightChange);
    on<CustomerBodyTypeChange>(_onBodytypeChange);
    on<CustomerEmailChange>(_onEmailChange);
    on<CustomerPasswordChange>(_onPasswordChange);
    on<CustomerFirstNameChange>(_onFirstNameChange);
    on<CustomerNameChange>(_onNameChange);
    on<CustomerGenderChange>(_onGenderChange);
    on<CustomerSportChange>(_onSportChange);
    on<CustomerSaveFitness>(_onSaveFitness);
    on<CustomerSaveGoal>(_onSaveGoal);
    on<CustomerSaveSex>(_onSaveSex);
    on<CustomerSaveWeight>(_onSaveWeight);
    on<CustomerSaveHeight>(_onSaveHeight);
    on<CustomerSave>(_onSave);
  }

  void _onLoad(CustomerLoad event, Emitter<CustomerChangeState> emit) async {
    emit(CustomerChangeLoading());
    emit(CustomerDataChanged());
  }

  void _onGoalChange(CustomerGoalChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setGoal(event.goal);
    emit(CustomerDataChanged());
  }

  void _onChangePasswordObscure(CustomerChangePasswordObscure event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    visiblePassword = !visiblePassword;
    emit(CustomerDataChanged());
  }

  void _onFitnessChange(CustomerFitnessChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setFitnessLevel(event.fitness);
    selectedFitnessItem = event.fitness;
    emit(CustomerDataChanged());
  }

  void _onBirthyearChange(CustomerBirthYearChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setBirthYear(event.year);
    year = event.year;
    emit(CustomerDataChanged());
  }

  void _onWeightChange(CustomerWeightChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setWeight(event.weight);
    weight = event.weight.toDouble();
    emit(CustomerDataChanged());
  }

  void _onHeightChange(CustomerHeightChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setHeight(event.height);
    height = event.height.toDouble();
    emit(CustomerDataChanged());
  }

  void _onBodytypeChange(CustomerBodyTypeChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setBodyType(event.bodyType);
    emit(CustomerDataChanged());
  }

  void _onEmailChange(CustomerEmailChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setEmail(event.email);
    emit(CustomerDataChanged());
  }

  void _onPasswordChange(CustomerPasswordChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setPassword(event.password);
    emit(CustomerDataChanged());
  }

  void _onFirstNameChange(CustomerFirstNameChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setFirstName(event.firstName);
    emit(CustomerDataChanged());
  }

  void _onNameChange(CustomerNameChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setName(event.name);
    emit(CustomerDataChanged());
  }

  void _onGenderChange(CustomerGenderChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    customerRepository.setSex(event.gender == 0 ? "m" : "w");
    emit(CustomerDataChanged());
  }

  void _onSportChange(CustomerSportChange event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    selectedSport = event.sport;
    print("Selected Sport $selectedSport");
    emit(CustomerDataChanged());
  }

  void _onSaveFitness(CustomerSaveFitness event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    if (customerRepository.customer!.fitnessLevel == null) {
      throw Exception("Please select your fitness level");
    }
    emit(CustomerSaveSuccess());
  }

  void _onSaveGoal(CustomerSaveGoal event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    if (customerRepository.customer!.goal == null) {
      throw Exception("Please select your goal");
    }
    emit(CustomerSaveSuccess());
  }

  void _onSaveSex(CustomerSaveSex event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    if (customerRepository.customer!.sex == null) {
      throw Exception("Please select your biologial gender");
    }
    emit(CustomerSaveSuccess());
  }

  void _onSaveWeight(CustomerSaveWeight event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    emit(CustomerSaveSuccess());
  }

  void _onSaveHeight(CustomerSaveHeight event, Emitter<CustomerChangeState> emit) {
    emit(CustomerChangeLoading());
    emit(CustomerSaveSuccess());
  }

  void _onSave(CustomerSave event, Emitter<CustomerChangeState> emit) async {
    emit(CustomerSaving());
    if (validation()) {
      if (selectedFitnessItem != null) {
        customerRepository.setFitnessLevel(selectedFitnessItem!);
      }
      if (selectedSport != null) {
        customerRepository.customer!.sportId = selectedSport!.sportId;
      }

      if (customerRepository.customer!.lang == null) {
        customerRepository.customer!.lang = AppLanguage().appLocal.languageCode;
      }

      await customerRepository.saveCustomer();
      MauticRepository mauticRepository = MauticRepository(customerRepository: customerRepository);
      await mauticRepository.sendMauticDataChange();
      Cache().initBadges();
      emit(CustomerSaveSuccess());
    } else {
      emit(CustomerSaveError(message: "Please provide the necessary information"));
    }
  }

  void _load() {
    if (this.customerRepository.customer == null) {
      this.customerRepository.createNew();
    }
    year = this.customerRepository.customer!.birthYear;
    if (year == null || year == 0) {
      year = 1990;
    }
    weight = this.customerRepository.getWeight() == 0 ? 60 : this.customerRepository.getWeight();
    height = this.customerRepository.getHeight() == 0 ? 170 : this.customerRepository.getHeight();

    selectedFitnessItem = customerRepository.fitnessLevel;
    selectedSport = customerRepository.getSport();
  }

  Sport? selectedSport;
  String? selectedFitnessItem;

  bool validation() {
    if (customerRepository.customer == null) throw Exception("Customer object not defined");
    return true;
  }

  String? emailValidation(String? email) {
    String? message = Common.emailValidation(email);
    return message;
  }

  String? passwordValidation(String? value) {
    String? message = Common.passwordValidation(value);
    return message;
  }

  String? nameValidation(String? value) {
    if (value == null || value.length == 0) {
      return "Name too short";
    }
    return null;
  }

  Sport? get getSelectedSport => this.selectedSport;
  set setSelectedSport(Sport? selectedSport) => this.selectedSport = selectedSport;
}
