import 'dart:async';

import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/trans.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> with Trans {
  final CustomerRepository customerRepository;
  final BuildContext context;
  bool visiblePassword = false;
  int year = 1990;
  double weight = 60;
  double height = 170;
  CustomerChangeBloc({this.customerRepository, this.context}) : super(CustomerChangeInitial()) {
    year = this.customerRepository.customer.birthYear;
    if (year == 0) {
      year = 1990;
    }
    weight = this.customerRepository.getWeight() == 0 ? 60 : this.customerRepository.getWeight();
    height = this.customerRepository.getHeight() == 0 ? 170 : this.customerRepository.getHeight();
  }

  @override
  Stream<CustomerChangeState> mapEventToState(
    CustomerChangeEvent event,
  ) async* {
    try {
      if (event is CustomerLoad) {
        yield CustomerChangeLoading();
        yield CustomerDataChanged();
      } else if (event is CustomerGoalChange) {
        customerRepository.setGoal(event.goal);
        yield CustomerDataChanged();
      } else if (event is CustomerChangePasswordObscure) {
        visiblePassword = !visiblePassword;
        yield CustomerDataChanged();
      } else if (event is CustomerFitnessChange) {
        customerRepository.setFitnessLevel(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 CustomerSave) {
        yield CustomerSaving();
        if (validation()) {
          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() {
    return true;
    /* print("f " + customerRepository.customer.firstname);
    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);
    if (message != null) {
      message = t(message);
    }
    return message;
  }

  String passwordValidation(String value) {
    String message = Common.passwordValidation(value);
    if (message != null) {
      message = t(message);
    }
    return message;
  }

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