package com.bytesw.bytebot.service;

import com.bytesw.bytebot.bean.LegBean;
import com.bytesw.bytebot.bean.ReservationBean;
import com.bytesw.bytebot.model.Airport;
import com.bytesw.bytebot.model.Flight;
import com.bytesw.bytebot.model.Leg;
import com.bytesw.bytebot.model.Reservation;
import com.bytesw.bytebot.repository.AirportRepository;
import com.bytesw.bytebot.repository.FlightRepository;
import com.bytesw.bytebot.repository.LegRepository;
import com.bytesw.xdf.exception.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.Option;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class LegService {
    @Autowired
    private LegRepository legRepository;

    @Autowired
    private FlightRepository flightRepository;

    @Autowired
    private AirportRepository airportRepository;

    public void create(LegBean bean) {
        Optional<Flight> flight = this.flightRepository.findById(bean.getFlightId());
        Optional<Airport> origin = this.airportRepository.findById(bean.getOriginId());
        Optional<Airport> destination = this.airportRepository.findById(bean.getDestinationId());
        if (!flight.isPresent()) {
            throw new NotFoundException();
        }
        if (!origin.isPresent()) {
            throw new NotFoundException();
        }
        if (!destination.isPresent()) {
            throw new NotFoundException();
        }
        Leg model = new Leg();
        model.setFlight(flight.get());
        model.setArrivalDate(bean.getArrivalDate());
        model.setDepartureDate(bean.getDepartureDate());
        model.setArrivalTime(bean.getArrivalTime());
        model.setDepartureTime(bean.getDepartureTime());
        model.setOrigin(origin.get());
        model.setDestination(destination.get());
        this.legRepository.save(model);
    }

    public void update(LegBean bean, Long id) {
        Optional<Leg> found = this.legRepository.findById(id);
        Optional<Flight> flight = this.flightRepository.findById(bean.getFlightId());
        Optional<Airport> origin = this.airportRepository.findById(bean.getOriginId());
        Optional<Airport> destination = this.airportRepository.findById(bean.getDestinationId());
        if (!found.isPresent()) {
            throw new NotFoundException();
        }
        if (!flight.isPresent()) {
            throw new NotFoundException();
        }
        if (!origin.isPresent()) {
            throw new NotFoundException();
        }
        if (!destination.isPresent()) {
            throw new NotFoundException();
        }
        Leg model = found.get();
        model.setFlight(flight.get());
        model.setArrivalDate(bean.getArrivalDate());
        model.setDepartureDate(bean.getDepartureDate());
        model.setArrivalTime(bean.getArrivalTime());
        model.setDepartureTime(bean.getDepartureTime());
        model.setOrigin(origin.get());
        model.setDestination(destination.get());
        this.legRepository.save(model);
    }

    public List<Leg> getLeg(Long id) {
        List<Leg> found = legRepository.findAllByFlight_Id(id);
        if (found.isEmpty()) {
            throw new NotFoundException();
        }
        return found;
    }
}
