<?php
/**
 * Created by PhpStorm.
 * User: robin
 * Date: 9/30/16
 * Time: 8:58 PM
 */

namespace App\Business;

use App\DataAccess\RoutesDataAccess;
use App\Dbo\RoutesDbo;
use Luticate\Utils\Dbo\LuPaginatedDbo;

class RoutesBusiness
{
    protected static function getDataAccess()
    {
        return new RoutesDataAccess();
    }
    public static function containsRouteId(array $routes, string $id)
    {
        /**
         * @var $routes RoutesDbo[]
         */
        foreach ($routes as $route) {
            if ($route->getId() == $id) {
                return true;
            }
        }
        return false;
    }

    public static function getAll()
    {
        $config = MiscBusiness::getConfig();
        $dbos = [];
        foreach ($config["scheduleTypes"] as $scheduleType) {
            $scheduleDbos = static::getDataAccess()->getAll($scheduleType["resourceId"], $scheduleType["type"]);
            foreach ($scheduleDbos as $scheduleDbo) {
                if (!static::containsRouteId($dbos, $scheduleDbo->getId())) {
                    $dbos[] = $scheduleDbo;
                }
            }
        }
        usort($dbos, function ($dbo1, $dbo2)
        {
            /**
             * @var $dbo1 RoutesDbo
             * @var $dbo2 RoutesDbo
             */
            return intval($dbo1->getId()) > intval($dbo2->getId());
        });
        return new LuPaginatedDbo(count($dbos), $dbos);
    }
}