<?php

namespace LivraisonParSecteurs\Loop;

use LivraisonParSecteurs\LivraisonParSecteurs;
use LivraisonParSecteurs\Model\LpsAreaSchedule;
use LivraisonParSecteurs\Model\LpsAreaScheduleQuery;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Model\OrderQuery;

/**
 * @package LivraisonParSecteurs\Loop
 */
class SelectedDeliveryDate extends BaseLoop implements PropelSearchLoopInterface
{
    protected function getArgDefinitions()
    {
        return new ArgumentCollection(
            Argument::createIntTypeArgument('order_id')
        );
    }


    public function buildModelCriteria()
    {
        if (null !== $orderId = $this->getOrderId())
        {

            if (null !== $order = OrderQuery::create()->findPk($orderId))
            {
                return MondialRelayPickupAddressQuery::create()
                    ->filterByOrderAddressId($order->getDeliveryOrderAddressId());
            }
        }

        return null;
    }

    public function parseResults(LoopResult $loopResult)
    {
        foreach ($loopResult->getResultDataCollection() as $item) {
            $loopResultRow = new LoopResultRow($item);
            $loopResultRow
                ->set("ID", $relayData['id'])
                ->set("LATITUDE", $relayData['latitude'])
                ->set("LONGITUDE", $relayData['longitude'])
                ->set("ZIPCODE", $relayData['zipcode'])
                ->set("CITY", $relayData['city'])
                ->set("COUNTRY", $relayData['country'])
                ->set("NAME", $relayData['name'])
                ->set("ADDRESS", $relayData['address'])
                ->set("DISTANCE", $relayData['distance'])
                ->set("OPENINGS", $relayData['openings'])
            ;

            $loopResult->addRow($loopResultRow);
        }

        return $loopResult;
    }
}
