<?php


namespace App\Model\Crates\WorldOfWarcraft;


use stdClass;

class Instance
{
    /** @var stdClass */
    protected $data;

    /** @var  array */
    protected $bosses;

    /**
     * BattleNetProfile constructor.
     * @param $data
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getName() : string
    {
        return $this->data->name;
    }

    public function isDungeon() : bool
    {
        return $this->data->isDungeon;
    }

    public function isRaid() : bool
    {
        return $this->data->isRaid;
    }

    public function getMinimumPlayers() : int
    {
        if (is_numeric($this->data->numPlayers)) {
            return $this->data->numPlayers;
        }

        list($min, $max) = explode('-', str_replace('/', '-', $this->data->numPlayers), 2);
        return $min;
    }

    public function getMaximumPlayers() : int
    {
        if (is_numeric($this->data->numPlayers)) {
            return $this->data->numPlayers;
        }

        list($min, $max) = explode('-',str_replace('/', '-', $this->data->numPlayers), 2);
        return $max;
    }

    public function getModes() : array
    {
        $modes = $this->data->availableModes;
        if (empty($modes)) {
            $modes[] = 'RAID_NORMAL';
        }

        return $modes;
    }

    /**
     * @return Boss[]
     */
    public function getBosses() : array
    {
        if ($this->bosses === null) {
            $this->parseBosses();
        }

        return $this->bosses;
    }

    public function getRequiredLevelNormal()
    {
        return $this->data->advisedMinLevel;
    }

    public function getRequiredLevelHeroic()
    {
        return $this->data->advisedHeroicMinLevel;
    }

    protected function parseBosses()
    {
        $this->bosses = array();

        foreach ($this->data->bosses as $bossData) {
            $this->bosses[] = new Boss($bossData);
        }
    }
}