package de.jottyfan.camporganizer.module.business.privileges.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import de.jottyfan.camporganizer.module.business.camp.impl.CampBean;
import de.jottyfan.camporganizer.module.business.privileges.IPrivilegesService;

/**
 *
 * @author jotty
 *
 */
@Service
public class PrivilegesService implements IPrivilegesService {
	@Autowired
	private PrivilegesGateway gateway;

	@Override
	public Map<Integer, CampBean> getPrivileges() {
		List<PrivilegesBean> list = gateway.getPrivileges();
		Map<Integer, CampBean> camps = new HashMap<>();
		for (PrivilegesBean bean : list) {
			CampBean campBean = camps.get(bean.getFkCamp());
			if (campBean == null) {
				campBean = new CampBean();
				camps.put(bean.getFkCamp(), campBean);
			}
			campBean.setPk(bean.getFkCamp());
			campBean.setName(bean.getCampName());
			campBean.setYear(bean.getCampYear());
			ProfileBean profileBean = new ProfileBean();
			profileBean.setPk(bean.getFkProfile());
			profileBean.setForename(bean.getForename());
			profileBean.setSurname(bean.getSurname());
			profileBean.setUsername(bean.getUsername());
			profileBean.setDuedate(bean.getDuedate());
			campBean.getProfiles().add(profileBean);
		}
		return camps;
	}

	@Override
	public List<ProfileBean> getProfiles() {
		return gateway.getProfiles();
	}

	@Override
	public void add(PrivilegesBean bean) {
		gateway.add(bean);
	}

	@Override
	public void remove(PrivilegesBean bean, String currentUser) {
		gateway.remove(bean, currentUser);
	}
}
