<?php

namespace PlanificationLivraison\Loop;

use DateTime;
use PlanificationLivraison\Model\OrderDeliveryScheduleQuery;
use Propel\Runtime\ActiveQuery\Criteria;
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;

/**
 * Class ScheduledDeliveriesLoop
 * @package PlanificationLivraison\Loop
 */
class ScheduledDeliveriesLoop extends BaseLoop implements PropelSearchLoopInterface
{
    const DOMICILE = "domicile";
    const RETRAIT = "retrait";


    public $countable = true;

    /**
     * @param LoopResult $loopResult
     *
     * @return LoopResult
     */
    public function parseResults(LoopResult $loopResult)
    {
        foreach ($loopResult->getResultDataCollection() as $deliveries) {

            $loopResultRow = new LoopResultRow($deliveries);

            $delta = date_diff($deliveries->getDueDeliveryTimeStart(), new DateTime("now"));

            $loopResultRow
                ->set("ORDER_ID", $deliveries->getOrderId())
                ->set("ADDRESS_ID", $deliveries->getDeliveryAddressId())
                ->set("PLACE_ID", $deliveries->getDeliveryPlaceId())
                ->set("SCHEDULE_ID", $deliveries->getScheduleId())
                ->set("START_DATE", $deliveries->getDueDeliveryTimeStart())
                ->set("END_DATE", $deliveries->getDueDeliveryTimeEnd())
                ->set("DELTA", $delta->days)
            ;
            $loopResult->addRow($loopResultRow);
        }
        return $loopResult;
    }


    /**
     * @inheritdoc
     */
    protected function getArgDefinitions()
    {
        return new ArgumentCollection(
            Argument::createEnumListTypeArgument('domicile_ou_retrait', [
                self::DOMICILE,
                self::RETRAIT
            ], self::DOMICILE),
            Argument::createBooleanTypeArgument('only_future', true),
            Argument::createEnumListTypeArgument('order', [
                'date',
                'date-reverse'
            ], 'date')
        );
    }

    /**
     * @inheritdoc
     */
    public function buildModelCriteria()
    {
        $deliveries = OrderDeliveryScheduleQuery::create();

        foreach ($this->getDomicileOuRetrait() as $parametre) {
            switch ($parametre) {
                case self::DOMICILE:
                    $deliveries->filterByDeliveryAddressId(null, Criteria::NOT_EQUAL);
                    break;
                case self::RETRAIT:
                    $deliveries->filterByDeliveryPlaceId(null, Criteria::NOT_EQUAL);
                    break;
            }
        }

        if ($this->getOnlyFuture())
            $deliveries->filterByDueDeliveryTimeStart(array('min' => time()))->find();
        else
            $deliveries->filterByDueDeliveryTimeStart(array('max' => time()))->find();

        foreach ($this->getOrder() as $parametre) {
            switch ($parametre) {
                case 'date':
                    $deliveries->orderByDueDeliveryTimeStart(Criteria::ASC);
                    break;
                case 'date-reverse':
                    $deliveries->orderByDueDeliveryTimeStart(Criteria::DESC);
                    break;
            }
        }

        return $deliveries;
    }

}
