package de.jottyfan.camporganizer.module.confirmation.confirmation;

import java.util.List;

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

import de.jottyfan.camporganizer.db.jooq.enums.EnumProgress;
import de.jottyfan.camporganizer.module.confirmation.confirmation.model.BookingBean;
import de.jottyfan.camporganizer.module.confirmation.confirmation.model.CampOverviewBean;

/**
 *
 * @author jotty
 *
 */
@Service
public class ConfirmationService {
	@Autowired
	private ConfirmationRepository repository;

	public List<CampOverviewBean> getCampOverview(String currentUser) {
		return repository.getCampOverviewBeans(currentUser);
	}

	public List<BookingBean> getUntouched(String currentUser) {
		return repository.getUntouched(currentUser);
	}

	public List<BookingBean> getApproved(String currentUser) {
		return repository.getApproved(currentUser);
	}

	public List<BookingBean> getRejected(String currentUser) {
		return repository.getRejected(currentUser);
	}

	public List<BookingBean> getRevoked(String currentUser) {
		return repository.getRevoked(currentUser);
	}

	public String search(String needle, String linkURL, String currentUser) {
		StringBuilder buf = new StringBuilder(
				"<table class=\"table table-striped\"><thead><tr><th>Dabei</th><th>Name</th><th>Freizeit</th><th>Rolle</th></tr><tbody>");
		for (BookingBean bean : repository.getSearchResult(needle, currentUser)) {
			String acceptHtml = "";
			if (EnumProgress.requested.equals(bean.getProgress())) {
				acceptHtml = "<i class=\"fas fa-question framed framed-orange\"></i>";
			} else if (EnumProgress.approved.equals(bean.getProgress())) {
				acceptHtml = "<i class=\"fas fa-check framed framed-green\"></i>";
			} else if (EnumProgress.rejected.equals(bean.getProgress())) {
				acceptHtml = "<i class=\"fas fa-ban framed framed-red\"></i>";
			} else if (EnumProgress.revoked.equals(bean.getProgress())) {
				acceptHtml = "<i class=\"fas fa-skull framed framed-pink\"></i>";
			}
			buf.append(String.format("<tr><td>%s</td><td><a href=\"%s/%d\">%s</a></td><td>%s</td><td>%s</td></tr>",
					acceptHtml, linkURL, bean.getPkPerson(), bean.getFullname(), bean.getCamp(), bean.getRolename()));
		}
		buf.append("</tbody></table>");
		return buf.toString();
	}

	/**
	 * remove the booking and all of its dependencies
	 *
	 * @param id the id of the booking (t_person.pk)
	 */
	public Boolean removeBooking(Integer id) {
		return repository.removeBooking(id) > 0;
	}

}
