<?php

namespace AppBundle\Entity\Education;

use AppBundle\Annotation\ExportSplitFields;
use AppBundle\Entity\AccessAndFunction\Access;
use AppBundle\Entity\Core\Tagg;
use AppBundle\Entity\Traits\ActivityYearTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Finite\StatefulInterface;
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\Education as OpentalentAssert;

/**
 * EducationCurriculum suivi par un Access pendant une année scolaire
 *
 * @Iri("http://schema.org/EducationStudent")
 * @OpentalentAssert\EducationStudentAccessStudent
 * @OpentalentAssert\EducationStudentTiming
 */
#[ORM\Entity(repositoryClass: 'AppBundle\Entity\Education\Repository\EducationStudentRepository')]
class EducationStudent implements StatefulInterface
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    use ActivityYearTrait;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['educationstudent', 'access_details', 'student_list', 'report_card', 'student_registration', 'educationnotation_list', 'examenconvocation_list_student', 'education_input_list', 'education_student_next_year', 'online_registration_access_details', 'accesses_no_reregistred_list'])]
    private $id;
    /**
     * @var Access
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\AccessAndFunction\Access', inversedBy: 'educationStudent', cascade: ['persist'])]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['educationstudent', 'my_student_list', 'educationstudent_reference', 'education_student_next_year', 'educationnotation_list_educationstudent', 'education_input_list', 'education_student_next_year', 'educationstudent_notation', 'education_student_next_year_edit'])]
    private $access;
    /**
     * @var EducationCurriculum
     *
     *
     */
    #[ORM\ManyToOne(targetEntity: 'EducationCurriculum', inversedBy: 'educationStudent')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['educationstudent', 'access_details_educationstudent', 'my_student_list', 'report_card_educationstudent', 'student_registration_educationstudent', 'educationstudent_reference', 'educationnotation_list_educationstudent', 'examenconvocation_list_student', 'education_input_list', 'education_student_next_year', 'education_student_next_year_educationstudentlastyear', 'online_registration_access_details_educationstudent', 'educationstudent_notation', 'accesses_no_reregistred_list_educationstudent', 'student_list_educationstudent'])]
    private $educationCurriculum;
    /**
     * @var Access
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\AccessAndFunction\Access', inversedBy: 'educationStudentByTeacher')]
    #[ORM\JoinTable(name: 'educationstudent_teacher')]
    #[Groups(['educationstudent_teachers', 'report_card_educationstudent', 'access_details_educationstudent', 'student_registration_educationstudent', 'educationstudent_reference', 'student_list_educationstudent', 'educationnotation_list_educationstudent', 'education_student_next_year', 'education_student_next_year_educationstudentlastyear', 'education_input_list', 'accesses_no_reregistred_list_educationstudent'])]
    private $teachers;
    /**
     * @var string
     */
    #[ORM\Column(type: 'string', length: 25, nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['educationstudent', 'report_card_educationstudent'])]
    private $graduation;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['educationstudent', 'report_card_educationstudent', 'education_student_next_year', 'student_registration_educationstudent', 'education_student_next_year_educationstudentlastyear'])]
    private $acquired = false;
    /**
     * @var ArrayCollection<EducationNotation>
     */
    #[Assert\Valid]
    #[ORM\OneToMany(targetEntity: 'AppBundle\Entity\Education\EducationNotation', mappedBy: 'educationStudent', orphanRemoval: true, cascade: ['persist'])]
    #[Groups(['educationstudent_educationnotation', 'report_card_educationstudent', 'education_input_list', 'educationstudent_notation'])]
    private $educationNotations;
    /**
     * @var string
     */
    #[ORM\Column(type: 'text', nullable: true)]
    #[Assert\Type(type: 'string')]
    #[Groups(['educationstudent', 'report_card_educationstudent'])]
    private $annualAppreciation;
    /**
     * @var bool
     */
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Assert\Type(type: 'boolean')]
    #[Assert\NotNull]
    #[Groups(['educationstudent', 'education_student_next_year', 'education_student_next_year_educationstudentlastyear', 'student_registration_educationstudent'])]
    private $registeredFollowingYear = false;
    /**
     * Finite State Status
     * @var
     */
    private $state;
    /**
     * @var bool
     */
    #[Groups(['template', 'education_student_next_year'])]
    private $stateTemplate;
    /**
     * Last year information
     * @var EducationStudent
     */
    #[ORM\ManyToOne(targetEntity: 'EducationStudent', inversedBy: 'referencedEducationStudent')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['education_student_next_year'])]
    private $educationStudentLastYear;
    #[ORM\OneToMany(targetEntity: 'EducationStudent', mappedBy: 'educationStudentLastYear')]
    protected $referencedEducationStudent;
    /**
     * @var boolean
     */
    #[Groups(['educationstudent'])]
    private $allNotationsValid;
    /**
     * @var string
     * @ExportSplitFields({"educationCurriculum.education.educationCategory.label","educationCurriculum.education.educationComplementTemplate","educationCurriculum.cycle.label","educationCurriculum.year","educationCurriculum.level","teachersTemplate","educationTiming.timing"})
     */
    #[Groups(['intangible_list_educationcurriculums', 'educationcurriculum', 'student_list_educationstudent', 'accesses_no_reregistred_list_educationstudent'])]
    private $fullLabelTemplate;
    /**
     * @var string
     */
    #[Groups(['template', 'educationstudent_notation'])]
    private $educationCurriculumTemplate;
    /**
     * @var string
     */
    #[Groups(['template'])]
    private $teachersTemplate;
    /**
     * @var string
     */
    #[Groups(['educationstudent_placeroom', 'access_details_educationstudent'])]
    private $placeRoom;
    /**
     * @var array
     */
    #[ORM\Column(type: 'json_array', nullable: true)]
    #[Groups(['educationstudent', 'educationstudent_days', 'education_student_next_year', 'student_registration_educationstudent'])]
    private $days;
    private $days_export;
    /**
     * @var \DateTime
     */
    #[ORM\Column(type: 'time', nullable: true)]
    #[Assert\Time]
    #[Groups(['educationstudent', 'education_student_next_year', 'student_registration_educationstudent'])]
    private $time;
    /**
     * @var ArrayCollection<Tagg>
     */
    #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Core\Tagg', cascade: ['persist'], inversedBy: 'educationStudents')]
    #[Assert\Valid]
    #[ORM\JoinTable(name: 'tag_educationStudent', joinColumns: [], inverseJoinColumns: [])]
    #[ORM\JoinColumn(name: 'educationStudent_id', referencedColumnName: 'id')]
    #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    #[Groups(['eductionstudent_tags', 'manage_tags'])]
    private $tags;
    /**
     * @var EducationTiming
     */
    #[ORM\ManyToOne(targetEntity: 'EducationTiming', inversedBy: 'educationStudents')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    #[Groups(['educationstudent', 'student_registration_educationstudent', 'access_details_educationstudent', 'student_list_educationstudent', 'education_student_next_year', 'education_student_next_year_educationstudentlastyear'])]
    private $educationTiming;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->educationNotations = new ArrayCollection();
        $this->referencedEducationStudent = new ArrayCollection();
        $this->teachers = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }
    /**
     * Gets allNotationsValid.
     *
     * @return boolean
     */
    public function getAllNotationsValid()
    {
        $allNotationsValid = true;
        $notations = $this->getEducationNotations();

        if(count($notations) == 0) $allNotationsValid = false;

        foreach ($notations as $notation) {
            if(!$notation->getIsValid()){
                $allNotationsValid = false;
                break;
            }
        }
        return $allNotationsValid;
    }
    /**
     * 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 access.
     *
     * @param Access $access
     *
     * @return $this
     */
    public function setAccess(\AppBundle\Entity\AccessAndFunction\Access $access)
    {
        $this->access = $access;

        return $this;
    }
    /**
     * Gets access.
     *
     * @return Access
     */
    public function getAccess()
    {
        return $this->access;
    }
    /**
     * Sets education Curriculum.
     *
     * @param EducationCurriculum $educationCurriculum
     *
     * @return $this
     */
    public function setEducationCurriculum(EducationCurriculum $educationCurriculum = null)
    {
        $this->educationCurriculum = $educationCurriculum;

        return $this;
    }
    /**
     * Gets education Curriculum.
     *
     * @return EducationCurriculum
     */
    public function getEducationCurriculum()
    {
        return $this->educationCurriculum;
    }
    /**
     * Sets graduation.
     *
     * @param string $graduation
     *
     * @return $this
     */
    public function setGraduation($graduation)
    {
        $this->graduation = $graduation;

        return $this;
    }
    /**
     * Gets graduation.
     *
     * @return string
     */
    public function getGraduation()
    {
        return $this->graduation;
    }
    /**
     * Sets Acquired.
     *
     * @param bool $acquired
     *
     * @return $this
     */
    public function setAcquired($acquired)
    {
        $this->acquired = $acquired;

        return $this;
    }
    /**
     * Gets Acquired.
     *
     * @return bool
     */
    public function getAcquired()
    {
        return $this->acquired;
    }
    /**
     * Gets the object state.
     *
     * @return string
     */
    public function getFiniteState()
    {
        return $this->state;
    }
    /**
     * @param string $state
     * @return $this
     */
    public function setFiniteState($state)
    {
        $this->state = $state;
        return $this;
    }
    /**
     * Set annualAppreciation
     *
     * @param string $annualAppreciation
     *
     * @return EducationStudent
     */
    public function setAnnualAppreciation($annualAppreciation)
    {
        $this->annualAppreciation = $annualAppreciation;

        return $this;
    }
    /**
     * Get annualAppreciation
     *
     * @return string
     */
    public function getAnnualAppreciation()
    {
        return $this->annualAppreciation;
    }
    /**
     * Add educationNotation
     *
     * @param \AppBundle\Entity\Education\EducationNotation $educationNotation
     *
     * @return EducationStudent
     */
    public function addEducationNotation(\AppBundle\Entity\Education\EducationNotation $educationNotation)
    {
        $educationNotation->setEducationStudent($this);
        $this->educationNotations[] = $educationNotation;

        return $this;
    }
    /**
     * Remove educationNotation
     *
     * @param \AppBundle\Entity\Education\EducationNotation $educationNotation
     */
    public function removeEducationNotation(\AppBundle\Entity\Education\EducationNotation $educationNotation)
    {
        $this->educationNotations->removeElement($educationNotation);
    }
    /**
     * Get educationNotations
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getEducationNotations()
    {
        return $this->educationNotations;
    }
    /**
     * Add teacher
     *
     * @param \AppBundle\Entity\AccessAndFunction\Access $teacher
     *
     * @return EducationStudent
     */
    public function addTeacher(\AppBundle\Entity\AccessAndFunction\Access $teacher)
    {
        $this->teachers[] = $teacher;

        return $this;
    }
    /**
     * Remove teacher
     *
     * @param \AppBundle\Entity\AccessAndFunction\Access $teacher
     */
    public function removeTeacher(\AppBundle\Entity\AccessAndFunction\Access $teacher)
    {
        $this->teachers->removeElement($teacher);
    }
    /**
     * Get teachers
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTeachers()
    {
        return $this->teachers;
    }
    /**
     * Gets full label.
     *
     * @return array
     */
    public function getFullLabelTemplate()
    {
        return array_merge($this->getEducationCurriculumTemplate(), $this->getTeachersTemplate(), [$this->getEducationTiming() ? $this->getEducationTiming()->getTiming() : '']);
    }
    /**
     * @return array
     */
    public function getEducationCurriculumTemplate()
    {

        $educationCurriculumTemplate = [];
        if($this->getEducationCurriculum())
            $educationCurriculumTemplate = $this->getEducationCurriculum()->getFullLabelTemplate();

        return $educationCurriculumTemplate;
    }
    /**
     * @return array
     */
    public function getTeachersTemplate()
    {

        $teachers = [];
        foreach($this->getTeachers() as $teacher){
            $teachers[] = $teacher->getPerson()->getFullNameTemplate();
        }


        return [implode(", ", $teachers)];
    }
    /**
     * Gets full label.
     *
     * @return string
     */
    public function getPlaceRoom()
    {
        $placeRoom = '';
        $student = $this->getAccess();

        foreach ($student->getCourses() as $course){
            foreach ($course->getEducationCurriculum() as $educationCurri)
                if (!is_null($educationCurri) && !is_null($this->getEducationCurriculum()) &&
                    $educationCurri->getId() === $this->getEducationCurriculum()->getId()
                    && $course->getStartYear() === $this->getStartYear()){
                    $place = $course->getPlace() ? $course->getPlace()->getName().' ' : '';
                    $room = $course->getRoom() ? $course->getRoom()->getName() : '';
                    $placeRoom = $place.$room;
                }
        }

        return $placeRoom;
    }
    /**
     * Set registeredFollowingYear
     *
     * @param boolean $registeredFollowingYear
     *
     * @return EducationStudent
     */
    public function setRegisteredFollowingYear($registeredFollowingYear)
    {
        $this->registeredFollowingYear = $registeredFollowingYear;

        return $this;
    }
    /**
     * Get registeredFollowingYear
     *
     * @return boolean
     */
    public function getRegisteredFollowingYear()
    {
        return $this->registeredFollowingYear;
    }
    /**
     * Sets Education Student Last Year information.
     *
     * @param EducationStudent $access
     *
     * @return $this
     */
    public function setEducationStudentLastYear(EducationStudent $educationStudentLastYear)
    {
        $this->educationStudentLastYear = $educationStudentLastYear;

        return $this;
    }
    /**
     * Gets Education Student Last Year information.
     *
     * @return EducationStudent
     */
    public function getEducationStudentLastYear()
    {
        return $this->educationStudentLastYear;
    }
    /**
     * Set days
     *
     * @param array $days
     *
     * @return EducationStudent
     */
    public function setDays($days)
    {
        $this->days = $days;

        return $this;
    }
    /**
     * Get days
     *
     * @return array
     */
    public function getDays()
    {
        return $this->days;
    }
    /**
     * Set time
     *
     * @param \DateTime $time
     *
     * @return EducationStudent
     */
    public function setTime($time)
    {
        $this->time = $time;

        return $this;
    }
    /**
     * Get time
     *
     */
    public function getTime()
    {
        if($this->time){
            $this->time->setTime($this->time->format("H"), $this->time->format("i"), 0);
            return $this->time->format('H:i');
        }
        return null;
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tagg $tag
     *
     * @return EducationStudent
     */
    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;
    }
    /**
     * Gets full label.
     *
     * @return bool
     */
    public function getStateTemplate()
    {
        $year = date("Y");
        $lastYear = $year - 1;
        return $this->getStartYear() != $lastYear;
    }
    /**
     * Sets EducationTiming
     *
     * @param EducationTiming $educationTiming
     *
     * @return $this
     */
    public function setEducationTiming(EducationTiming $educationTiming = null)
    {
        $this->educationTiming = $educationTiming;

        return $this;
    }
    /**
     * Gets EducationTiming
     *
     * @return EducationTiming
     */
    public function getEducationTiming()
    {
        return $this->educationTiming;
    }
}
