<?php

namespace Lc\SovBundle\Repository;

use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\Site\PageInterface;

abstract class AbstractStore implements StoreInterface
{
    public function createDefaultQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface
    {
        $query = $this->createQuery($query);
        $query->resetRelationsJoin();
        $this->filtersDefault($query);
        $this->relationsDefault($query);
        $this->orderByDefault($query);
        return $query;
    }

    public function createQuery(RepositoryQueryInterface $query = null): RepositoryQueryInterface
    {
        if (is_null($query)) {
            $query = $this->query->create();
        }

        return $query;
    }
/*
    public function orderByDefault(RepositoryQueryInterface $query)
    {
        return $query;
    }

    public function filtersDefault(RepositoryQueryInterface $query)
    {
        $query->filterIsOnlineAndOffline();
        return $query;
    }

    public function relationsDefault($query)
    {
        return $query;
    }
*/
    public function getRepositoryQuery()
    {
        return $this->query;
    }

    public function getOneById(int $id)
    {
        $query = $this->query->create();

        $query->filterById($id);

        return $query->findOne();
    }

    public function getOneBySlug(string $slug, bool $isOnline = true, $query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query->filterBySlug($slug);

        if ($isOnline) {
            $query->filterIsOnline();
        }

        return $query->findOne();
    }

    public function getOneByDevAlias(string $devAlias, $query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query->filterByDevAlias($devAlias);
        return $query->findOne();
    }

    public function getOneOnlineByDevAlias(string $devAlias, $query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query
                ->filterByDevAlias($devAlias)
                ->filterIsOnline();

        return $query->findOne();
    }

    public function getOneByOldUrl(string $oldUrl, $query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query
                ->filterByOldUrl($oldUrl)
                ->filterIsOnline();

        return $query->findOne();
    }

    public function get($query = null)
    {
        $query = $this->createDefaultQuery($query);
        return $query->find();
    }

    public function getAll($query = null)
    {
        $query = $this->createQuery($query);
        $query->filterIsOnlineAndOffline();
        return $query->find();
    }

    public function getOnline($query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query->filterIsOnline();
        return $query->find();
    }

    public function getOnlineAndOffline($query = null)
    {
        $query = $this->createDefaultQuery($query);
        $query->filterIsOnlineAndOffline();
        return $query->find();
    }

    public function getParent(bool $isOnline = true, $query = null)
    {
        $query = $this->createDefaultQuery($query);

        if ($isOnline) {
            $query->filterIsOnline();
        }

        $query->filterIsParent();

        return $query->find();
    }
}
