<?php

namespace AppBundle\Entity\Product;

use AppBundle\Annotation\ExportSplitFields;
use AppBundle\Entity\AccessAndFunction\Access;
use AppBundle\Entity\Core\Tagg;
use AppBundle\Entity\Person\Person;
use AppBundle\Entity\Traits\ActivityPeriodTrait;
use AppBundle\Entity\Traits\BillingRuleTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Entity\Traits\TimestampableEntity;
use AppBundle\Entity\Traits\CreatorUpdaterEntity;
use AppBundle\Validator\Constraints\Product as OpentalentAssert;

/**
 * Location d'éaquipement,
 * Fait le lien entre un Access et un Equipment sur une période donnée
 *
 * @Iri("http://schema.org/EquipmentLoan")
 * @OpentalentAssert\EquipmentLoanAvailableRent
 */
#[ORM\Entity]
class EquipmentLoan
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    use ActivityPeriodTrait;
    use BillingRuleTrait;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'equipmentloan_list', 'accessequipment_grid', 'own_access', 'access_details', 'equipment_details', 'own_student_access', 'equipment_list', 'equipmentmediatheque_list', 'equipmentcostume_list', 'equipment_availability_form'])]
    private $id;
    /**
     * @var Access
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\AccessAndFunction\Access', inversedBy: 'equipmentLoans', cascade: ['persist'])]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'equipmentloan_list', 'equipment_details_equipmentloanfiltered', 'equipment_list_equipmentloan', 'equipmentmediatheque_list_equipmentloan', 'equipmentcostume_list_equipmentloan', 'equipment_availability_form', 'equipmentloan_list', 'equipmentrent_list'])]
    private $borrower;
    /**
     * @var Equipment
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Product\Equipment', inversedBy: 'equipmentLoan')]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'equipmentloan_list', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'access_details_equipmentloans', 'own_student_access_equipmentloans', 'equipment_availability_form'])]
    private $equipment;
    /**
     * @var \DateTime
     */
    #[ORM\Column(type: 'date', nullable: true)]
    #[Assert\Date]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'equipmentloan_list', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'access_details_equipmentloans', 'own_student_access_equipmentloans', 'equipment_availability_form'])]
    private $endDateTheorical;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Assert\Choice(callback: ['\AppBundle\Enum\Product\LoanTypeEnum', 'toArray'])]
    #[Groups(['equipmentloan', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'access_details_equipmentloans', 'own_student_access_equipmentloans', 'equipmentloan_list', 'equipment_list_equipmentloan', 'equipmentmediatheque_list_equipmentloan', 'equipmentcostume_list_equipmentloan', 'equipment_availability_form'])]
    private $type;
    /**
     * @var float
     *
     *
     */
    #[ORM\Column(type: 'float', nullable: true)]
    #[Assert\Type(type: 'float')]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'equipmentloan_list', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'own_student_access_equipmentloans', 'equipment_availability_form'])]
    private $deposit;
    /**
     * @var float
     */
    #[ORM\Column(type: 'float', nullable: true)]
    #[Assert\Type(type: 'float')]
    #[Groups(['equipmentloan', 'equipmentrent_list', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'own_student_access_equipmentloans', 'equipment_availability_form'])]
    private $amount;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', nullable: true)]
    #[Assert\Type(type: 'boolean')]
    #[Groups(['equipmentloan', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans', 'own_student_access_equipmentloans', 'equipment_availability_form'])]
    private $forcingAmount = false;
    /**
     * @var ArrayCollection<BillLine>
     */
    #[ORM\OneToMany(targetEntity: 'AppBundle\Entity\Billing\BillLine', mappedBy: 'equipmentLoan', orphanRemoval: true)]
    #[Groups(['equipmentloan_billline'])]
    private $billLines;
    /**
     * @var ArrayCollection<BillingIntangibleExcludeDate>
     */
    #[Assert\Valid]
    #[ORM\OneToMany(targetEntity: 'AppBundle\Entity\Billing\BillingIntangibleExcludeDate', mappedBy: 'equipmentLoan', cascade: ['persist'], orphanRemoval: true)]
    #[Groups(['accessfictionalintangible', 'accessequipment_grid_equipmentloans', 'own_access_equipmentloans'])]
    private $billingIntangibleExcludeDates;
    /**
     * @var string
     * @ExportSplitFields({"borrower.person.name","borrower.person.givenName","type","startDate","endDate"})
     *
     */
    #[Groups(['template'])]
    private $fullLabelTemplate;
    /**
     * @var ArrayCollection<Tagg>
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Tagg', cascade: ['persist'], inversedBy: 'equipmentLoans')]
    #[Assert\Valid]
    #[ORM\JoinTable(name: 'tag_equipmentLoan', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'equipmentLoan_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    #[Groups(['equipmentloan_tags', 'manage_tags', 'equipmentloan_list', 'equipmentrent_list'])]
    private $tags;
    public function __construct()
    {
        $this->billLines = new ArrayCollection();
        $this->billingIntangibleExcludeDates = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }
    /**
     * Sets id.
     *
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }
    /**
     * Gets id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @param Access|null $borrower
     * @return $this
     */
    public function setBorrower(Access $borrower = null)
    {
        $this->borrower = $borrower;

        return $this;
    }
    /**
     * Gets Borrower.
     *
     * @return Access
     */
    public function getBorrower()
    {
        return $this->borrower;
    }
    /**
     * Sets equipment.
     *
     * @param Equipment $equipment
     *
     * @return $this
     */
    public function setEquipment(Equipment $equipment = null)
    {
        $this->equipment = $equipment;

        return $this;
    }
    /**
     * Gets equipment.
     *
     * @return Equipment
     */
    public function getEquipment()
    {
        return $this->equipment;
    }
    /**
     * Sets endDateTheorical.
     *
     * @param \DateTime $endDateTheorical
     *
     * @return $this
     */
    public function setEndDateTheorical(\DateTime $endDateTheorical = null)
    {
        $this->endDateTheorical = $endDateTheorical;

        return $this;
    }
    /**
     * Gets endDateTheorical.
     *
     * @return \DateTime
     */
    public function getEndDateTheorical()
    {
        return $this->endDateTheorical ? $this->endDateTheorical->format('Y-m-d') : $this->endDateTheorical;
    }
    /**
     * Sets type.
     *
     * @param string $type
     *
     * @return $this
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }
    /**
     * Gets type.
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }
    /**
     * Sets deposit.
     *
     * @param float $deposit
     *
     * @return $this
     */
    public function setDeposit($deposit)
    {
        $this->deposit = floatval($deposit);

        return $this;
    }
    /**
     * Gets deposit.
     *
     * @return float
     */
    public function getDeposit()
    {
        return $this->deposit;
    }
    /**
     * Sets amount.
     *
     * @param float $amount
     *
     * @return $this
     */
    public function setAmount($amount)
    {
        $this->amount = floatval($amount);

        return $this;
    }
    /**
     * Gets amount.
     *
     * @return float
     */
    public function getAmount()
    {
        return $this->amount;
    }
    /**
     * Add billLine
     *
     * @param \AppBundle\Entity\Billing\BillLine $billLine
     *
     * @return EquipmentLoan
     */
    public function addBillLine(\AppBundle\Entity\Billing\BillLine $billLine)
    {
        $this->billLines[] = $billLine;

        return $this;
    }
    /**
     * Remove billLine
     *
     * @param \AppBundle\Entity\Billing\BillLine $billLine
     */
    public function removeBillLine(\AppBundle\Entity\Billing\BillLine $billLine)
    {
        $this->billLines->removeElement($billLine);
    }
    /**
     * Get billLines
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getBillLines()
    {
        return $this->billLines;
    }
    /**
     * Add billingIntangibleExcludeDate
     *
     * @param \AppBundle\Entity\Billing\BillingIntangibleExcludeDate $billingIntangibleExcludeDate
     *
     * @return EquipmentLoan
     */
    public function addBillingIntangibleExcludeDate(\AppBundle\Entity\Billing\BillingIntangibleExcludeDate $billingIntangibleExcludeDate)
    {
        $this->billingIntangibleExcludeDates[] = $billingIntangibleExcludeDate;

        return $this;
    }
    /**
     * Remove billingIntangibleExcludeDate
     *
     * @param \AppBundle\Entity\Billing\BillingIntangibleExcludeDate $billingIntangibleExcludeDate
     */
    public function removeBillingIntangibleExcludeDate(\AppBundle\Entity\Billing\BillingIntangibleExcludeDate $billingIntangibleExcludeDate)
    {
        $this->billingIntangibleExcludeDates->removeElement($billingIntangibleExcludeDate);
    }
    /**
     * Get billingIntangibleExcludeDates
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getBillingIntangibleExcludeDates()
    {
        return $this->billingIntangibleExcludeDates;
    }
    /**
     * Set forcingAmount
     *
     * @param boolean $forcingAmount
     *
     * @return EquipmentLoan
     */
    public function setForcingAmount($forcingAmount)
    {
        $this->forcingAmount = $forcingAmount;

        return $this;
    }
    /**
     * Get forcingAmount
     *
     * @return boolean
     */
    public function getForcingAmount()
    {
        return $this->forcingAmount;
    }
    /**
     * Gets full label.
     *
     * @return array
     */
    public function getFullLabelTemplate()
    {
        $fullLabel = [];

        $fullLabel[] = $this->getBorrower() ? $this->getBorrower()->getPerson()->getFullNameTemplate() : '';
        $fullLabel[] = ['value' => $this->getType(), 'translate' => true];
        $fullLabel[] = ['type' => 'date', 'value' => $this->getStartDate()];

        if(!empty($this->getEndDate()))
            $fullLabel[] = ['type' => 'date', 'value' => $this->getEndDate()];

        return $fullLabel;
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     *
     * @return EquipmentLoan
     */
    public function addTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }
    /**
     * Remove tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     */
    public function removeTag(\AppBundle\Entity\Core\Tagg $tag)
    {
        $this->tags->removeElement($tag);
    }
    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }
}
