<?php


namespace App\Model\Facades\Readers;


use App\Model\Crates\WorldOfWarcraft\CharacterClass;
use App\Model\Crates\WorldOfWarcraft\Instance;
use App\Model\Crates\WorldOfWarcraft\Race;
use App\Model\Crates\WorldOfWarcraft\Realm;
use App\Model\Services\BattleNetApiService;

class WowApiReader extends BaseReader
{
    /** @var BattleNetApiService */
    protected $battleNetApiService;
    /** @var RegionReader */
    protected $regionReader;

    public function __construct(BattleNetApiService $battleNetApiService, RegionReader $regionReader)
    {
        $this->battleNetApiService = $battleNetApiService;
        $this->regionReader = $regionReader;
    }

    /**
     * @param $region
     * @return Realm[]
     */
    public function retrieveRealms($region) : array
    {
        $regionEntity = $this->regionReader->validateRegion($region);
        $data = $this->battleNetApiService->retrieveRealmStatus($regionEntity->getApiIdentifier());

        $realms = array();
        foreach ($data->realms as $realm) {
            $realms[] = new Realm($realm);
        }

        return $realms;
    }

    /**
     * @return CharacterClass[]
     */
    public function retrieveCharacterClasses() : array
    {
        $data = $this->battleNetApiService->retrieveCharacterClasses('eu');

        $classes = array();
        foreach ($data->classes as $characterClass) {
            $classes[] = new CharacterClass($characterClass);
        }

        return $classes;
    }

    /**
     * @return Race[]
     */
    public function retrieveRaces() : array
    {
        $data = $this->battleNetApiService->retrieveRaces('eu');

        $races = array();
        foreach ($data->races as $race) {
            $races[] = new Race($race);
        }

        return $races;
    }

    /**
     * @return Instance[]
     */
    public function retrieveInstances() : array
    {
        $data = $this->battleNetApiService->retrieveInstances('eu');

        $instances = array();
        foreach ($data->zones as $zone) {
            $instances[] = new Instance($zone);
        }

        return $instances;
    }

}