<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\GameData\Mmorpg\Instance;
use App\Model\Entities\GameData\Mmorpg\InstanceType;
use App\Model\Services\InstanceService;
use Kdyby\Doctrine\QueryBuilder;

class InstanceReader extends BaseReader
{
    /** @var GameReader */
    protected $gameReader;
    /** @var InstanceService */
    protected $instanceService;

    public function __construct(GameReader $gameReader, InstanceService $instanceService)
    {
        $this->gameReader = $gameReader;
        $this->instanceService = $instanceService;
    }

    public function getInstancesNameAssoc($game)
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getInstancesNameAssoc($gameEntity);
    }

    private function findInstanceById(int $id) : Instance
    {
        $instance = $this->instanceService->findInstanceById($id);
        $this->validateEntity($instance);

        return $instance;
    }

    public function findInstanceTypeById(int $id) : InstanceType
    {
        $instanceType = $this->instanceService->findInstanceTypeById($id);
        $this->validateEntity($instanceType);

        return $instanceType;
    }

    /**
     * @param $game
     * @return QueryBuilder
     */
    public function getInstancesGridDataSource($game) : QueryBuilder
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getInstancesGridDataSource($gameEntity);
    }

    public function getInstanceTypesGridDataSource($game) : QueryBuilder
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getInstanceTypesGridDataSource($gameEntity);
    }

    public function getBossesGridDataSource($game) : QueryBuilder
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getBossesGridDataSource($gameEntity);
    }

    public function getInstanceTypeNamePairs($game) : array
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getInstanceTypeNamePairs($gameEntity);
    }

    public function getInstanceTypesNameAssoc($game) : array
    {
        $gameEntity = $this->gameReader->validateGame($game);

        return $this->instanceService->getInstanceTypesNameAssoc($gameEntity);
    }

    public function validateInstance($instance) : Instance
    {
        if ($instance instanceof Instance) {
            return $instance;
        }

        return $this->findInstanceById($instance);
    }

    public function validateInstanceType($instanceType) : InstanceType
    {
        if ($instanceType instanceof InstanceType) {
            return $instanceType;
        }

        return $this->findInstanceTypeById($instanceType);
    }

}