package com.bytesw.bytebot.service;

import com.bytesw.bytebot.bean.CalendarBean;
import com.bytesw.bytebot.bean.WeekSchedulerBean;
import com.bytesw.bytebot.model.Calendar;
import com.bytesw.bytebot.model.WeekScheduler;
import com.bytesw.bytebot.repository.CalendarRepository;
import com.bytesw.bytebot.repository.WeekSchedulerRepository;
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.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class WeekSchedulerService extends XDFService<WeekScheduler, WeekSchedulerBean, BigInteger> {
    @Autowired
    private WeekSchedulerRepository weekSchedulerRepository;

    @Autowired
    private CalendarService calendarService;

    @Autowired
    private CalendarRepository calendarRepository;

    @Autowired
    private CalendarService CalendarService;

    protected WeekSchedulerService(WeekSchedulerRepository repository) {
        super(repository);
    }

    @Override
    protected WeekScheduler toModel(WeekScheduler model, WeekSchedulerBean bean) {
        if (model == null) {
            model = new WeekScheduler();
        }
        BeanUtils.copyProperties(bean, model);

        if (bean.getCalendarID() == null) {
            throw new NotFoundException("Calendar can not be null");
        }
        bean.setCalendarID(bean.getCalendarID().trim());
        Optional<Calendar> calendarOptional = calendarRepository.findById(bean.getCalendarID().trim());
        if (!calendarOptional.isPresent()) {
            throw new NotFoundException("Calendar not found " + bean.getCalendarID());
        }

//        model.setCalendarID(calendarOptional.get());
        return model;
    }

    @Override
    protected WeekSchedulerBean toBean(WeekScheduler model) {
        WeekSchedulerBean bean = new WeekSchedulerBean();
        BeanUtils.copyProperties(model, bean);
        bean.setCalendarID(model.getCalendarID());

        CalendarBean found = this.CalendarService.getById(bean.getCalendarID().trim());

        if(found != null){
            bean.setCalendarName(found.getName());
        }

        return bean;
    }

    public List<WeekSchedulerBean> getWeekSchedulerByCalId(String caleID) {
        List<WeekSchedulerBean> weekSchedulerBeanList = new ArrayList<>(7);
        Optional<List<WeekScheduler>> weekSchedulerOptional = this.weekSchedulerRepository.findByCalendarId(caleID.trim());
        if (!weekSchedulerOptional.isPresent()) {
            for (int i = 0; i < 7; i++) {
                weekSchedulerBeanList.add(i, new WeekSchedulerBean());
            }
        } else {
            for (int i = 0; i < 7; i++) {
                WeekSchedulerBean aux = new WeekSchedulerBean();
                for (int j = 0; j < weekSchedulerOptional.get().size(); j++){
                    aux.setDayOfWeek(i+1);
                    if(weekSchedulerOptional.get().get(j).getDayOfWeek() == i+1){
                        BeanUtils.copyProperties(weekSchedulerOptional.get().get(j), aux);
                    }
                }
                weekSchedulerBeanList.add(i, aux);
            }
        }
        return weekSchedulerBeanList;
    }
}
