<?php

namespace KupShop\I18nBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;

/**
 * @ORM\Entity
 *
 * @ORM\Table(name="countries")
 *
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
 */
class Country
{
    /**
     * @ORM\Id
     *
     * @ORM\Column(type="string", length=10)
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=60)
     */
    private $name;

    /**
     * @ORM\Column(type="integer")
     */
    private $position;

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

    /**
     * Set position.
     *
     * @param int $position
     *
     * @return Country
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * Get position.
     *
     * @return int
     */
    public function getPosition()
    {
        return $this->position;
    }

    /**
     * Set id.
     *
     * @param string $id
     *
     * @return Country
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Set name.
     *
     * @param string $name
     *
     * @return Country
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get localized name.
     *
     * @return string
     */
    public function getName()
    {
        if (Contexts::get(LanguageContext::class)->translationActive()) {
            if ($name = translate($this->id, 'countries', true)) {
                return $name;
            }
        }

        return $this->name;
    }

    /**
     * Get untranslated name.
     *
     * @return string
     */
    public function getRawName()
    {
        return $this->name;
    }

    public function getCode3()
    {
        $iso3166 = new \League\ISO3166\ISO3166();

        return $iso3166->alpha2($this->id)['alpha3'];
    }

    public function getCode2()
    {
        return $this->id;
    }

    public function getCodeNumeric()
    {
        $iso3166 = new \League\ISO3166\ISO3166();

        return $iso3166->alpha2($this->id)['numeric'];
    }
}
