import 'dart:async';

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/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()) {
    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();

    selectedSport = customerRepository.getSport();
    print("selected: $selectedFitnessItem sport: $selectedSport " + customerRepository.customer!.fitnessLevel.toString());
  }

  Sport? selectedSport;
  String? selectedFitnessItem;

  @override
  Stream<CustomerChangeState> mapEventToState(
    CustomerChangeEvent event,
  ) async* {
    try {
      if (event is CustomerLoad) {
        yield CustomerChangeLoading();
        yield CustomerDataChanged();
      } else if (event is CustomerGoalChange) {
        yield CustomerChangeLoading();
        customerRepository.setGoal(event.goal);
        yield CustomerDataChanged();
      } else if (event is CustomerChangePasswordObscure) {
        yield CustomerChangeLoading();
        visiblePassword = !visiblePassword;
        yield CustomerDataChanged();
      } else if (event is CustomerFitnessChange) {
        yield CustomerChangeLoading();
        customerRepository.setFitnessLevel(event.fitness);
        selectedFitnessItem = event.fitness;
        yield CustomerDataChanged();
      } else if (event is CustomerBirthYearChange) {
        yield CustomerChangeLoading();
        customerRepository.setBirthYear(event.year);
        year = event.year;
        yield CustomerDataChanged();
      } else if (event is CustomerWeightChange) {
        yield CustomerChangeLoading();
        customerRepository.setWeight(event.weight);
        weight = event.weight.toDouble();
        yield CustomerDataChanged();
      } else if (event is CustomerHeightChange) {
        yield CustomerChangeLoading();
        customerRepository.setHeight(event.height);
        height = event.height.toDouble();
        yield CustomerDataChanged();
      } else if (event is CustomerBodyTypeChange) {
        customerRepository.setBodyType(event.bodyType);
        yield CustomerDataChanged();
      } else if (event is CustomerEmailChange) {
        customerRepository.setEmail(event.email);
        yield CustomerDataChanged();
      } else if (event is CustomerPasswordChange) {
        customerRepository.setPassword(event.password);
        yield CustomerDataChanged();
      } else if (event is CustomerFirstNameChange) {
        customerRepository.setFirstName(event.firstName);
        yield CustomerDataChanged();
      } else if (event is CustomerNameChange) {
        customerRepository.setName(event.name);
        yield CustomerDataChanged();
      } else if (event is CustomerGenderChange) {
        customerRepository.setSex(event.gender == 0 ? "m" : "w");
        yield CustomerDataChanged();
      } else if (event is CustomerSportChange) {
        yield CustomerChangeLoading();
        selectedSport = event.sport;
        yield CustomerDataChanged();
      } else if (event is CustomerSave) {
        yield CustomerSaving();
        if (validation()) {
          if (selectedFitnessItem != null) {
            customerRepository.setFitnessLevel(selectedFitnessItem!);
          }
          if (selectedSport != null) {
            customerRepository.customer!.sportId = selectedSport!.sportId;
          }

          await customerRepository.saveCustomer();
          Cache().initBadges();
          yield CustomerSaveSuccess();
        } else {
          yield CustomerSaveError(message: "Please provide the necessary information");
        }
      }
    } on Exception catch (e) {
      yield CustomerSaveError(message: e.toString());
    }
  }

  bool validation() {
    if (customerRepository.customer == null) throw Exception("Customer object not defined");
    return true;
    /* return (emailValidation(customerRepository.customer!.email) == null) &&
        (passwordValidation(customerRepository.customer!.password) == null) &&
        (nameValidation(customerRepository.customer!.firstname) == null) &&
        (nameValidation(customerRepository.customer!.name) == null); */
  }

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