package sg.ncl.service.team.logic;

import org.springframework.stereotype.Service;
import sg.ncl.service.team.data.jpa.TeamEntity;
import sg.ncl.service.team.data.jpa.TeamRepository;
import sg.ncl.service.team.domain.Team;
import sg.ncl.service.team.domain.TeamVisibility;
import sg.ncl.service.team.web.TeamMemberInfo;
import sg.ncl.service.team.exceptions.TeamIdNullException;
import sg.ncl.service.team.exceptions.TeamNameNullException;
import sg.ncl.service.team.exceptions.TeamNotFoundException;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Desmond/Te Ye
 */
@Service
public class TeamService {

    private final TeamRepository teamRepository;

    @Inject
    protected TeamService(final TeamRepository teamRepository) {
        this.teamRepository = teamRepository;
    }

    public TeamEntity save(TeamEntity team) {
        TeamEntity teamEntity = new TeamEntity();

        teamEntity.setName(team.getName());
        teamEntity.setDescription(team.getDescription());
        teamEntity.setWebsite(team.getWebsite());
        teamEntity.setOrganisationType(team.getOrganisationType());
        teamEntity.setApplicationDate(team.getApplicationDate());

        return teamRepository.save(teamEntity);
    }

    public List<TeamEntity> get() {
        final List<TeamEntity> result = new ArrayList<>();
        for (TeamEntity team : teamRepository.findAll()) {
            result.add(team);
        }
        return result;
    }

    public List<TeamEntity> getPublic() {
        List<TeamEntity> result = teamRepository.findByVisibility(TeamVisibility.PUBLIC);
        return result;
    }

    public TeamEntity find(final String id) {
        if (id == null || id.isEmpty()) {
            throw new TeamIdNullException();
        }

        final TeamEntity one = teamRepository.findOne(id);
        if (one == null) {
            throw new TeamNotFoundException();
        }

        return one;
    }

    public TeamEntity getName(String name) {
        if (name == null || name.isEmpty()) {
            throw new TeamNameNullException();
        }

        final TeamEntity one = teamRepository.findByName(name);
        if (one == null) {
            throw new TeamNotFoundException();
        }
        return one;
    }

    public String getTeamStatus(final String id) {
        final TeamEntity one = this.find(id);
        return one.getStatus().toString();
    }

    public void update(final Team inputTeam) {
        final TeamEntity one = find(inputTeam.getId());

        if (inputTeam.getDescription() != null) {
            one.setDescription(inputTeam.getDescription());
        }

        if (inputTeam.getPrivacy() != null) {
            one.setPrivacy(inputTeam.getPrivacy());
        }

        if (inputTeam.getStatus() != null) {
            one.setStatus(inputTeam.getStatus());
        }

        if (inputTeam.getVisibility() != null) {
            one.setVisibility(inputTeam.getVisibility());
        }

        teamRepository.save(one);
    }

    public void addUserToTeam(final String teamId, TeamMemberInfo teamMemberInfo) {
            TeamEntity teamEntity = find(teamId);
            teamEntity.addMember(teamMemberInfo);
            teamRepository.save(teamEntity);
    }

    public void seedData() {
        TeamEntity teamEntity = new TeamEntity();

        teamEntity.setName("Aries");
        teamEntity.setDescription("This is a project description");
        teamEntity.setApplicationDate(ZonedDateTime.now());
        teamEntity.setOrganisationType("Academic");
        teamEntity.setWebsite("http://www.nus.edu.sg");
        teamRepository.save(teamEntity);
    }
}

@@ -76,16 +76,81 @@
 ervice;%0A
+import org.springframework.transaction.annotation.Transactional;%0A
 import s
@@ -185,24 +185,24 @@
 TeamEntity;%0A
-
 import sg.nc
@@ -917,32 +917,51 @@
 ository;%0A    %7D%0A%0A
+    @Transactional%0A
     public TeamE
@@ -1375,32 +1375,51 @@
 Entity);%0A    %7D%0A%0A
+    @Transactional%0A
     public List%3C
@@ -1618,32 +1618,51 @@
  result;%0A    %7D%0A%0A
+    @Transactional%0A
     public List%3C
@@ -1799,32 +1799,51 @@
  result;%0A    %7D%0A%0A
+    @Transactional%0A
     public TeamE
@@ -2133,32 +2133,51 @@
 urn one;%0A    %7D%0A%0A
+    @Transactional%0A
     public TeamE
@@ -2476,32 +2476,51 @@
 urn one;%0A    %7D%0A%0A
+    @Transactional%0A
     public Strin
@@ -2642,32 +2642,51 @@
 tring();%0A    %7D%0A%0A
+    @Transactional%0A
     public void 
@@ -3260,32 +3260,51 @@
 ve(one);%0A    %7D%0A%0A
+    @Transactional%0A
     public void 

add @Transactional to all public methods in TeamService