package com.uca.flights;

import java.time.Duration;
import java.util.Iterator;

/**
 * Represent an step : an airport and a duration from first take off
 * @Author Cesano Ugo, Colmerauer Clément
 * 
 */
public class Step implements Iterator<Step>, Cloneable
{
	/**Step duration*/
	private Duration duration;
	/**Step airport*/
	private Airport  airport;
	/**Step next step*/
	private Step     next; 
	
	/**
     * Constructor
     * @param airport an airport
     * @param duration the step duration
     * @throws IllegalArgumentException on null parameter
     */
	public Step(Airport airport, Duration duration) 
	{
		if(airport == null)
		{
			throw new IllegalArgumentException("Airport can't be null.");
		}
		if(duration == null)
		{
			throw new IllegalArgumentException("Duration can't be null.");
		}
		this.airport  = airport;
		this.duration = duration;
	}

	/**
     * Getter
     * @return step duration
     */
	public Duration getDuration()
	{
		return this.duration;
	}

	/**
     * Getter
     * @return step airport
     */
	public Airport getAirport()
	{
		return this.airport;
	}

	/**
     * Delay the duration from another duration
     * @param d the duration
     * @throws IllegalArgumentException on null parameter
     */
	void delay(Duration d)
	{
		if(d == null)
		{
			throw new IllegalArgumentException("Duration can't be null.");
		}
		this.duration = d.plus(this.duration);
	}

	/**
     * Getter, implementation of iterator
     * @return next step
     */
	public Step next()
	{
		return this.next;
	}

	/**
     * Getter, implementation of iterator
     * @return if next step exist
     */
	public boolean hasNext()
	{
		return this.next != null;
	}

	/**
     * Setter
     * @param s the next step
     * @throws IllegalArgumentException on null parameter
     */
	void setNext(Step s)
	{
		if(s == null)
		{
			throw new IllegalArgumentException("Step can't be null.");
		}
		this.next = s;
	}

	/**
     * Redefinition of clone
     * @return new step with same attributes
     */
	@Override
	public Object clone()
	{
		return new Step(this.airport, this.duration);
	}

	/**
     * Redefinition of toString
     * @return a string with airport and duration in hours
     */
	@Override 
	public String toString()
	{
		return this.airport.toString() + " for " + String.valueOf(this.duration.toHours()) + "hours.";
	}

}