<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use AppBundle\Utils\Traits\CareerJsonTrait;
/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

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

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="status", type="string", nullable=true, options={"comment":"Etat de visibilite"})
     */
    private $status;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Company", inversedBy="users", cascade={"persist"})
     */
    private $company;

    private $file;

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

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Answer",mappedBy="user", cascade={"persist", "remove"})
     * @ORM\OrderBy({"sendTime" = "DESC"})
     */
    private $answers;

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

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Product",mappedBy="user", cascade={"persist", "remove"})
     */
    private $products;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Media",mappedBy="creator", cascade={"persist", "remove"})
     */
    private $medias;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Bookmark",mappedBy="creator", cascade={"persist", "remove"})
     */
    private $bookmarks;

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Bookmark", inversedBy="sharedWith", cascade={"persist", "remove"})
     */
    private $sharedBookmarks;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Message",mappedBy="sender", cascade={"persist", "remove"})
     */
    private $sendedMessages;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Message",mappedBy="recipient", cascade={"persist", "remove"})
     */
    private $receivedMessages;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Recommendation",mappedBy="user", cascade={"persist", "remove"})
     */
    private $recommendations;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Recommendation",mappedBy="sender", cascade={"persist", "remove"})
     */
    private $sendedRecommendations;

    /************************
     * Social Network
     ************************/

    /**
     * @ORM\Column(name="facebook_id", type="string", length=255, nullable=true)
     */
    private $facebookId;

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

    /**
     * @ORM\Column(name="linkedin_id", type="string", length=255, nullable=true)
     */
    private $linkedinId;

    /**
     * @var string
     *
     * @ORM\Column(name="linkedin_access_token", type="text", nullable=true)
     */

    private $linkedinAccessToken;

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


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

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

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

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

    public function __construct()
    {
        parent::__construct();
        parent::setUsername(uniqId());
        $this->answers = new ArrayCollection();
        $this->ads = new ArrayCollection();
        $this->bookmarks = new ArrayCollection();
        $this->sharedBookmarks = new ArrayCollection();
        $this->medias = new ArrayCollection();
        $this->products = new ArrayCollection();
        $this->recommendations = new ArrayCollection();
        $this->sendedRecommendations = new ArrayCollection();
    }

    /***********************
     * Custom Functions
     ***********************/

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

    }

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

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

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

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

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

    /**
     * @param string $type
     * @return array|string
     */
    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';
        }
    }

    /**
     * @return string
     */
    public function getCivility()
    {
        if ($this->gender == 'male') {
            return 'user.civility.man';
        } elseif ($this->gender == 'female') {
            return 'user.civility.woman';
        } else {
            return '';
        }
    }

    /**
     * @return bool
     */
    public function isIndexableTalent()
    {
        //if ($this->company->getType() == "TALENT") {
        if (in_array('ROLE_TALENT', $this->getRoles())) {
            return true;
        } else {
            return false;
        }
    }

    public function setEmail($email)
    {
        $email = is_null($email) ? '' : $email;
        parent::setEmail($email);
        $this->setUsername($email);

        return $this;
    }

    /***********************
     * Getters & Setters
     ***********************/

    /**
     * Set firstname.
     *
     * @param string|null $firstname
     *
     * @return User
     */
    public function setFirstname($firstname = null)
    {
        $this->firstname = $firstname;

        return $this;
    }

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

    /**
     * Set lastname.
     *
     * @param string|null $lastname
     *
     * @return User
     */
    public function setLastname($lastname = null)
    {
        $this->lastname = $lastname;

        return $this;
    }

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

    /**
     * Set birthday.
     *
     * @param \DateTime|null $birthday
     *
     * @return User
     */
    public function setBirthday($birthday = null, $isSocial = false)
    {
        if($birthday != null){
            if($isSocial){
                $birthday = \DateTime::createFromFormat('m/d/Y', $birthday);
            } else {
                $birthday = \DateTime::createFromFormat('d/m/Y', $birthday);
            }
        }

        $this->birthday = $birthday;

        return $this;
    }

    /**
     * Get birthday.
     *
     * @return \DateTime|null
     */
    public function getBirthday()
    {
        if ($this->birthday == null) {
            
            return $this->birthday;
        }

        return $this->birthday->format('d/m/Y');
    }

    /**
     * Set photo.
     *
     * @param string|null $photo
     *
     * @return User
     */
    public function setPhoto($photo = null)
    {
        $this->photo = $photo;

        return $this;
    }

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

    /**
     * Set gender.
     *
     * @param string|null $gender
     *
     * @return User
     */
    public function setGender($gender = null)
    {
        $this->gender = $gender;

        return $this;
    }

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

    /**
     * Set status.
     *
     * @param string|null $status
     *
     * @return User
     */
    public function setStatus($status = null)
    {
        $this->status = $status;

        return $this;
    }

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

    /**
     * Set facebookId.
     *
     * @param string|null $facebookId
     *
     * @return User
     */
    public function setFacebookId($facebookId = null)
    {
        $this->facebookId = $facebookId;

        return $this;
    }

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

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

        return $this;
    }

    /**
     * Get company.
     *
     * @return \AppBundle\Entity\Company|null
     */
    public function getCompany()
    {
        return $this->company;
    }

    /**
     * Add answer.
     *
     * @param \AppBundle\Entity\Answer $answer
     *
     * @return User
     */
    public function addAnswer(\AppBundle\Entity\Answer $answer)
    {
        $this->answers[] = $answer;

        return $this;
    }

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

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

    /**
     * Add ad.
     *
     * @param \AppBundle\Entity\Ad $ad
     *
     * @return User
     */
    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;
    }

    /**
     * Add product.
     *
     * @param \AppBundle\Entity\Product $product
     *
     * @return User
     */
    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 media.
     *
     * @param \AppBundle\Entity\Media $media
     *
     * @return User
     */
    public function addMedia(\AppBundle\Entity\Media $media)
    {
        $this->medias[] = $media;

        return $this;
    }

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

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

    /**
     * Add bookmark.
     *
     * @param \AppBundle\Entity\Bookmark $bookmark
     *
     * @return User
     */
    public function addBookmark(\AppBundle\Entity\Bookmark $bookmark)
    {
        $this->bookmarks[] = $bookmark;

        return $this;
    }

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

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

    /**
     * Add sharedBookmark.
     *
     * @param \AppBundle\Entity\Bookmark $sharedBookmark
     *
     * @return User
     */
    public function addSharedBookmark(\AppBundle\Entity\Bookmark $sharedBookmark)
    {
        $this->sharedBookmarks[] = $sharedBookmark;

        return $this;
    }

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

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

    /**
     * Add sendedMessage.
     *
     * @param \AppBundle\Entity\Message $sendedMessage
     *
     * @return User
     */
    public function addSendedMessage(\AppBundle\Entity\Message $sendedMessage)
    {
        $this->sendedMessages[] = $sendedMessage;

        return $this;
    }

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

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

    /**
     * Add receivedMessage.
     *
     * @param \AppBundle\Entity\Message $receivedMessage
     *
     * @return User
     */
    public function addReceivedMessage(\AppBundle\Entity\Message $receivedMessage)
    {
        $this->receivedMessages[] = $receivedMessage;

        return $this;
    }

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

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

    /**
     * Set linkedinId.
     *
     * @param string|null $linkedinId
     *
     * @return User
     */
    public function setLinkedinId($linkedinId = null)
    {
        $this->linkedinId = $linkedinId;

        return $this;
    }

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

    /**
     * Set socialPicture.
     *
     * @param string|null $socialPicture
     *
     * @return User
     */
    public function setSocialPicture($socialPicture = null)
    {
        $this->socialPicture = $socialPicture;

        return $this;
    }

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

    /**
     * Set facebookAccessToken.
     *
     * @param string|null $facebookAccessToken
     *
     * @return User
     */
    public function setFacebookAccessToken($facebookAccessToken = null)
    {
        $this->facebookAccessToken = $facebookAccessToken;

        return $this;
    }

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

    /**
     * Set linkedinAccessToken.
     *
     * @param string|null $linkedinAccessToken
     *
     * @return User
     */
    public function setLinkedinAccessToken($linkedinAccessToken = null)
    {
        $this->linkedinAccessToken = $linkedinAccessToken;

        return $this;
    }

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

    /**
     * Get the value of recommendations
     */
    public function getRecommendations()
    {
        return $this->recommendations;
    }

    /**
     * Add Recommendation.
     *
     * @param \AppBundle\Entity\Recommendation $reco
     *
     * @return User
     */
    public function addRecommendation(\AppBundle\Entity\Recommendation $reco)
    {
        $this->recommendations[] = $reco;

        return $this;
    }

    /**
     * Remove Recommendation.
     *
     * @param \AppBundle\Entity\Recommendation $reco
     *
     */
    public function removeRecommendation(\AppBundle\Entity\Recommendation $reco)
    {
        return $this->recommendations->removeElement($reco);
    }

    /**
     * Get the value of sendedRecommendations
     */
    public function getSendedRecommendations()
    {
        return $this->sendedRecommendations;
    }

    /**
     * Add sendedRecommendations.
     *
     * @param \AppBundle\Entity\Recommendation $reco
     *
     * @return User
     */
    public function addSendedRecommendations(\AppBundle\Entity\Recommendation $reco)
    {
        $this->sendedRecommendations[] = $reco;

        return $this;
    }

    /**
     * Remove sendedRecommendations.
     *
     * @param \AppBundle\Entity\Recommendation $reco
     *
     */
    public function removeSendedRecommendations(\AppBundle\Entity\Recommendation $reco)
    {
        return $this->sendedRecommendations->removeElement($reco);
    }

    /**
     * Set career.
     *
     * @param json_document|null $career
     *
     * @return User
     */
    public function setCareer($career = null)
    {
        $this->career = $career;

        return $this;
    }

    /**
     * Get career.
     *
     * @return json_document|null
     */
    public function getCareer()
    {
        return $this->career;
    }

    /**
     * Add sendedRecommendation.
     *
     * @param \AppBundle\Entity\Recommendation $sendedRecommendation
     *
     * @return User
     */
    public function addSendedRecommendation(\AppBundle\Entity\Recommendation $sendedRecommendation)
    {
        $this->sendedRecommendations[] = $sendedRecommendation;

        return $this;
    }

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

    /**
     * Set formation.
     *
     * @param json_document|null $formation
     *
     * @return User
     */
    public function setFormation($formation = null)
    {
        $this->formation = $formation;

        return $this;
    }

    /**
     * Get formation.
     *
     * @return json_document|null
     */
    public function getFormation()
    {
        return $this->formation;
    }

    /**
     * Set language.
     *
     * @param json_document|null $language
     *
     * @return User
     */
    public function setLanguage($language = null)
    {
        $this->language = $language;

        return $this;
    }

    /**
     * Get language.
     *
     * @return json_document|null
     */
    public function getLanguage()
    {
        return $this->language;
    }
}
