<?php

namespace App\Model\Entities\GameData\Mmorpg;

use App\Model\Entities\BaseNamedRecord;
use App\Model\Entities\Game;
use App\Model\Entities\Security\User;
use App\Model\Interfaces\ICommonRecord;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="faction")
 */
class Faction extends BaseNamedRecord implements ICommonRecord
{
    /**
     * @var Game
     * @ORM\ManyToOne(targetEntity="App\Model\Entities\Game")
     */
    protected $game;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="Race")
     * @ORM\JoinTable(name="available_races")
     */
    protected $races;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="CharacterClass")
     * @ORM\JoinTable(name="available_character_classes")
     */
    protected $characterClasses;

    public function __construct(User $createdBy)
    {
        parent::__construct($createdBy);
        $this->races = new ArrayCollection();
        $this->characterClasses = new ArrayCollection();
    }

    /**
     * @return Game
     */
    public function getGame() : Game
    {
        return $this->game;
    }

    /**
     * @param Game $game
     */
    public function setGame(Game $game)
    {
        $this->game = $game;
    }

    /**
     * @return ArrayCollection
     */
//    public function getRaces() : ArrayCollection // todo: find out why it is returning PersistentCollection
    public function getRaces()
    {
        return $this->races;
    }

    /**
     * @param Race $race
     */
    public function addRace(Race $race)
    {
        if (!$this->getRaces()->contains($race)) {
            $this->races->add($race);
        }
    }

    /**
     * @param Race $race
     */
    public function removeRace(Race $race)
    {
        $this->races->removeElement($race);
    }

    public function addCharacterClass(CharacterClass $class)
    {
        if (!$this->getCharacterClasses()->contains($class)) {
            $this->characterClasses->add($class);
        }
    }

    public function removeCharacterClass(CharacterClass $class)
    {
        $this->characterClasses->removeElement($class);
    }

    /**
     * @return ArrayCollection
     */
//    public function getCharacterClasses() : ArrayCollection // todo: find out why it is returning PersistentCollection
    public function getCharacterClasses()
    {
        return $this->characterClasses;
    }
}