<?php

namespace LivraisonParSecteurs\Loop;

use LivraisonParSecteurs\Model\LpsAreaCityQuery;
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 AreaCitiesLoop
 * @package LivraisonParSecteurs\Loop
 */
class AreaCitiesLoop 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_cities) {

            $loopResultRow = new LoopResultRow($lps_area_cities);
            $loopResultRow
                ->set("ID", $lps_area_cities->getId())
                ->set("ZIPCODE", $lps_area_cities->getZipcode())
            ;
            $loopResult->addRow($loopResultRow);
        }
        return $loopResult;
    }

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

    /**
     * @inheritdoc
     */
    public function buildModelCriteria()
    {
        $query = LpsAreaCityQuery::create();
        if (null != $id = $this->getAreaId()) {
            $query->filterByIdArea($id);
        }

        return $query->orderByZipcode();
    }

}
