package ch.uzh.csg.reimbursement.service;

import java.util.Date;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.uzh.csg.reimbursement.dto.ExpenseDto;
import ch.uzh.csg.reimbursement.model.Expense;
import ch.uzh.csg.reimbursement.model.ExpenseItem;
import ch.uzh.csg.reimbursement.model.User;
import ch.uzh.csg.reimbursement.model.exception.ExpenseItemNotFoundException;
import ch.uzh.csg.reimbursement.model.exception.ExpenseNotFoundException;
import ch.uzh.csg.reimbursement.repository.ExpenseRepositoryProvider;

@Service
@Transactional
public class ExpenseService {

	private final Logger LOG = LoggerFactory.getLogger(ExpenseService.class);

	@Autowired
	private ExpenseRepositoryProvider expenseRepository;

	@Autowired
	private UserService userService;

	public void create(ExpenseDto dto) {
		User user = userService.getLoggedInUser();
		//TODO Determine where contactPerson will be defined
		User contactPerson = userService.findByUid("cleib");
		Date currentDate = new Date();
		Expense expense = new Expense(user, currentDate, contactPerson, dto.getBookingText());
		expenseRepository.create(expense);
	}

	public Set<Expense> findAllByUser(String uid) {
		Set<Expense> expenses = expenseRepository.findAllByUser(uid);
		if (expenses.isEmpty()) {
			LOG.debug("No expenses found for the user wih the uid: " + uid);
			throw new ExpenseNotFoundException();
		}
		return expenses;
	}

	public Set<Expense> findAllByCurrentUser() {
		User user = userService.getLoggedInUser();
		return findAllByUser(user.getUid());
	}

	public void updateExpense(String uid, ExpenseDto dto) {
		Expense expense = findByUid(uid);
		//TODO Determine where contactPerson will be defined
		User contactPerson = userService.findByUid("cleib");
		Date currentDate = new Date();
		expense.updateExpense(currentDate, contactPerson, dto.getBookingText());
	}

	public void computeTotalAmount(String uid) {
		Expense expense = findByUid(uid);
		double totalAmount=0;

		for(ExpenseItem item: expense.getExpenseItems()){
			totalAmount += totalAmount + item.getAmount();
		}
		expense.setTotalAmount(totalAmount);
	}

	public Expense findByUid(String uid) {
		Expense expense = expenseRepository.findByUid(uid);

		if (expense == null) {
			LOG.debug("Expense not found in database with uid: " + uid);
			throw new ExpenseNotFoundException();
		}
		return expense;
	}

	public Set<ExpenseItem> findAllExpenseItemsByUid(String uid) {
		Expense expense = findByUid(uid);
		if (expense.getExpenseItems().isEmpty()) {
			LOG.debug("No expenseItems found for the user wih the uid: " + expense.getUser().getUid());
			throw new ExpenseItemNotFoundException();
		}
		return expense.getExpenseItems();
	}
}

@@ -509,86 +509,8 @@
 er;%0A
-import ch.uzh.csg.reimbursement.model.exception.ExpenseItemNotFoundException;%0A
 impo
@@ -579,16 +579,16 @@
 eption;%0A
+
 import c
@@ -1306,31 +1306,14 @@
 %7B%0A%09%09
-Set%3CExpense%3E expenses =
+return
  exp
@@ -1351,168 +1351,8 @@
 d);%0A
-%09%09if (expenses.isEmpty()) %7B%0A%09%09%09LOG.debug(%22No expenses found for the user wih the uid: %22 + uid);%0A%09%09%09throw new ExpenseNotFoundException();%0A%09%09%7D%0A%09%09return expenses;%0A
 %09%7D%0A%0A
@@ -2363,32 +2363,32 @@
 d(String uid) %7B%0A
+
 %09%09Expense expens
@@ -2411,197 +2411,8 @@
 d);%0A
-%09%09if (expense.getExpenseItems().isEmpty()) %7B%0A%09%09%09LOG.debug(%22No expenseItems found for the user wih the uid: %22 + expense.getUser().getUid());%0A%09%09%09throw new ExpenseItemNotFoundException();%0A%09%09%7D%0A
 %09%09re

return empty list if no expenses/expenseItems are stored instead of an exception