<?php

namespace RxConnexionBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use RxConnexionBundle\Entity\Base\BaseEntity;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;

/**
 * @ORM\Entity(repositoryClass="RxConnexionBundle\Repository\AuditTrailRepository")
 * @ORM\Table(name="audit_trail", indexes={@ORM\Index(name="IDX_SEARCH_000", columns={"table_name", "table_id", "user_type", "user_id"})})
 */
class AuditTrailEntity extends BaseEntity
{
    const USER_TYPE_ACCOUNT = 'Account';
    const USER_TYPE_SYSTEM = 'System';
    const AXN_NEW = 'New';
    const AXN_UPDATE = 'Update';
    const AXN_DELETE = 'Delete';

    // Entity holder
    public $entity;

    // Original details holder
    private $originalDetails;

    // new details holder
    private $details;

    /**
     * @ORM\Column(name="table_name", type="string")
     */
    protected $tableName;

    /**
     * @ORM\Column(name="table_id", type="bigint")
     */
    protected $tableId;

    /**
     * @ORM\Column(name="tran_label", type="string")
     */
    protected $tranLabel;

    /**
     * @ORM\Column(name="axn", type="string", length=50)
     */
    protected $axn;

    /**
     * @ORM\Column(name="user_type", type="string", length=20)
     */
    protected $userType;

    /**
     * @ORM\Column(name="user_id", type="bigint")
     */
    protected $userId;

    /**
     * @ORM\Column(name="data", type="text")
     */
    protected $data;

    /**
     * Set createdAt
     *
     * @ORM\PrePersist
     */
    public function setCreatedAtValue()
    {
        parent::setCreatedAtValue();

        $data = array();

        if(isset($this->originalDetails)) {
            $data['Original Details'] = $this->originalDetails;
        }

        $data['Details'] = $this->details;

        $this->data = json_encode($data);
        $this->tableId = $this->details['id'];
        $this->userType = isset($_COOKIE['userType']) && !is_null($_COOKIE['userType']) && !empty($_COOKIE['userType']) ? $_COOKIE['userType'] : 'Super Admin';
        $this->userId = isset($_COOKIE['userId']) && !is_null($_COOKIE['userId']) && !empty($_COOKIE['userId']) ? $_COOKIE['userId'] : '';
    }

    /**
     * Constructor
     */
    public function __construct($tranLabel, $tableName, $axn)
    {
        // We can't put the serializer because context can only be used once.

        $this->tranLabel = $tranLabel;
        $this->tableName = $tableName;
        $this->axn = $axn;
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Methods         														  */
    /*--------------------------------------------------------------------------------------------------------*/

    /**
     * @return $this
     */
    public function parseOriginalDetails() {

        $serializer = SerializerBuilder::create()->build();
        $context = SerializationContext::create();
        $context->setGroups('forAudit');
        $context->setSerializeNull(true);
        $context->enableMaxDepthChecks();

        $this->originalDetails = $serializer->toArray($this->entity, $context);

        return $this;
    }

    /**
     * @return $this
     */
    public function parseDetails() {

        $serializer = SerializerBuilder::create()->build();
        $context = SerializationContext::create();
        $context->setGroups('forAudit');
        $context->setSerializeNull(true);
        $context->enableMaxDepthChecks();

        $this->details = $serializer->toArray($this->entity, $context);

        return $this;
    }

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Use Defined Setters and Getters														  */
    /*--------------------------------------------------------------------------------------------------------*/

    /*--------------------------------------------------------------------------------------------------------*/
    /*					Setters and Getters																	  */
    /*--------------------------------------------------------------------------------------------------------*/

    /**
     * Set tableName
     *
     * @param string $tableName
     *
     * @return AuditTrailEntity
     */
    public function setTableName($tableName)
    {
        $this->tableName = $tableName;

        return $this;
    }

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

    /**
     * Set tableId
     *
     * @param integer $tableId
     *
     * @return AuditTrailEntity
     */
    public function setTableId($tableId)
    {
        $this->tableId = $tableId;

        return $this;
    }

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

    /**
     * Set tranLabel
     *
     * @param string $tranLabel
     *
     * @return AuditTrailEntity
     */
    public function setTranLabel($tranLabel)
    {
        $this->tranLabel = $tranLabel;

        return $this;
    }

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

    /**
     * Set axn
     *
     * @param string $axn
     *
     * @return AuditTrailEntity
     */
    public function setAxn($axn)
    {
        $this->axn = $axn;

        return $this;
    }

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

    /**
     * Set data
     *
     * @param string $data
     *
     * @return AuditTrailEntity
     */
    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

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

    /**
     * Set userType
     *
     * @param string $userType
     *
     * @return AuditTrailEntity
     */
    public function setUserType($userType)
    {
        $this->userType = $userType;

        return $this;
    }

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

    /**
     * Set userId
     *
     * @param integer $userId
     *
     * @return AuditTrailEntity
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;

        return $this;
    }

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

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

    /**
     * Set createdBy
     *
     * @param string $createdBy
     *
     * @return AuditTrailEntity
     */
    public function setCreatedBy($createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set updatedBy
     *
     * @param string $updatedBy
     *
     * @return AuditTrailEntity
     */
    public function setUpdatedBy($updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

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

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

        return $this;
    }

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