<?php

namespace App\Entity;
use App\Entity\Base\BaseCountry;
use App\Entity\IFace\IEntity;
use App\Utils\Utils;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Config\Definition\Exception\Exception;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="country")
 */
class EntCountry implements IEntity
{
    const COUNTRY_GERMANY_ID = 1;
    const COUNTRY_AUSTRIA_ID = 2;
    const COUNTRY_SWITZERLAND_ID = 3;
    const COUNTRY_FRANCE_ID = 4;

    private static $cacheCountriesById = null;
    private static $cacheCountriesByName = null;

    private static $validCountries = array(
        self::COUNTRY_GERMANY_ID => 'Deutschland',
        self::COUNTRY_AUSTRIA_ID => 'Österreich',
        self::COUNTRY_SWITZERLAND_ID => 'Schweiz',
        self::COUNTRY_FRANCE_ID => 'Frankreich',
    );

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=50, nullable=false)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=3)
     */
    protected $iso_code;


    /**
     * Gets static countries
     * @param ObjectManager $em
     * @return array
     */
    public static function getCountriesById(ObjectManager $em)
    {
        if (is_null(self::$cacheCountriesById)) {
            self::$cacheCountriesById = Utils::getSortedObjectsById($em->getRepository('App:EntCountry')->findAll());
        }
        return self::$cacheCountriesById;
    }

    /**
     * Gets static country
     * @param ObjectManager $em
     * @param $countryId
     * @return mixed|null
     */
    public static function getCountryById(ObjectManager $em, $countryId)
    {
        $entCountries = self::getCountriesById($em);
        return array_key_exists($countryId, $entCountries) ? $entCountries[$countryId] : null;
    }

    /**
     * Gets static country by name
     * @param ObjectManager $em
     * @param $countryName
     * @return mixed|null
     */
    public static function getCountryByName(ObjectManager $em, $countryName)
    {
        $entCountries = self::getCountriesById($em);
        if (is_null(self::$cacheCountriesByName)) {
            self::$cacheCountriesByName = Utils::getSortedObjects('getName', $entCountries);
        }
        return array_key_exists($countryName, self::$cacheCountriesByName) ? self::$cacheCountriesByName[$countryName] : null;
    }

    /**
     * EntCountry constructor.
     * @param $countryName
     * @param $isoCode
     */
    public function __construct($countryName, $isoCode)
    {
        if (!in_array($countryName, self::$validCountries)) {
            throw new Exception('Invalid country name');
        }

        $this->name = $countryName;
        $this->iso_code = $isoCode;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getIsoCode()
    {
        return $this->iso_code;
    }

    /**
     * Returns if country id is valid
     * @param $countryId
     * @return bool
     */
    public static function isValidCountryId($countryId)
    {
        return array_key_exists($countryId, self::$validCountries);
    }

    /**
     * Returns country name by given country id
     * @param $countryId
     * @return mixed|string
     */
    public static function getCountryNameById($countryId)
    {
        if (!self::isValidCountryId($countryId)) {
            return "";
        }
        return self::$validCountries[$countryId];
    }

    /**
     * Clientmapper
     * @param ObjectManager $em
     * @param bool $fullMapping
     * @return array
     */
    public function clientMapper(ObjectManager $em, $fullMapping = false)
    {
        return [
            'id' => $this->getId(),
            'name' => $this->getName(),
            'iso_code' => $this->getIsoCode(),
        ];
    }
}