<?php

namespace duel\arena;

use duel\Loader;

/**
 * Class EmptyArenaChooser
 * @package vixikhd\onevsone
 */
class EmptyArenaChooser {

    /** @var Loader */
    public $loader;

    /**
     * EmptyArenaChooser constructor.
     * @param Loader $loader
     */
    public function __construct(Loader $loader)
    {
        $this->loader = $loader;
    }



    /**
     * @return null|Arena
     */
    public function getRandomArena() 
    {

        /** @var array */
        $availableArenas = [];
        foreach ($this->loader->arenaManager->arenas as $index => $arena) {
            $availableArenas[$index] = $arena;
        }

        foreach ($availableArenas as $index => $arena) {
            if($arena->stat !== 0) {
                unset($availableArenas[$index]);
            }
        }

        $arenasByPlayers = [];
        foreach ($availableArenas as $index => $arena) {
            $arenasByPlayers[$index] = count($arena->players);
        }

        arsort($arenasByPlayers);
        $top = -1;
        $availableArenas = [];

        foreach ($arenasByPlayers as $index => $players) {
            if($top == -1) {
                $top = $players;
                $availableArenas[] = $index;
            }
            else {
                if($top == $players) {
                    $availableArenas[] = $index;
                }
            }
        }

        if(empty($availableArenas)) {
            return null;
        }

        return $this->loader->arenaManager->arenas[$availableArenas[array_rand($availableArenas, 1)]];
    }
}