package com.service.impl;

import com.entity.Task;
import com.repository.TaskRepository;
import com.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * Implementation of the task service.
 *
 * @author Laurentiu Marcut
 */
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskRepository taskRepository;

    @Override
    public Task select(int task) throws SQLException {
        Task taskObj =  this.taskRepository.select(task);
        List<String> tags = this.taskRepository.selectAllTags(taskObj.getId());
        taskObj.setTags(tags);

        return taskObj;
    }

    @Override
    public List<Task> selectAll(String project) throws SQLException {
        List<Task> tasks = this.taskRepository.selectAll(project);

        for (int i = 0; i < tasks.size(); i++) {
            List<String> tags = this.taskRepository.selectAllTags(tasks.get(i).getId());
            tasks.get(i).setTags(tags);
        }

        return tasks;
    }

    @Override
    public List<Task> selectAllWithoutSprint(String project) throws SQLException {
        List<Task> tasks = this.taskRepository.selectAllWithoutSprint(project);

        for (int i = 0; i < tasks.size(); i++) {
            List<String> tags = this.taskRepository.selectAllTags(tasks.get(i).getId());
            tasks.get(i).setTags(tags);
        }

        return tasks;
    }


    @Override
    public List<Task> selectAllByTags(String project, List<String> tags) throws SQLException {
        List<Task> tasks = this.taskRepository.selectAllByTags(project, tags);

        for (int i = 0; i < tasks.size(); i++) {
            List<String> taskTags = this.taskRepository.selectAllTags(tasks.get(i).getId());
            tasks.get(i).setTags(taskTags);
        }

        return tasks;
    }

    @Override
    public void insert(Task task) throws SQLException {
        int totalPriorities = this.taskRepository.selectTotalPriorities(task.getProject());
        task.setPriority(totalPriorities + 1);
        int taskId = this.taskRepository.insert(task);

        for (String tag : task.getTags()) {
            this.taskRepository.insertTag(taskId, tag);
        }
    }

    @Override
    public void delete(String project, int id, int priority) throws SQLException {
        this.taskRepository.decreasePrioritiesBeforeDeleteTask(project, priority);
        this.taskRepository.delete(id);
    }

    @Override
    public void insertTag(int task, String tag) throws SQLException {
        this.taskRepository.insertTag(task, tag);
    }

    @Override
    public void deleteTag(int task, String tag) throws SQLException {
        this.taskRepository.deleteTag(task, tag);
    }

    @Override
    public void updatePriority(String project, int task, int previousPriority, int currentPriority) throws SQLException {
        if (previousPriority > currentPriority) {
            this.taskRepository.increasePriorities(project, previousPriority, currentPriority);
        } else {
            this.taskRepository.decreasePriorities(project, previousPriority, currentPriority);
        }

        this.taskRepository.updatePriority(task, currentPriority);
    }

    @Override
    public void updatePoints(int task, int points) throws SQLException {
        this.taskRepository.updatePoints(task, points);
    }

    @Override
    public void updateAssignedTo(int task, String assignedTo) throws SQLException {
        this.taskRepository.updateAssignedTo(task, assignedTo);
    }

    @Override
    public void updateSprint(int task, int sprint) throws SQLException {
        this.taskRepository.updateSprint(task, sprint);
    }

    @Override
    public void updateState(int task, String state) throws SQLException {
        this.taskRepository.updateState(task, state);
    }


}
