package com.service.impl;

import com.entity.Sprint;
import com.entity.Task;
import com.repository.SprintsRepository;
import com.repository.TaskRepository;
import com.service.SprintsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * Implementation of the sprint service.
 *
 * @author Laurentiu Marcut
 */
public class SprintsServiceImpl implements SprintsService {

    @Autowired
    private SprintsRepository sprintsRepository;

    @Autowired
    private TaskRepository taskRepository;

    @Override
    public Sprint select(String project, int id) throws SQLException {
        Sprint sprint =  this.sprintsRepository.select(project, id);

        for (Task task : sprint.getTasks()) {
            if ("Done".equalsIgnoreCase(task.getState())) {
                sprint.setDoneTasks(sprint.getDoneTasks()+1);
            } else if ("In progress".equalsIgnoreCase(task.getState())) {
                sprint.setInProgressTasks(sprint.getInProgressTasks()+1);
            } else if ("To do".equalsIgnoreCase(task.getState())) {
                sprint.setNotStartedTasks(sprint.getNotStartedTasks()+1);
            } else {
                sprint.setBloquedTasks(sprint.getBloquedTasks()+1);
            }

            List<String> taskTags = this.taskRepository.selectAllTags(task.getId());
            task.setTags(taskTags);
        }

        return sprint;
    }

    @Override
    public List<Sprint> selectAll(String project) throws SQLException {
        List<Sprint> sprints = this.sprintsRepository.selectAll(project);

        for (Sprint sprint : sprints) {
            for (Task task : sprint.getTasks()) {
                if ("Done".equalsIgnoreCase(task.getState())) {
                    sprint.setDoneTasks(sprint.getDoneTasks()+1);
                } else if ("In progress".equalsIgnoreCase(task.getState())) {
                    sprint.setInProgressTasks(sprint.getInProgressTasks()+1);
                } else if ("To do".equalsIgnoreCase(task.getState())) {
                    sprint.setNotStartedTasks(sprint.getNotStartedTasks()+1);
                } else {
                    sprint.setBloquedTasks(sprint.getBloquedTasks()+1);
                }

                List<String> taskTags = this.taskRepository.selectAllTags(task.getId());
                task.setTags(taskTags);
            }
        }

        return sprints;
    }

    @Override
    public List<Integer> selectAllIds(String project) throws SQLException {
        return this.sprintsRepository.selectAllIds(project);
    }

    @Override
    public int selectTotalSprints(String project) throws SQLException {
        return this.sprintsRepository.selectTotalSprints(project);
    }

    @Override
    public void insert(int id, String project) throws SQLException {
        this.sprintsRepository.insert(id, project);
    }

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

    @Override
    public void updateStartDate(int id, String project, Date startDate) throws SQLException {
        this.sprintsRepository.updateStartDate(id, project, startDate);
    }

    @Override
    public void updateEndDate(int id, String project, Date endDate) throws SQLException {
        this.sprintsRepository.updateEndDate(id, project, endDate);
    }

    @Override
    public void updateTotalCapacity(int id, String project, int totalCapacity) throws SQLException {
        this.sprintsRepository.updateTotalCapacity(id, project, totalCapacity);
    }

    @Override
    public void updateMembers(int id, String project, int members) throws SQLException {
        this.sprintsRepository.updateMembers(id, project, members);
    }

    @Override
    public void updateState(String project, int id, String state) throws SQLException {
        this.sprintsRepository.updateState(project, id, state);
    }

}
