<?php

namespace VGM\WOBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * CUnit
 */
class CUnit
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var integer
     */
    private $level;

    /**
     * @var string
     */
    private $adrees;

    /**
     * @var string
     */
    private $city;

    /**
     * @var string
     */
    private $state;

    /**
     * @var integer
     */
    private $zip_code;

    /**
     * @var string
     */
    private $phone1;

    /**
     * @var string
     */
    private $phone2;

    /**
     * @var \DateTime
     */
    private $created_at;

    /**
     * @var \DateTime
     */
    private $updated_at;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $workers;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $tasks;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $children;

    /**
     * @var \VGM\WOBundle\Entity\CUnit
     */
    private $parent;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $departments;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return CUnit
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set level
     *
     * @param integer $level
     *
     * @return CUnit
     */
    public function setLevel($level)
    {
        $this->level = $level;

        return $this;
    }

    /**
     * Get level
     *
     * @return integer
     */
    public function getLevel()
    {
        return $this->level;
    }

    /**
     * Set adrees
     *
     * @param string $adrees
     *
     * @return CUnit
     */
    public function setAdrees($adrees)
    {
        $this->adrees = $adrees;

        return $this;
    }

    /**
     * Get adrees
     *
     * @return string
     */
    public function getAdrees()
    {
        return $this->adrees;
    }

    /**
     * Set city
     *
     * @param string $city
     *
     * @return CUnit
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set state
     *
     * @param string $state
     *
     * @return CUnit
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return string
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set zipCode
     *
     * @param integer $zipCode
     *
     * @return CUnit
     */
    public function setZipCode($zipCode)
    {
        $this->zip_code = $zipCode;

        return $this;
    }

    /**
     * Get zipCode
     *
     * @return integer
     */
    public function getZipCode()
    {
        return $this->zip_code;
    }

    /**
     * Set phone1
     *
     * @param string $phone1
     *
     * @return CUnit
     */
    public function setPhone1($phone1)
    {
        $this->phone1 = $phone1;

        return $this;
    }

    /**
     * Get phone1
     *
     * @return string
     */
    public function getPhone1()
    {
        return $this->phone1;
    }

    /**
     * Set phone2
     *
     * @param string $phone2
     *
     * @return CUnit
     */
    public function setPhone2($phone2)
    {
        $this->phone2 = $phone2;

        return $this;
    }

    /**
     * Get phone2
     *
     * @return string
     */
    public function getPhone2()
    {
        return $this->phone2;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return CUnit
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return CUnit
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Add child
     *
     * @param \VGM\WOBundle\Entity\CUnit $child
     *
     * @return CUnit
     */
    public function addChild(\VGM\WOBundle\Entity\CUnit $child)
    {
        $this->children[] = $child;

        return $this;
    }

    /**
     * Remove child
     *
     * @param \VGM\WOBundle\Entity\CUnit $child
     */
    public function removeChild(\VGM\WOBundle\Entity\CUnit $child)
    {
        $this->children->removeElement($child);
    }

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

    /**
     * Set parent
     *
     * @param \VGM\WOBundle\Entity\CUnit $parent
     *
     * @return CUnit
     */
    public function setParent(\VGM\WOBundle\Entity\CUnit $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \VGM\WOBundle\Entity\CUnit
     */
    public function getParent()
    {
        return $this->parent;
    }

    public function getAllParents()
    {
        $parents = [];
        if($this->parent == null)
            return $parents;
        else{
            $parents = $this->parent->getAllParents();
            $parents[] = $this->parent;

            return $parents;
        }
    }

    public function getAllChildren()
    {
        $children = [];
        if(count($this->children) == 0 )
            return $children;
        else{
            $children = $this->children->getAllParents();
            $children[] = $this->children;

            return $children;
        }
    }

    /**
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        $this->setCreatedAt(new \DateTime());

        return $this;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setUpdatedAtValue()
    {
        $this->setUpdatedAt(new \DateTime());

        return $this;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setLevelValue()
    {
        if($this->parent == null)
            $this->setLevel(0);
        else
            $this->setLevel($this->parent->getLevel() + 1);

        return $this;
    }

    public function __toString()
    {
        $parents = $this->getAllParents();
        $cad = "";
        foreach($parents as $i=>$p){
            $cad = $cad.$p->getName().'-';
            /*if($i < count($parents) - 1)
                $cad = $cad.'-';*/
        }
        return $cad.$this->getName();
    }

    /**
     * Add department
     *
     * @param \VGM\WOBundle\Entity\NDepartment $department
     *
     * @return CUnit
     */
    public function addDepartment(\VGM\WOBundle\Entity\NDepartment $department)
    {
        $this->departments[] = $department;

        return $this;
    }

    /**
     * Remove department
     *
     * @param \VGM\WOBundle\Entity\NDepartment $department
     */
    public function removeDepartment(\VGM\WOBundle\Entity\NDepartment $department)
    {
        $this->departments->removeElement($department);
    }

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

    /**
     * Add worker
     *
     * @param \VGM\WOBundle\Entity\Worker $worker
     *
     * @return CUnit
     */
    public function addWorker(\VGM\WOBundle\Entity\Worker $worker)
    {
        $this->workers[] = $worker;

        return $this;
    }

    /**
     * Remove worker
     *
     * @param \VGM\WOBundle\Entity\Worker $worker
     */
    public function removeWorker(\VGM\WOBundle\Entity\Worker $worker)
    {
        $this->workers->removeElement($worker);
    }

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

    /**
     * Add task
     *
     * @param \VGM\WOBundle\Entity\PlanningTask $task
     *
     * @return CUnit
     */
    public function addTask(\VGM\WOBundle\Entity\PlanningTask $task)
    {
        $this->tasks[] = $task;

        return $this;
    }

    /**
     * Remove task
     *
     * @param \VGM\WOBundle\Entity\PlanningTask $task
     */
    public function removeTask(\VGM\WOBundle\Entity\PlanningTask $task)
    {
        $this->tasks->removeElement($task);
    }

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