package de.jottyfan.camporganizer.module.registration;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.jottyfan.camporganizer.module.camplist.model.BookingBean;
import de.jottyfan.camporganizer.module.registration.model.AlreadyKnownPersonBean;
import de.jottyfan.camporganizer.module.registration.model.CampBean;
import de.jottyfan.camporganizer.module.registration.model.RegistrationBean;

/**
 *
 * @author jotty
 *
 */
@Service
public class RegistrationService {

	@Autowired
	private RegistrationRepository repository;

	@Autowired
	private KeycloakRepository keycloak;

	/**
	 * return true if the camp is not yet over
	 *
	 * @param fkCamp the camp ID
	 * @return true or false
	 */
	public Boolean campIsNotYetOver(Integer fkCamp) {
		return repository.campIsNotYetOver(fkCamp);
	}

	/**
	 * get the camp
	 *
	 * @param fkCamp the ID of the camp
	 * @return the camp bean or null
	 */
	public CampBean getCamp(Integer fkCamp) {
		return repository.getCamp(fkCamp);
	}

	/**
	 * register the person for a camp; if registerInKeycloak, do so also
	 *
	 * @param bean the bean
	 * @return number of database rows; should be > 0
	 */
	public Integer register(RegistrationBean bean, String currentUser) {
		if (currentUser != null) {
			bean.setRegisterInKeycloak(false); // already registered
			bean.setLogin(currentUser);
		}
		Integer result = repository.register(bean);
		if (result > 0 && bean.getRegisterInKeycloak()) {
			keycloak.register(bean.getKcForename(), bean.getKcSurname(), bean.getLogin(), bean.getPassword(),
					bean.getKcEmail());
		}
		return result;
	}

	/**
	 * get the booking bean
	 *
	 * @param id the id of the registration (t_person.pk)
	 * @return the booking bean or null
	 */
	public BookingBean getBooking(Integer id) {
		return repository.getBooking(id);
	}

	/**
	 * revoke a booking
	 *
	 * @param id the ID of the booking
	 * @parem doRevoke if true, revoke the booking; if false, unrevoke and set it to
	 *        requested again
	 */
	public void revokeBooking(Integer id, Boolean doRevoke) {
		if (doRevoke) {
			repository.revokeBooking(id);
		} else {
			repository.unrevokeBooking(id);
		}
	}

	public void toggleConsent(Integer id) {
		repository.toggleConsent(id);
	}

	/**
	 * get already registered entries from the database
	 *
	 * @param currentUser the name of the current user; may be null
	 * @return the list or null if current user is null
	 */
	public List<AlreadyKnownPersonBean> getWellKnownRegistrations(String currentUser) {
		if (currentUser == null) {
			return null;
		} else {
			List<RegistrationBean> r = repository.getRegistrations(currentUser);
			if (r == null) {
				return null;
			} else {
				List<AlreadyKnownPersonBean> list = new ArrayList<>();
				for (RegistrationBean b : r) {
					AlreadyKnownPersonBean bean = new AlreadyKnownPersonBean();
					bean.setSource(b);
					bean.setOption(String.format("%s %s, %s, %s %s, %s, %s", b.getForename(), b.getSurname(), b.getStreet(),
							b.getZip(), b.getCity(), b.getEmail(), b.getPhone()));
					list.add(bean);
				}
				return list;
			}
		}
	}
}
