/**
 * 
 */
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.QUserPage;
import de.bstly.board.model.UserPage;
import de.bstly.board.model.support.UserPageSorting;
import de.bstly.board.repository.UserPageRepository;

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

	@Autowired
	private UserPageRepository userPageRepository;
	private QUserPage qUserPage = QUserPage.userPage;

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

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

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

	/**
	 * Save.
	 *
	 * @param userPage the user page
	 * @return the user page
	 */
	public UserPage save(UserPage userPage) {
		return userPageRepository.save(userPage);
	}

	/**
	 * 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<UserPage> getByUser(String username, int page, int size, boolean desc) {
		return userPageRepository.findAll(qUserPage.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 userPageRepository.count(qUserPage.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<UserPage> getPublic(String username, int page, int size, String sortBy,
			boolean desc) {
		return userPageRepository.findAll(
				qUserPage.username.notEqualsIgnoreCase(username).and(qUserPage.publicPage.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), "UserPage not found!");
		userPageRepository.delete(get(username, name));
	}

	public void createDefault(String username) {
		if (!exists(username, "TOP")) {
			UserPage userPageTop = new UserPage();
			userPageTop.setName("TOP");
			userPageTop.setUsername(username);
			userPageTop.setSorting(UserPageSorting.TOP);
			userPageTop.setIndex(20);
			save(userPageTop);
		}
		if (!exists(username, "NEW")) {
			UserPage userPageNew = new UserPage();
			userPageNew.setName("NEW");
			userPageNew.setUsername(username);
			userPageNew.setSorting(UserPageSorting.NEW);
			userPageNew.setIndex(40);
			save(userPageNew);
		}
		if (!exists(username, "HOT")) {
			UserPage userPageHot = new UserPage();
			userPageHot.setName("HOT");
			userPageHot.setUsername(username);
			userPageHot.setSorting(UserPageSorting.HOT);
			userPageHot.setIndex(60);
			userPageHot.setDivider(true);
			save(userPageHot);
		}
		if (!exists(username, "LAST")) {
			UserPage userPageLast = new UserPage();
			userPageLast.setName("LAST");
			userPageLast.setUsername(username);
			userPageLast.setSorting(UserPageSorting.LAST);
			userPageLast.setIndex(80);
			save(userPageLast);
		}
	}
}
