<?php

namespace LivraisonParSecteurs\Loop;

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 LivraisonParSecteurs\Model\LpsAreaQuery;

/**
 * Class AreaLoop
 * @package LivraisonParSecteurs\Loop
 */
class AreaLoop extends BaseLoop implements PropelSearchLoopInterface
{
    public $countable = false;
    public $timestampable = false;
    public $versionable = false;

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

            $loopResultRow = new LoopResultRow($lps_area);
            $loopResultRow
                ->set("ID", $lps_area->getId())
                ->set("TITLE", $lps_area->getTitle())
                ->set("ACTIVE", $lps_area->getActive())
                ->set("PRICE", $lps_area->getPrice())
                ->set("DELIVERY_DAYS", "Lundi....")
            ;
            $loopResult->addRow($loopResultRow);
        }
        return $loopResult;
    }

    /**
     * @inheritdoc
     */
    protected function getArgDefinitions()
    {
        return new ArgumentCollection(
            Argument::createIntListTypeArgument('id'),
            Argument::createIntListTypeArgument('active')
        );
    }

    /**
     * @inheritdoc
     */
    public function buildModelCriteria()
    {
        $query = LpsAreaQuery::create();

        /* Filtrage éventuel */
        if (null != $id = $this->getId()) {
            $query->filterById($id);
        }
        if (null != $active = $this->getActive()) {
            $query->filterByActive($active);
        }

        return $query->orderByTitle();
    }

}
