package ru.ulstu.timeline.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.repository.EventRepository;
import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.service.UserService;

import java.util.Date;
import java.util.List;

import static ru.ulstu.core.util.StreamApiUtils.convert;

@Service
public class EventService {

    private final EventRepository eventRepository;
    private final UserService userService;

    public EventService(EventRepository eventRepository,
                        UserService userService) {
        this.eventRepository = eventRepository;
        this.userService = userService;
    }

    public List<EventDto> findAllDto() {
        return convert(findAll(), EventDto::new);
    }

    public List<Event> findAll() {
        return eventRepository.findAll();
    }

    @Transactional
    public int create(EventDto eventDto) {
        return eventRepository.save(copyFromDto(new Event(), eventDto)).getId();
    }

    private Event copyFromDto(Event event, EventDto eventDto) {
        event.setExecuteDate(eventDto.getExecuteDate());
        event.setCreateDate(eventDto.getId() == null ? new Date() : eventDto.getCreateDate());
        event.setDescription(eventDto.getDescription());
        event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
        event.setTitle(eventDto.getTitle());
        event.setPeriod(eventDto.getPeriod());
        event.setStatus(eventDto.getStatus());
        event.setUpdateDate(new Date());
        return event;
    }

    @Transactional
    public Integer update(EventDto eventDto) {
        Event event = eventRepository.findOne(eventDto.getId());
        return eventRepository.save(copyFromDto(event, eventDto)).getId();
    }

    @Transactional
    public void delete(Integer eventId) {
        Event event = eventRepository.findOne(eventId);
        event.setParents(null);
        eventRepository.delete(event);
    }

    public List<Event> findByIds(List<Integer> ids) {
        return eventRepository.findAll(ids);
    }

    public void createBasedOn(Event event, Date executeDate) {
        //backup event id
        Integer parentEventId = event.getId();

        event.setId(null);
        event.setStatus(Event.EventStatus.POSSIBLE);
        event.setExecuteDate(executeDate);
        event.setCreateDate(new Date());
        event.setUpdateDate(new Date());
        event = eventRepository.save(event);

        //set child to parent
        Event parentEvent = eventRepository.findOne(parentEventId);
        parentEvent.setChild(event);
        eventRepository.save(parentEvent);
    }

    public List<Event> findByCurrentDate() {
        return eventRepository.findByCurrentDate();
    }

    public List<Event> findAllFuture() {
        return eventRepository.findAllFuture();
    }

    public List<EventDto> findAllFutureDto() {
        return convert(findAllFuture(), EventDto::new);
    }
}
