/**
 * 
 */
package de.bstly.board.businesslogic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import de.bstly.board.model.QView;
import de.bstly.board.model.View;
import de.bstly.board.model.support.ViewSorting;
import de.bstly.board.repository.ViewRepository;

/**
 * The Class ViewManager.
 */
@Component
public class ViewManager {

	@Autowired
	private ViewRepository viewRepository;
	private QView qView = QView.view;

	/**
	 * Exists.
	 *
	 * @param username the username
	 * @param name     the name
	 * @return true, if successful
	 */
	public boolean exists(String username, String name) {
		return viewRepository
				.exists(qView.username.equalsIgnoreCase(username).and(qView.name.eq(name)));
	}

	/**
	 * Gets the.
	 *
	 * @param id the id
	 * @return the user page
	 */
	public View get(Long id) {
		return viewRepository.findById(id).orElse(null);
	}

	/**
	 * Gets the.
	 *
	 * @param username the username
	 * @param name     the name
	 * @return the user page
	 */
	public View get(String username, String name) {
		return viewRepository
				.findOne(qView.username.equalsIgnoreCase(username).and(qView.name.eq(name)))
				.orElse(null);
	}

	/**
	 * Save.
	 *
	 * @param view the user page
	 * @return the user page
	 */
	public View save(View view) {
		return viewRepository.save(view);
	}

	/**
	 * Gets the by user.
	 *
	 * @param username the username
	 * @param page     the page
	 * @param size     the size
	 * @param sortBy   the sort by
	 * @param desc     the desc
	 * @return the by user
	 */
	public Page<View> getByUser(String username, int page, int size, boolean desc) {
		return viewRepository.findAll(qView.username.equalsIgnoreCase(username),
				PageRequest.of(page, size, desc ? Sort.by(Order.desc("index"), Order.desc("name"))
						: Sort.by(Order.asc("index"), Order.asc("name"))));
	}

	/**
	 * Count by user.
	 *
	 * @param username the username
	 * @return the long
	 */
	public long countByUser(String username) {
		return viewRepository.count(qView.username.equalsIgnoreCase(username));
	}

	/**
	 * Gets the public.
	 *
	 * @param username the username
	 * @param page     the page
	 * @param size     the size
	 * @param sortBy   the sort by
	 * @param desc     the desc
	 * @return the public
	 */
	public Page<View> getPublic(String username, int page, int size, String sortBy, boolean desc) {
		return viewRepository.findAll(
				qView.username.notEqualsIgnoreCase(username).and(qView.publicView.isTrue()),
				PageRequest.of(page, size, Sort.by(desc ? Direction.DESC : Direction.ASC, sortBy)));
	}

	/**
	 * Delete.
	 *
	 * @param username the username
	 * @param name     the name
	 */
	public void delete(String username, String name) {
		Assert.isTrue(exists(username, name), "View not found!");
		viewRepository.delete(get(username, name));
	}

	public void createDefault(String username) {
		if (!exists(username, "TOP")) {
			View viewTop = new View();
			viewTop.setName("TOP");
			viewTop.setUsername(username);
			viewTop.setSorting(ViewSorting.TOP);
			viewTop.setIndex(20);
			save(viewTop);
		}
		if (!exists(username, "NEW")) {
			View viewNew = new View();
			viewNew.setName("NEW");
			viewNew.setUsername(username);
			viewNew.setSorting(ViewSorting.NEW);
			viewNew.setIndex(40);
			save(viewNew);
		}
		if (!exists(username, "HOT")) {
			View viewHot = new View();
			viewHot.setName("HOT");
			viewHot.setUsername(username);
			viewHot.setSorting(ViewSorting.HOT);
			viewHot.setIndex(60);
			viewHot.setDivider(true);
			save(viewHot);
		}
		if (!exists(username, "LAST")) {
			View viewLast = new View();
			viewLast.setName("LAST");
			viewLast.setUsername(username);
			viewLast.setSorting(ViewSorting.LAST);
			viewLast.setIndex(80);
			save(viewLast);
		}
	}
}
