<?php

namespace App\Entity;


use App\Entity\IFace\IEntity;
use App\Utils\Utils;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Config\Definition\Exception\Exception;

/**
 * @ORM\Entity
 * @ORM\Table(name="customer_note")
 */
class EntCustomerNote implements IEntity
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer", nullable=false, options={"unsigned" = true})
     */
    protected $customer_id;

    /**
     * @ORM\Column(type="integer", nullable=true, options={"unsigned" = true})
     */
    protected $customer_contact_id;

    /**
     * @ORM\Column(name="gender", type="string", columnDefinition="enum('male', 'female', 'diverse')")
     */
    protected $gender;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $firstname;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $lastname;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $email;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $phone_no;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $mobile_no;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $fax_no;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $department;

    /**
     * @ORM\Column(type="string", nullable=false)
     */
    protected $title;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $comment;

    /**
     * @ORM\Column(type="date", nullable=false)
     */
    protected $note_date;

    /**
     * @ORM\Column(type="integer", nullable=false, options={"unsigned" = true})
     */
    protected $creation_user_id;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $creation_user_firstname;

    /**
     * @ORM\Column(type="string", nullable=false)
     */
    protected $creation_user_lastname;

    /**
     * @ORM\Column(type="datetime", nullable=false)
     */
    protected $creation_date;


    /**
     * EntCustomerNote constructor.
     * @param ObjectManager $em
     * @param EntCustomer $entCustomer
     * @param EntUser $entUser
     * @param $title
     * @throws \Exception
     */
    public function __construct(ObjectManager $em, EntCustomer $entCustomer, EntUser $entUser, $title)
    {
        if (is_null($entCustomer->getId())) {
            throw new Exception('no id found');
        }

        if (is_null($entUser->getId())) {
            throw new Exception('no id found');
        }

        $this->customer_id = $entCustomer->getId();
        $this->creation_user_id = $entUser->getId();
        $this->creation_user_firstname = $entUser->getFirstname();
        $this->creation_user_lastname = $entUser->getLastname();
        $this->title = $title;
        $this->note_date = new \DateTime();
        $this->creation_date = new \DateTime();
    }

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

    /**
     * @return mixed
     */
    public function getCustomerId()
    {
        return $this->customer_id;
    }

    /**
     * @return mixed
     */
    public function getCreationUserId()
    {
        return $this->creation_user_id;
    }

    /**
     * @return mixed
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * @return mixed
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * @return mixed
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @return mixed
     */
    public function getPhoneNo()
    {
        return $this->phone_no;
    }

    /**
     * @return mixed
     */
    public function getMobileNo()
    {
        return $this->mobile_no;
    }

    /**
     * @return mixed
     */
    public function getFaxNo()
    {
        return $this->fax_no;
    }

    /**
     * @return mixed
     */
    public function getDepartment()
    {
        return $this->department;
    }

    /**
     * @return mixed
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @return mixed
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * @return mixed
     */
    public function getNoteDate()
    {
        return $this->note_date;
    }

                /**
     * @return mixed
     */
    public function getCreationUserFirstname()
    {
        return $this->creation_user_firstname;
    }

    /**
     * @return mixed
     */
    public function getCreationUserLastname()
    {
        return $this->creation_user_lastname;
    }

    /**
     * @return mixed
     */
    public function getCreationDate()
    {
        return $this->creation_date;
    }

    /**
     * @return mixed
     */
    public function getCustomerContactId()
    {
        return $this->customer_contact_id;
    }

    /**
     * @param ObjectManager $em
     * @param $customer_contact_id
     */
    public function setCustomerContactId(ObjectManager $em, $customer_contact_id): void
    {
        if ($customer_contact_id == 0) {
            $this->customer_contact_id = $customer_contact_id;
        } else {
            /** @var EntCustomerContact $entCustomerContact */
            $entCustomerContact = $em->getRepository('App:EntCustomerContact')->find($customer_contact_id);
            // NOTE: Check if contact still exists
            $this->customer_contact_id = !is_null($entCustomerContact) ? $customer_contact_id : null;
        }
    }

    /**
     * @param mixed $gender
     */
    public function setGender($gender): void
    {
        if (!is_null($gender) && !Utils::isValidGender($gender)) {
            throw new Exception('invalid gender');
        }
        $this->gender = $gender;
    }

    /**
     * @param mixed $firstname
     */
    public function setFirstname($firstname): void
    {
        $this->firstname = $firstname;
    }

    /**
     * @param mixed $lastname
     */
    public function setLastname($lastname): void
    {
        $this->lastname = $lastname;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email): void
    {
        $this->email = $email;
    }

    /**
     * @param mixed $phone_no
     */
    public function setPhoneNo($phone_no): void
    {
        $this->phone_no = $phone_no;
    }

    /**
     * @param mixed $mobile_no
     */
    public function setMobileNo($mobile_no): void
    {
        $this->mobile_no = $mobile_no;
    }

    /**
     * @param mixed $fax_no
     */
    public function setFaxNo($fax_no): void
    {
        $this->fax_no = $fax_no;
    }

    /**
     * @param mixed $department
     */
    public function setDepartment($department): void
    {
        $this->department = $department;
    }

    /**
     * @param mixed $title
     */
    public function setTitle($title): void
    {
        $this->title = $title;
    }

    /**
     * @param mixed $comment
     */
    public function setComment($comment): void
    {
        $this->comment = $comment;
    }

    /**
     * @param mixed $note_date
     */
    public function setNoteDate($note_date): void
    {
        $this->note_date = \DateTime::createFromFormat('Y-m-d', $note_date);
    }

    /**
     * Sets client data
     * @param ObjectManager $em
     * @param $clientData
     */
    public function setClientData(ObjectManager $em, $clientData)
    {
        $this->setCustomerContactId($em, $clientData->customer_contact_id);
        $this->setGender($clientData->gender);
        $this->setFirstname($clientData->firstname);
        $this->setLastname($clientData->lastname);
        $this->setEmail($clientData->email);
        $this->setPhoneNo($clientData->phone_no);
        $this->setMobileNo($clientData->mobile_no);
        $this->setFaxNo($clientData->fax_no);
        $this->setDepartment($clientData->department);
        $this->setTitle($clientData->title);
        $this->setComment($clientData->comment);
        $this->setNoteDate($clientData->note_date);
    }

    /**
     * Client mapper
     * @param ObjectManager $em
     * @param bool $fullMapping
     * @return array
     */
    public function clientMapper(ObjectManager $em, $fullMapping = false)
    {
        return [
            'id' => $this->id,
            'customer_id' => $this->customer_id,
            'customer_contact_id' => $this->customer_contact_id,
            'gender' => $this->gender,
            'firstname' => $this->firstname,
            'lastname' => $this->lastname,
            'email' => $this->email,
            'phone_no' => $this->phone_no,
            'mobile_no' => $this->mobile_no,
            'fax_no' => $this->fax_no,
            'department' => $this->department,
            'title' => $this->title,
            'comment' => $this->comment,
            'note_date' => !is_null($this->note_date) ? $this->note_date->format('Y-m-d'): null,
            'creation_date' => !is_null($this->creation_date) ? $this->creation_date->format('Y-m-d'): null,
            'creation_user_id' => $this->getCreationUserId(),
            'creation_user_firstname' => $this->getCreationUserFirstname(),
            'creation_user_lastname' => $this->getCreationUserLastname(),
        ];
    }

}
