<?php

namespace AppBundle\Entity;

use AppBundle\Utils\Traits\AddressJsonTrait;
use AppBundle\Utils\Traits\ExtraJsonTrait;
use AppBundle\Utils\Traits\IsVisibleTrait;
use AppBundle\Utils\Traits\TagsJsonTrait;
use AppBundle\Utils\Traits\TimestampableTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Company
 *
 * @ORM\Table(name="company")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Company
{

    use TimestampableTrait;
    use IsVisibleTrait;
    use AddressJsonTrait;
    use ExtraJsonTrait;
    use TagsJsonTrait;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="brand_name", type="string", length=255, options={"comment":"Nom commercial"}, nullable=true)
     */
    private $brandName;

    /**
     * @var string
     *
     * @ORM\Column(name="business_name", type="string", length=255, options={"comment":"Raison sociale"}, nullable=true)
     */
    private $businessName;

    /**
     * @var text
     *
     * @ORM\Column(name="description", type="text", length=255, nullable=true)
     */
    private $description;

    /**
     * @var text
     *
     * @ORM\Column(name="description_admin", type="text", length=255, nullable=true)
     */
    private $descriptionAdmin;

    /**
     * @var string
     *
     * @ORM\Column(name="logo", type="string", nullable=true)
     */
    private $logo;

    /**
     * @var string
     *
     * @ORM\Column(name="website", type="string", nullable=true)
     */
    private $website;

    /**
     * @var string
     *
     * @ORM\Column(name="alias", type="string", length=255, unique=true)
     */
    private $alias;

    /**
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Media", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="presentation_video_id", nullable=true)
     */
    private $presentationVideo;

    /**
     * @var string
     *
     * @ORM\Column(name="type", type="string", nullable=true)
     */
    private $type;

    private $file;

    /**
     * @var string
     *
     * @ORM\Column(name="emphasis", type="boolean", options={"comment":"A la une"}, nullable=true)
     */
    private $emphasis;

    /**
     * @var string
     *
     * @ORM\Column(name="emphasis_order", type="integer", options={"comment":"Ordre"}, nullable=true)
     */
    private $emphasisOrder;

    /**
     * @var bool
     *
     * @ORM\Column(name="searchable", type="boolean", nullable=true)
     */
    private $searchable;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_private", type="boolean", nullable=true, options={"comment":"Company privé, non listée en front"})
     */
    private $isPrivate;

    /***********************
     * OneToMany Array's
     ***********************/

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product", mappedBy="company")
     */
    private $products;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="company", cascade={"persist"})
     */
    private $users;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Ad",mappedBy="company", cascade={"persist", "remove"})
     */
    private $ads;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\TerritoryAd", mappedBy="territory", cascade={"persist", "remove"})
     */
    private $territoryAds;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\TerritoryAd", mappedBy="company", cascade={"persist", "remove"})
     */
    private $territories;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Answer",mappedBy="company", cascade={"persist", "remove"})
     */
    private $spontaneousApplications;

    /***********************
     * Constructor
     ***********************/

    public function __construct()
    {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
        $this->ads = new \Doctrine\Common\Collections\ArrayCollection();
        $this->emphasis = false;
    }

    /***********************
     * Custom Function
     ***********************/

    public function init()
    {
        $this->initAddress();
        $this->initTags();
        $this->initExtra();
    }

    public function upload()
    {
        if (null === $this->file) {
            return;
        }
        //$name = $this->file->getClientOriginalName()."_".uniqid();
        $name = md5(uniqid()) . '.' . $this->file->guessExtension();
        $this->file->move($this->getUploadRootDir(), $name);
        $this->logo = $name;

    }

    public function getUploadDir()
    {
        return 'uploads/img';
    }

    protected function getUploadRootDir()
    {
        return __DIR__ . '/../../../web/' . $this->getUploadDir();
    }

    public function getWebPath()
    {
        return null === $this->logo ? null : $this->getUploadDir() . '/' . $this->logo;
    }

    /**
     * @param string $type
     * @return array|string
     *
     * return token available for a company
     */
    public function getAvailableTokens($type = 'count')
    {
        $monthTokens = array();
        $adTokens = array();
        foreach ($this->getProducts() as $p) {
            if ($p->getType() == "MONTH" && $p->getStatus() == "AVAILABLE") {
                $monthTokens[] = $p;
            } elseif ($p->getType() == "AD" && $p->getStatus() == "AVAILABLE") {
                $adTokens[] = $p;
            }
        }

        if ($type == 'month') {
            return $monthTokens;
        } elseif ($type == 'ad') {
            return $adTokens;
        } elseif ($type == 'count') {
            $tokens = array();
            $tokens['month'] = count($monthTokens);
            $tokens['ad'] = count($adTokens);
            return $tokens;
        } else {
            return 'Invalid parameter';
        }
    }

    public function hasCompanyVideo()
    {
        $response = false;
        foreach ($this->getProducts() as $p) {
            if ($p->getType() == "TTPE" && $p->getStatus() == "AVAILABLE") {
                $response = true;
            }
        }

        return $response;
    }

    public function getPublishedAds()
    {
        $arrayPublishedAds = array();
        foreach ($this->getAds() as $ad) {
            if ($ad->getStatus() == "PUBLISHED") {
                array_push($arrayPublishedAds, $ad);
            }
        }

        return $arrayPublishedAds;
    }

    /**
     * @return mixed
     *
     * Return the first user of the company
     */
    public function getMainUser()
    {
        return $this->users->first();
    }

    /***********************
     * Getter & Setters
     ***********************/

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

    /**
     * Set brandName.
     *
     * @param string|null $brandName
     *
     * @return Company
     */
    public function setBrandName($brandName = null)
    {
        $this->brandName = $brandName;

        return $this;
    }

    /**
     * Get brandName.
     *
     * @return string|null
     */
    public function getBrandName()
    {
        return $this->brandName;
    }

    /**
     * Set businessName.
     *
     * @param string|null $businessName
     *
     * @return Company
     */
    public function setBusinessName($businessName = null)
    {
        $this->businessName = $businessName;

        return $this;
    }

    /**
     * Get businessName.
     *
     * @return string|null
     */
    public function getBusinessName()
    {
        return $this->businessName;
    }

    /**
     * Set description.
     *
     * @param string|null $description
     *
     * @return Company
     */
    public function setDescription($description = null)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description.
     *
     * @return string|null
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set logo.
     *
     * @param string|null $logo
     *
     * @return Company
     */
    public function setLogo($logo = null)
    {
        $this->logo = $logo;

        return $this;
    }

    /**
     * Get logo.
     *
     * @return string|null
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Set website.
     *
     * @param string|null $website
     *
     * @return Company
     */
    public function setWebsite($website = null)
    {
        $this->website = $website;

        return $this;
    }

    /**
     * Get website.
     *
     * @return string|null
     */
    public function getWebsite()
    {
        return $this->website;
    }

    /**
     * Set alias.
     *
     * @param string $alias
     *
     * @return Company
     */
    public function setAlias($alias)
    {
        $this->alias = $alias;

        return $this;
    }

    /**
     * Get alias.
     *
     * @return string
     */
    public function getAlias()
    {
        return $this->alias;
    }

    /**
     * Set type.
     *
     * @param string|null $type
     *
     * @return Company
     */
    public function setType($type = null)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type.
     *
     * @return string|null
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Add ad.
     *
     * @param \AppBundle\Entity\Ad $ad
     *
     * @return Company
     */
    public function addAd(\AppBundle\Entity\Ad $ad)
    {
        $this->ads[] = $ad;

        return $this;
    }

    /**
     * Remove ad.
     *
     * @param \AppBundle\Entity\Ad $ad
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeAd(\AppBundle\Entity\Ad $ad)
    {
        return $this->ads->removeElement($ad);
    }

    /**
     * Get ads.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getAds()
    {
        return $this->ads;
    }

    /**
     * Set presentationVideo.
     *
     * @param \AppBundle\Entity\Media|null $presentationVideo
     *
     * @return Company
     */
    public function setPresentationVideo(\AppBundle\Entity\Media $presentationVideo = null)
    {
        $this->presentationVideo = $presentationVideo;

        return $this;
    }

    /**
     * Get presentationVideo.
     *
     * @return \AppBundle\Entity\Media|null
     */
    public function getPresentationVideo()
    {
        return $this->presentationVideo;
    }

    /**
     * Add product.
     *
     * @param \AppBundle\Entity\Product $product
     *
     * @return Company
     */
    public function addProduct(\AppBundle\Entity\Product $product)
    {
        $this->products[] = $product;

        return $this;
    }

    /**
     * Remove product.
     *
     * @param \AppBundle\Entity\Product $product
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeProduct(\AppBundle\Entity\Product $product)
    {
        return $this->products->removeElement($product);
    }

    /**
     * Get products.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * Add user.
     *
     * @param \AppBundle\Entity\User $user
     *
     * @return Company
     */
    public function addUser(\AppBundle\Entity\User $user)
    {
        $this->users[] = $user;

        return $this;
    }

    /**
     * Remove user.
     *
     * @param \AppBundle\Entity\User $user
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeUser(\AppBundle\Entity\User $user)
    {
        return $this->users->removeElement($user);
    }

    /**
     * Get users.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUsers()
    {
        return $this->users;
    }

    public function getFile()
    {
        return $this->file;
    }

    /**
     * Set file
     *
     * @param UploadedFile $file
     *
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Set emphasis.
     *
     * @param bool|null $emphasis
     *
     * @return Company
     */
    public function setEmphasis($emphasis = null)
    {
        $this->emphasis = $emphasis;

        return $this;
    }

    /**
     * Get emphasis.
     *
     * @return bool|null
     */
    public function getEmphasis()
    {
        return $this->emphasis;
    }

    /**
     * Set emphasisOrder.
     *
     * @param int|null $emphasisOrder
     *
     * @return Company
     */
    public function setEmphasisOrder($emphasisOrder = null)
    {
        $this->emphasisOrder = $emphasisOrder;

        return $this;
    }

    /**
     * Get emphasisOrder.
     *
     * @return int|null
     */
    public function getEmphasisOrder()
    {
        return $this->emphasisOrder;
    }

    /**
     * Set searchable.
     *
     * @param bool|null $searchable
     *
     * @return Company
     */
    public function setSearchable($searchable = null)
    {
        $this->searchable = $searchable;

        return $this;
    }

    /**
     * Get searchable.
     *
     * @return bool|null
     */
    public function getSearchable()
    {
        return $this->searchable;
    }

    /**
     * Add territoryAd.
     *
     * @param \AppBundle\Entity\TerritoryAd $territoryAd
     *
     * @return Company
     */
    public function addTerritoryAd(\AppBundle\Entity\TerritoryAd $territoryAd)
    {
        $this->territoryAds[] = $territoryAd;

        return $this;
    }

    /**
     * Remove territoryAd.
     *
     * @param \AppBundle\Entity\TerritoryAd $territoryAd
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeTerritoryAd(\AppBundle\Entity\TerritoryAd $territoryAd)
    {
        return $this->territoryAds->removeElement($territoryAd);
    }

    /**
     * Get territoryAds.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTerritoryAds()
    {
        return $this->territoryAds;
    }

    /**
     * Add territory.
     *
     * @param \AppBundle\Entity\TerritoryAd $territory
     *
     * @return Company
     */
    public function addTerritory(\AppBundle\Entity\TerritoryAd $territory)
    {
        $this->territories[] = $territory;

        return $this;
    }

    /**
     * Remove territory.
     *
     * @param \AppBundle\Entity\TerritoryAd $territory
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeTerritory(\AppBundle\Entity\TerritoryAd $territory)
    {
        return $this->territories->removeElement($territory);
    }

    /**
     * Get territories.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTerritories()
    {
        return $this->territories;
    }

    /**
     * Add spontaneousApplication.
     *
     * @param \AppBundle\Entity\Answer $spontaneousApplication
     *
     * @return Company
     */
    public function addSpontaneousApplication(\AppBundle\Entity\Answer $spontaneousApplication)
    {
        $this->spontaneousApplications[] = $spontaneousApplication;

        return $this;
    }

    /**
     * Remove spontaneousApplication.
     *
     * @param \AppBundle\Entity\Answer $spontaneousApplication
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeSpontaneousApplication(\AppBundle\Entity\Answer $spontaneousApplication)
    {
        return $this->spontaneousApplications->removeElement($spontaneousApplication);
    }

    /**
     * Get spontaneousApplications.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getSpontaneousApplications()
    {
        return $this->spontaneousApplications;
    }

    /**
     * Set descriptionAdmin.
     *
     * @param string|null $descriptionAdmin
     *
     * @return Company
     */
    public function setDescriptionAdmin($descriptionAdmin = null)
    {
        $this->descriptionAdmin = $descriptionAdmin;

        return $this;
    }

    /**
     * Get descriptionAdmin.
     *
     * @return string|null
     */
    public function getDescriptionAdmin()
    {
        return $this->descriptionAdmin;
    }

    /**
     * Set isPrivate.
     *
     * @param bool|null $isPrivate
     *
     * @return Company
     */
    public function setIsPrivate($isPrivate = null)
    {
        $this->isPrivate = $isPrivate;

        return $this;
    }

    /**
     * Get isPrivate.
     *
     * @return bool|null
     */
    public function getIsPrivate()
    {
        return $this->isPrivate;
    }
}
