package at.ac.tuwien.inso.service.impl;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

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 com.sun.istack.internal.Nullable; this does not exist!

import at.ac.tuwien.inso.dto.SemesterDto;
import at.ac.tuwien.inso.entity.Semester;
import at.ac.tuwien.inso.repository.SemesterRepository;
import at.ac.tuwien.inso.service.SemesterService;

@Service
public class SemesterServiceImpl implements SemesterService {
	
	private static final Logger log = LoggerFactory.getLogger(SemesterServiceImpl.class);

    @Autowired
    private SemesterRepository semesterRepository;

    @Autowired
    private Clock clock;

    @Override
    @Transactional
    public SemesterDto create(SemesterDto semester) {
    	log.info("creating semester "+semester.getLabel());
        return semesterRepository.save(semester.toEntity()).toDto();
    }

    @Override
    @Transactional(readOnly = true)
    public SemesterDto getCurrentSemester() {
        Semester s = semesterRepository.findFirstByOrderByIdDesc();
        
        if(s!=null){
        	log.info("returning current semester "+s.getLabel());
        	return s.toDto();
        }else{
        	log.warn("current semester is null");
        	return null;
        }
    }

    @Override
    public SemesterDto getOrCreateCurrentSemester() {
        LocalDateTime localDateTime = LocalDateTime.now(clock);
        Calendar now = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
        return getOrCreateCurrentSemester(now);
    }

    /**
     * If now is still in the current semester, we return the semester
     *
     * Otherwise the current semester will be created and returned
     *
     * @param now date to compare with
     */
    public SemesterDto getOrCreateCurrentSemester(Calendar now) {
        SemesterDto current = getCurrentSemester();

        if (current == null || !current.isCurrent(now)) {
            // TODO logging!!!
            return create(SemesterDto.calculateCurrentSemester(now));
        }

        return current;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SemesterDto> findAll() {
    	log.info("finding all semesters");
        return convertSemesterListToSemesterDtoList(semesterRepository.findAllByOrderByIdDesc());
    }

    @Override
    @Transactional(readOnly = true)
    public List<SemesterDto> findAllSince(SemesterDto semester) {
    	log.info("finding all semesters since "+semester.getLabel());
        return convertSemesterListToSemesterDtoList(semesterRepository.findAllSince(semester.toEntity()));
    }
    
    private List<SemesterDto> convertSemesterListToSemesterDtoList(List<Semester> semesters){
    	List<SemesterDto> toReturn = new ArrayList<SemesterDto>();
        for(Semester s : semesters){
        	toReturn.add(s.toDto());
        }
        return toReturn;
    }
}

@@ -826,11 +826,13 @@
 e %7B%0A
-%09%0A%09
+%0A    
 priv
@@ -1105,33 +1105,36 @@
 semester) %7B%0A    
-%09
+    
 log.info(%22creati
@@ -1146,17 +1146,19 @@
 mester %22
-+
+ + 
 semester
@@ -1361,16 +1361,23 @@
 mester s
+emester
  = semes
@@ -1418,24 +1418,16 @@
 Desc();%0A
-        
 %0A       
@@ -1433,12 +1433,22 @@
   if
+ 
 (s
+emester 
 !=
+ 
 null
@@ -1454,25 +1454,28 @@
 l)%7B%0A        
-%09
+    
 log.info(%22re
@@ -1500,18 +1500,27 @@
 mester %22
-+s
+ + semester
 .getLabe
@@ -1537,17 +1537,20 @@
     
-%09
+    
 return s
 .toD
@@ -1545,16 +1545,23 @@
 return s
+emester
 .toDto()
@@ -1575,12 +1575,14 @@
    %7D
+ 
 else
+ 
 %7B%0A  
@@ -1583,25 +1583,28 @@
 e %7B%0A        
-%09
+    
 log.warn(%22cu
@@ -1637,17 +1637,20 @@
         
-%09
+    
 return n
@@ -2576,33 +2576,36 @@
 findAll() %7B%0A    
-%09
+    
 log.info(%22findin
@@ -2848,17 +2848,20 @@
 ) %7B%0A    
-%09
+    
 log.info
@@ -2891,17 +2891,19 @@
  since %22
-+
+ + 
 semester
@@ -3132,17 +3132,20 @@
 s)%7B%0A    
-%09
+    
 List%3CSem
@@ -3179,27 +3179,16 @@
 rayList%3C
-SemesterDto
 %3E();%0A   
@@ -3233,9 +3233,12 @@
     
+    
-%09
 toRe

Fix code style