<?php

namespace AppBundle\Entity\Education;

use AppBundle\Entity\Core\Tagg;
use AppBundle\Enum\Education\TypeCriteriaEnum;
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;

/**
 * Notes d'un élève pour un enseignement suivi EducationStudent
 *
 * @Iri("http://schema.org/EducationNotation")
 */
#[ORM\Entity(repositoryClass: 'AppBundle\Entity\Education\Repository\EducationNotationRepository')]
class EducationNotation
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation', 'education_input_list'])]
    private $id;
    /**
     * @var EducationStudent
     */
    #[ORM\ManyToOne(targetEntity: 'EducationStudent', inversedBy: 'educationNotations')]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['educationnotation', 'educationnotation_list'])]
    private $educationStudent;
    /**
     * @var CriteriaNotation
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Education\CriteriaNotation', inversedBy: 'educationNotations')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation_educationnotations', 'education_input_list_educationnotations'])]
    private $criteriaNotation;
    /**
     * @var EducationNotationCriteriaConfig
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Education\EducationNotationCriteriaConfig', inversedBy: 'educationNotations')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation_educationnotations', 'education_input_list_educationnotations'])]
    private $criteriaNotationConfig;
    /**
     * @var string
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $freeCriterion;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', nullable: true)]
    #[Assert\Choice(callback: ['\AppBundle\Enum\Education\PeriodEnum', 'toArray'], multiple: false)]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation_educationnotations', 'education_input_list_educationnotations'])]
    private $period;
    /**
     * @var \DateTime
     */
    #[ORM\Column(type: 'date', nullable: true)]
    #[Assert\Date]
    #[Groups(['educationnotation', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $date;
    /**
     * @var float
     */
    #[ORM\Column(type: 'decimal', nullable: true, precision: 10, scale: 2)]
    #[Assert\GreaterThanOrEqual(value: 0)]
    #[Assert\LessThanOrEqual(value: 100, message: 'lessThanOrEqual20')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation_educationnotations', 'education_input_list_educationnotations'])]
    private $note;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', nullable: true)]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationnotation_list', 'educationstudent_notation_educationnotations', 'education_input_list_educationnotations'])]
    private $mention;
    /**
     * @var string
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $appreciation;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['educationnotation', 'report_card_educationstudent', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $isValid = false;
    /**
     * @var string
     */
    #[Groups(['educationnotation', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $noteTemplate;
    /**
     * @var string
     */
    #[Groups(['educationnotation', 'educationstudent_notation_educationnotations', 'educationnotation_list'])]
    private $noteOriTemplate;
    /**
     * @var ArrayCollection<Tagg>
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Tagg', cascade: ['persist'], inversedBy: 'educationNotations')]
    #[Assert\Valid]
    #[ORM\JoinTable(name: 'tag_educationNotation', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'educationNotation_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    #[Groups(['eductionnotation_tags', 'educationstudent_notation_educationnotations', 'manage_tags'])]
    private $tags;
    public function __construct() {
        $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;
    }
    /**
     * Sets criteriaNotation.
     *
     * @param CriteriaNotation $criteriaNotation
     *
     * @return $this
     */
    public function setCriteriaNotation($criteriaNotation)
    {
        $this->criteriaNotation = $criteriaNotation;

        return $this;
    }
    /**
     * Gets criteriaNotation.
     *
     * @return CriteriaNotation
     */
    public function getCriteriaNotation()
    {
        return $this->criteriaNotation;
    }
    /**
     * Sets freeCriterion.
     *
     * @param string $freeCriterion
     *
     * @return $this
     */
    public function setFreeCriterion($freeCriterion)
    {
        $this->freeCriterion = $freeCriterion;

        return $this;
    }
    /**
     * Gets freeCriterion.
     *
     * @return string
     */
    public function getFreeCriterion()
    {
        return $this->freeCriterion;
    }
    /**
     * Sets period.
     *
     * @param string $period
     *
     * @return $this
     */
    public function setPeriod($period)
    {
        if(empty($period) || $period=='')
            $period = null;

        $this->period = $period;

        return $this;
    }
    /**
     * Gets period.
     *
     * @return string
     */
    public function getPeriod()
    {
        return $this->period;
    }
    /**
     * Sets date.
     *
     * @param \DateTime $date
     *
     * @return $this
     */
    public function setDate(\DateTime $date = null)
    {
        $this->date = $date;

        return $this;
    }
    /**
     * Gets date.
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date ? $this->date->format('Y-m-d') : $this->date;
    }
    /**
     * Sets note.
     *
     * @param float $note
     *
     * @return $this
     */
    public function setNote($note)
    {
        $this->note = (!is_null($note)) ? floatval($note) : $note;

        return $this;
    }
    /**
     * Gets note.
     *
     * @return float
     */
    public function getNote()
    {
        if(is_null($this->note)) return $this->note;
        return round ( $this->note , 2 );
    }
    /**
     * Sets mention.
     *
     * @param string $mention
     *
     * @return $this
     */
    public function setMention($mention)
    {
        $this->mention = $mention;

        return $this;
    }
    /**
     * Gets mention.
     *
     * @return string
     */
    public function getMention()
    {
        return $this->mention;
    }
    /**
     * Sets appreciation.
     *
     * @param string $appreciation
     *
     * @return $this
     */
    public function setAppreciation($appreciation)
    {
        $this->appreciation = $appreciation;

        return $this;
    }
    /**
     * Gets appreciation.
     *
     * @return string
     */
    public function getAppreciation()
    {
        return $this->appreciation;
    }
    /**
     * Set isValid
     *
     * @param boolean $isValid
     *
     * @return EducationNotation
     */
    public function setIsValid($isValid)
    {
        $this->isValid = $isValid;

        return $this;
    }
    /**
     * Get isValid
     *
     * @return boolean
     */
    public function getIsValid()
    {
        return $this->isValid;
    }
    /**
     * Set educationStudent
     *
     * @param \AppBundle\Entity\Education\EducationStudent $educationStudent
     *
     * @return EducationNotation
     */
    public function setEducationStudent(\AppBundle\Entity\Education\EducationStudent $educationStudent)
    {
        $this->educationStudent = $educationStudent;

        return $this;
    }
    /**
     * Get educationStudent
     *
     * @return \AppBundle\Entity\Education\EducationStudent
     */
    public function getEducationStudent()
    {
        return $this->educationStudent;
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     *
     * @return EducationNotation
     */
    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;
    }
    public function getNoteTemplate($withNoteMax = true)
    {
        if(is_null($this->getNote())) return null;
        if(!$this->getCriteriaNotation()) return null;
        if($this->getCriteriaNotation()->getType() !== TypeCriteriaEnum::WITH_NOTATION) return null;

        $noteMax = $this->getEducationStudent()->getAccess()->getOrganization()->getParameters()->getAverage();
        $note = (!is_null($this->getNote())) ? floatval(($this->getNote() * $noteMax) / 100) : $this->getNote();
        $note = round ( $note , 2 );
        return $withNoteMax ? $note . '/'. $noteMax : $note;
    }
    public function getNoteOriTemplate($withNoteMax = true)
    {
        if(is_null($this->getNote())) return null;
        if(!$this->getCriteriaNotation()) return null;
        if($this->getCriteriaNotation()->getType() !== TypeCriteriaEnum::WITH_NOTATION) return null;

        $noteMax = $this->getCriteriaNotation()->getNoteMax();
        $note = (!is_null($this->getNote())) ? floatval(($this->getNote() * $noteMax) / 100) : $this->getNote();
        $note = round ( $note , 2 );
        return $withNoteMax ? $note . '/'. $noteMax : $note;
    }
    /**
     * Sets criteriaNotationConfig.
     *
     * @param EducationNotationCriteriaConfig $criteriaNotationConfig
     *
     * @return $this
     */
    public function setCriteriaNotationConfig($criteriaNotationConfig)
    {
        $this->criteriaNotationConfig = $criteriaNotationConfig;

        return $this;
    }
    /**
     * Gets criteriaNotationConfig.
     *
     * @return EducationNotationCriteriaConfig
     */
    public function getCriteriaNotationConfig()
    {
        return $this->criteriaNotationConfig;
    }
    /**
     * Pour elasticsearch...
     */
    public function noteOrigine(){
        return null;
    }
    public function noteRecalcul(){
        return null;
    }
}
