package com.bytesw.bytebot.service;

import com.bytesw.bytebot.bean.CalendarBean;
import com.bytesw.bytebot.bean.SchedulerTaskBean;
import com.bytesw.bytebot.etl.batch.service.ScheduleService;
import com.bytesw.bytebot.model.Calendar;
import com.bytesw.bytebot.model.SchedulerTask;
import com.bytesw.bytebot.repository.CalendarRepository;
import com.bytesw.bytebot.repository.SchedulerTaskRepository;
import com.bytesw.xdf.exception.AlreadyExistsException;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.service.XDFService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.*;

@Service
@Transactional
public class SchedulerTaskService extends XDFService<SchedulerTask, SchedulerTaskBean, BigInteger> {

    @Autowired
    private CalendarService CalendarService;

    @Autowired
    private CalendarRepository calendarRepository;

    @Autowired
    private SchedulerTaskRepository schedulerTaskRepository;

    @Autowired
    private ScheduleService scheduleService;

    protected SchedulerTaskService(SchedulerTaskRepository repository) {
        super(repository);
    }

    @Override
    protected SchedulerTask toModel(SchedulerTask model, SchedulerTaskBean bean) {
        if (model == null) {
            model = new SchedulerTask();
        }
        BeanUtils.copyProperties(bean, model);
        model.setInternals("Y".equals(bean.getInternals()));
        model.setProcessETL(bean.getEtlId());
        if (bean.getCalendarID() == null) {
            throw new NotFoundException("Calendar can not be null");
        }
        bean.setCalendarID(bean.getCalendarID().trim());
        Optional<Calendar> calendarOptional = this.calendarRepository.findById(bean.getCalendarID());
        if (!calendarOptional.isPresent()) {
            throw new NotFoundException("Calendar not found " + bean.getCalendarID());
        }
//        model.setCalendar(calendarOptional.get());
        return model;
    }

    @Override
    protected SchedulerTaskBean toBean(SchedulerTask model) {
        SchedulerTaskBean bean = new SchedulerTaskBean();
        BeanUtils.copyProperties(model, bean);
        bean.setInternals(model.getInternals().booleanValue() ? "Y" : "N");
        bean.setCalendarID(bean.getCalendarID().trim());
        bean.setEtlId(model.getProcessETL());
//      CalendarBean found = this.CalendarService.getById(bean.getCalendarID());
        Optional<Calendar> calendarOptional = this.calendarRepository.findById(bean.getCalendarID());
        if (!calendarOptional.isPresent()) {
            throw new NotFoundException("Calendar not found " + bean.getCalendarID());
        }
        bean.setCalendarName(calendarOptional.get().getName());

        return bean;
    }

    public Map<String, List> getInitialData() {
        List<CalendarBean> calendarBeanList = this.CalendarService.getAll();
        Map<String, List> initialData = new HashMap<>();
        initialData.put("calendarList", calendarBeanList);
        return initialData;
    }

    @Override
    public SchedulerTaskBean create(SchedulerTaskBean schedulerTaskBean) {
        SchedulerTask schedulerTask = new SchedulerTask();
        schedulerTask = toModel(schedulerTask, schedulerTaskBean);
        schedulerTask = schedulerTaskRepository.save(schedulerTask);
        scheduleService.restartTask(schedulerTask);
        return toBean(schedulerTask);
    }

    @Override
    public SchedulerTaskBean update(SchedulerTaskBean bean, BigInteger id) {
        Optional<SchedulerTask> model = schedulerTaskRepository.findById(id);
        if (!model.isPresent()) {
            throw new NotFoundException();
        }
        SchedulerTask schedulerTask = new SchedulerTask();
        schedulerTask = toModel(schedulerTask, bean);
        schedulerTask = schedulerTaskRepository.save(schedulerTask);
        scheduleService.restartTask(schedulerTask);
        return toBean(schedulerTask);
    }

    @Override
    public void delete (BigInteger id) {
        Optional<SchedulerTask> model = schedulerTaskRepository.findById(id);
        if (!model.isPresent()) {
            throw new NotFoundException();
        }
        scheduleService.cancelTask(model.get());
        schedulerTaskRepository.deleteById(id);
    }

}
