<?php

namespace AppBundle\Entity;

/**
 * TravelBookingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TravelBookingRepository extends \Doctrine\ORM\EntityRepository
{
    public function createFromBookingRequest(BookingRequest $bookingRequest, TravelDate $travelDate, $bookingPriceInfo)
    {
        $tp = $travelDate->getTravelProgram();
        $ret = new TravelBooking();
        $ret->setIp($_SERVER['REMOTE_ADDR']);
        $ret->setCreated(new \DateTime());
        $ret->setProgramName($tp->getTitle() . ' ('. $travelDate->getName() .')');
        //$ret->setClass()
        $ret->setSalutation($bookingRequest->getSalutation());
        $ret->setFirstName($bookingRequest->getFirstName());
        $ret->setLastName($bookingRequest->getLastName());
        $ret->setStreet($bookingRequest->getStreetAddress());
        //$ret->setHouseNr()
        $ret->setZipcode($bookingRequest->getZipCode());
        $ret->setCity($bookingRequest->getCity());
        $ret->setCountry($bookingRequest->getNation());
        $ret->setMail($bookingRequest->getEmail());
        $ret->setPhone($bookingRequest->getPhone());
        $ret->setFax($bookingRequest->getFax());
        $ret->setSelectedDeparture([
            'name' => $bookingRequest->getDeparture()->getName(),
            'extra_charge' => $bookingRequest->getDeparture()->getExtraCharge(),
            'extra_charge_total' => $bookingRequest->getTravelerCount()
        ]);
        $ret->setSelectedStartDate($travelDate->getStart());
        $ret->setSelectedEndDate($travelDate->getEnd());
        $ret->setSelectedAdults($bookingRequest->getTravelerCount());
        $ret->setSelectedChild1(0);
        $ret->setSelectedChild2(0);
        $ret->setSelectedChild3(0);
        

        $insurance = $bookingRequest->getInsurance();
        $ret->setInsuranceName($insurance ? $insurance->getName() : '0'); // #TODO Adapted from v2
        if (empty($bookingPriceInfo['insurances']))
        {
            $ret->setInsurances(false);
        }
        else
        {
            $insurances = [];
            foreach ($bookingPriceInfo['insurances'] as $insuranceInfo)
            {
                $insurances[] = [
                    'count' => $insuranceInfo['count'],
                    'price' => $insuranceInfo['insurancePriceValue'],
                    'code' => $insuranceInfo['insurancePrice']->getCode()
                ];
            }
            $ret->setInsurances($insurances);
        }
        
        //$ret->setTravelCancellation($bookingRequest->getTravelCancellation());
        $ret->setParticipants($bookingRequest->getTravelers());
        $ret->setParticipantsTotal($bookingRequest->getTravelerCount());
        $ret->setRooms($bookingPriceInfo['rooms']);
        $ret->setPriceTotal($bookingPriceInfo['total']);
        $ret->setComments($bookingRequest->getNotes());
        if (empty($bookingPriceInfo['options']))
        {
            $ret->setOptions(false);
        }
        else
        {
            $options = [];
            foreach ($bookingPriceInfo['options'] as $option)
            {
                $options[] = [
                    'name' => $option->getName(),
                    'price' => $option->getPrice()
                ];
            }
            $ret->setOptions($options);
        }
        $ret->setClassOptions(false);
        $ret->setExtraCategory(empty($bookingPriceInfo['classOptions']) ? false : $bookingPriceInfo['classOptions']);

        return $ret;
    }
}
