<?php
namespace ModulesAbstract\PluginReports\ExtraSupport\ItineraryAnalyser;

class ItineraryAnalyser extends \App\Utils\Itinerary\ItineraryAnalyser
{
    /**
     * Fill in itinerary gaps for "arrival unknown (ARNK)" cases
     * For TD-9714 - ARUNK/ARNK case
     * we need to obtain for the following segments:
     *          DXB-HKG, HKG-NGO, ........, PVG-HKG, HKG-KIX, KIX-HKG
     * the following result:
     *          DXB/HKG/NGO/PVG/HKG - for the prime ticket
     *          HKG/KIX/HKG - for the conjunction
     * - for this case we recompose the itinerary and add a segment for NGO-PVG
     *
     * @see https://en.wikipedia.org/wiki/Open-jaw_ticket#ARNK
     * @see https://support.travelport.com/webhelp/uapi/Content/Air/Shared_Air_Topics/ARNK_(Arrival_Unknown)_Segments.htm
     */
    public function fillItineraryGaps()
    {
        $recomposedItinerary = new \App\Itinerary();
        $segments = $this->itinerary->getSegments();
        foreach ($segments as $index => $segment) {
            $segmentArr = $segment->exportAsArray();
            $newSegment = new \ModulesAbstract\PluginReports\ExtraSupport\ItineraryAnalyser\ItinerarySegment();
            $newSegment->importFromArray($segmentArr);

            $recomposedItinerary->addSegment($newSegment);

            if ($segments[$index+1] and $segment->getArrivalStationCode() !== $segments[$index+1]->getDepartureStationCode()) {
                $newSegment = new \ModulesAbstract\PluginReports\ExtraSupport\ItineraryAnalyser\ItinerarySegment();

                $newSegment->setDepartureDateTime($segment->getDepartureDateTime());
                $newSegment->setDeparturePlace($segment->getArrivalCountryCode(), $segment->getArrivalCityId(), $segment->getArrivalStationCode());

                $newSegment->setArrivalDateTime($segments[$index+1]->getArrivalDateTime());
                $newSegment->setArrivalPlace($segments[$index+1]->getDepartureCountryCode(), $segments[$index+1]->getDepartureCityId(), $segments[$index+1]->getDepartureStationCode());

                $newSegment->setType("ARNK");

                $recomposedItinerary->addSegment($newSegment);
            }
        }

        $this->itinerary = $recomposedItinerary;

        return $this;
    }

}