<?php

namespace LivraisonParSecteurs\Loop;

use LivraisonParSecteurs\LivraisonParSecteurs;
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;


/**
 * Class AreaScheduleLoop
 * @package LivraisonParSecteurs\Loop
 */
class AreaScheduleLoop 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_schedule) {

            $loopResultRow = new LoopResultRow($lps_area_schedule);
            $loopResultRow
                ->set("ID", $lps_area_schedule->getId())
                ->set("AREA_ID", $lps_area_schedule->getIdArea())
                ->set("DAY", $lps_area_schedule->getDay())
                ->set("DAY_LABEL", LivraisonParSecteurs::getDayLabel($lps_area_schedule->getDay()))
                ->set("BEGIN", $lps_area_schedule->getBeginTime())
                ->set("END", $lps_area_schedule->getEndTime())
                ->set("CALCULATED_DAY", LivraisonParSecteurs::calculateRelativeDate($lps_area_schedule->getDay())->format(LivraisonParSecteurs::FORMAT_DATES))
            ;
            $loopResult->addRow($loopResultRow);
        }
        return $loopResult;
    }

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

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

        return $area->orderByDay()->orderByBeginTime();
    }

}
